Ignoring packages during dependency resolution
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 5.6k
- Forks
- 696
- Avg merge
- 8m
- Merged PRs (30d)
- 7
Description
Do you want to request a feature or report a bug? Question/feature
What is the current behavior?
We have a dependency which is built as a C library. In order to use and test this with our RN app, we have one package with our node bindings (node-dep) and another with our RN bindings (rn-dep). So our code looks like:
let dep = null
if (typeof navigator === 'undefined' || navigator.product !== 'ReactNative') {
dep = require("node-dep")
} else {
dep = require("rn-dep")
}
We blacklistRE this dependency because if it is discovered, you see this package itself specifies a main module field that could not be resolved, presumably because it is not a valid RN package.
The node version of the package is not needed when packaging for RN, but metro discovers the conditional code requiring it and complains that it can't be found:
error: bundling failed: Error: Unable to resolve module `node-dep` from `/home/omit/node_modules/other_dependency/file.js`: Module `node-dep` does not exist in the Haste module map or in these directories:
/home/omit/node_modules
So either we blacklistRE the node package to exclude it, and it fails to be found (though not needed), or we do not blacklistRE and it fails to package because it's not an RN package.
What is the expected behavior?
Is there a way to tell metro to simply ignore any require("node-dep") and just have the app crash at runtime if it were encountered? I'd like to be able to blacklist the package directory and just have metro skip it over. dynamicDepsInPackages does not seem helpful here.
Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.
metro.config.js:
const blacklist = require('metro-config/src/defaults/blacklist')
const blacklistRE = blacklist([/.*node-dep.*/])
module.exports = {
resolver: {
blacklistRE,
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
metro - 0.51.1 (determined by RN version)
yarn - 1.22.0
node - 10.17.0
OS - Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reported metro.config.js, especially resolver.blacklistRE, and compare its behavior with dynamicDepsInPackages. Trace how the node-dep require is resolved from other_dependency/file.js and use the reported bundling error as the reproduction case. Done means a blacklisted package can be omitted without a bundling failure for an unreachable conditional require.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, react-native
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100