Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: improve native module detection (#674)
Browse files Browse the repository at this point in the history
* fix: add node-gyp to native module detection

* feat: detect modules with native file

* chore: doh
  • Loading branch information
eduardoboucas committed Sep 27, 2021
1 parent f756b03 commit 2de5426
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 23 deletions.
33 changes: 19 additions & 14 deletions src/runtimes/node/native_modules/detector.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// eslint-disable-next-line complexity
const isNativeModule = ({ binary, dependencies = {}, devDependencies = {}, gypfile }) =>
Boolean(
dependencies.bindings ||
dependencies.prebuild ||
dependencies.nan ||
dependencies['node-pre-gyp'] ||
dependencies['node-gyp-build'] ||
devDependencies.prebuild ||
devDependencies['node-pre-gyp'] ||
devDependencies['node-gyp-build'] ||
gypfile ||
binary,
)
const { extname } = require('path')

const markerModules = ['bindings', 'nan', 'node-gyp', 'node-gyp-build', 'node-pre-gyp', 'prebuild']

const isNativeModule = ({ binary, dependencies = {}, devDependencies = {}, files = [], gypfile }) => {
if (binary || gypfile) {
return true
}

const hasMarkerModule = markerModules.some((marker) => dependencies[marker] || devDependencies[marker])

if (hasMarkerModule) {
return true
}

const hasBinaryFile = files.some((path) => !path.startsWith('!') && extname(path) === '.node')

return hasBinaryFile
}

module.exports = { isNativeModule }
6 changes: 5 additions & 1 deletion tests/fixtures/node-module-native-buildtime/function.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = require('test')
const moduleWithNodeFile = require('module-with-node-file')
const moduleWithNodeGyp = require('module-with-node-gyp')
const moduleWithPrebuild = require('module-with-prebuild')

module.exports = [moduleWithNodeFile, moduleWithNodeGyp, moduleWithPrebuild]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion tests/fixtures/node-module-native-buildtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "node-module-native-buildtime",
"version": "1.0.0",
"dependencies": {
"test": "1.0.0"
"module-with-node-file": "3.0.0",
"module-with-node-gyp": "1.0.0",
"module-with-prebuild": "2.0.0"
}
}
25 changes: 20 additions & 5 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,32 @@ testBundlers(
})
const requires = await getRequires({ filePath: resolve(tmpDir, 'function.js') })
const normalizedRequires = new Set(requires.map(unixify))
const modulePath = resolve(FIXTURES_DIR, `${fixtureDir}/node_modules/test`)

t.is(files.length, 1)
t.is(files[0].runtime, 'js')
t.true(await pathExists(`${tmpDir}/node_modules/test/native.node`))
t.true(await pathExists(`${tmpDir}/node_modules/test/side-file.js`))
t.true(normalizedRequires.has('test'))

const moduleWithNodeFile = resolve(FIXTURES_DIR, `${fixtureDir}/node_modules/module-with-node-file`)
t.true(await pathExists(`${tmpDir}/node_modules/module-with-node-file/native.node`))
t.true(await pathExists(`${tmpDir}/node_modules/module-with-node-file/side-file.js`))
t.true(normalizedRequires.has('module-with-node-file'))

const moduleWithNodeGypPath = resolve(FIXTURES_DIR, `${fixtureDir}/node_modules/module-with-node-gyp`)
t.true(await pathExists(`${tmpDir}/node_modules/module-with-node-gyp/native.node`))
t.true(await pathExists(`${tmpDir}/node_modules/module-with-node-gyp/side-file.js`))
t.true(normalizedRequires.has('module-with-node-gyp'))

const moduleWithPrebuildPath = resolve(FIXTURES_DIR, `${fixtureDir}/node_modules/module-with-prebuild`)
t.true(await pathExists(`${tmpDir}/node_modules/module-with-prebuild/native.node`))
t.true(await pathExists(`${tmpDir}/node_modules/module-with-prebuild/side-file.js`))
t.true(normalizedRequires.has('module-with-prebuild'))

// We can only detect native modules when using esbuild.
if (bundler !== DEFAULT) {
t.deepEqual(files[0].nativeNodeModules, { test: { [modulePath]: '1.0.0' } })
t.deepEqual(files[0].nativeNodeModules, {
'module-with-node-file': { [moduleWithNodeFile]: '3.0.0' },
'module-with-node-gyp': { [moduleWithNodeGypPath]: '1.0.0' },
'module-with-prebuild': { [moduleWithPrebuildPath]: '2.0.0' },
})
}
},
)
Expand Down

1 comment on commit 2de5426

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 12.8s

largeDepsZisi: 1m 9.5s

Please sign in to comment.