Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't wrap native modules #142

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ function createHook (meta) {
return result
}

// We don't want to attempt to wrap native modules
if (result.url.endsWith('.node')) {
return result
}

// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
Expand Down
5 changes: 5 additions & 0 deletions lib/get-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ async function getCjsExports (url, context, parentLoad, source) {
}
// Resolve the re-exported module relative to the current module.
const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href

if (newUrl.endsWith('.node')) {
return
}

for (const each of await getExports(newUrl, context, parentLoad)) {
full.add(each)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@babel/core": "^7.23.7",
"@babel/eslint-parser": "^7.23.3",
"@babel/plugin-syntax-import-assertions": "^7.23.3",
"@node-rs/crc32": "^1.10.3",
"@react-email/components": "^0.0.19",
"@types/node": "^18.0.6",
"c8": "^7.8.0",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/native-modules/darwin-arm64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-darwin-arm64')
1 change: 1 addition & 0 deletions test/fixtures/native-modules/darwin-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-darwin-x64')
1 change: 1 addition & 0 deletions test/fixtures/native-modules/linux-arm64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-linux-arm64-gnu')
1 change: 1 addition & 0 deletions test/fixtures/native-modules/linux-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-linux-x64-gnu')
1 change: 1 addition & 0 deletions test/fixtures/native-modules/win32-arm64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-win32-arm64-msvc')
1 change: 1 addition & 0 deletions test/fixtures/native-modules/win32-x64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@node-rs/crc32-win32-x64-msvc')
10 changes: 10 additions & 0 deletions test/hook/v14-native-modules.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { strictEqual } from 'assert'

// We dynamically import a specific file that imports the
// native module for this platform and architecture.
//
// This way we know the file exists and the native module can
// be loaded.
const lib = await import(`../fixtures/native-modules/${process.platform}-${process.arch}.js`)

strictEqual(typeof lib.default.crc32, 'function')
Loading