Skip to content

Commit

Permalink
fix: Don't wrap native modules (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Jul 15, 2024
1 parent 1db08ef commit f3278a3
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,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')

0 comments on commit f3278a3

Please sign in to comment.