-
Notifications
You must be signed in to change notification settings - Fork 362
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
Add ability to include node built-in modules as external dependencies #303
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 You're killin' it @krismuniz~!
I wouldn't rush to change anything (others should weigh in first), but I think this should be on by default, replacing the minuscule natives-exception list (here).
Right now, using --external natives
will prevent you from also making your (peer)dependencies external too, unless you declared them all individually, too.
If we have all natives on by default, a local dependency with the same name should/will already be external & should/will still be found when require
d.
@lukeed that would be even better! Something like this, right? - let external = ['dns', 'fs', 'path', 'url'].concat(/* ... */);
+ let external = require('module').builtinModules.concat(/* ... */);
Indeed, if for some reason it were impractical to just include them by default we could also make external = external.concat(peerDeps).concat(options.external.split(','));
// if options.external contains 'natives', include built-in modules
external = options.external.split(',').includes('natives')
? external.concat(require('module').builtinModules)
: external;
💯yes, that would be the ideal scenario, imho |
Correct! The only other thing to consider here is that |
I also agree that this should be the default with node as target, we could use this static list for this - https://github.com/sindresorhus/builtin-modules/blob/master/builtin-modules.json |
Don't think we need an extra dependency – the list is built-in to Node in all versions: require('module').builtinModules || Object.keys(process.binding('natives')) |
Right! So if we want Node 4.x support we'd do something like this: - let external = require('module').builtinModules.concat(/* ... */);
+ const builtinModules = require('module').builtinModules || Object.keys(process.binding('natives'));
/* ... */
+ builtinModules.concat(/* ... */); Would this apply only when |
@krismuniz it should only apply to If we want to support both automatic builtin externals for target=node and if (target=='node' && external.indexOf('natives')===-1) {
external.push('natives');
}
// ...
const i = external.indexOf('natives');
if (i!==-1) {
const builtins = require('module').builtinModules || Object.keys(process.binding('natives'));
external.splice(i, 1, builtins);
} |
Any news on this? This would be super helpful. |
Problems mentioned above would have to be solved |
FWIW this behavior is already enabled by default in Note: we could still merge this PR, though I'm not sure it does anything as of the update. |
Hello 👋
This PR adds the ability to include node built-in modules as external dependencies by specifying
--external natives
Problem
When using
microbundle
for building node modules (--target node
), you have to include every built-in module like this:This becomes even more tedious when a built-in module uses other built-in modules.
As @lukeed suggests in this comment, it would be nice to have the option to simply treat all built-in node modules as external dependencies by adding
--external natives
instead of--external events,util,child_process[, ...]
.Solution
Add all modules from
require('module').builtinModules
to the list of external dependencies like this:Feedback Welcome
✏️Let me know if you need any changes
🗯If this is not ideal, what would be a nice alternative to specifying all built-in modules?