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

fix: improve native module detection #674

Merged
merged 3 commits into from
Sep 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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']
Copy link
Contributor

Choose a reason for hiding this comment

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

Much better than a bunch of ||!


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