-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
What's the right way to use webpack-specific functionality in node.js? #183
Comments
I also have a use case for this. I think it can be solved like this:
Use in your app like this:
|
I would improve on that by having the require be It would be nice if there was a way to do it without having to require |
Polyfill the functions on the top of a module using them: if(typeof require.ensure !== "function") require.ensure = function(d, c) { c(require) };
if(typeof require.include !== "function") require.include = function() {}; I added a commit that would remove that polyfills in a bundled version. |
Those would have to be added at the top of every module that wanted to use them? That's a lot of annoying boilerplate.. Not a very friendly way to add this functionality. |
Polyfill is a also a solution but I'd prefer having |
I don't think that this are many modules. You generally use chunking only in a same fraction of modules. Using I have a module system for node.js providing |
The difference is that This could actually extend to quite a number of modules. Many modules have rarely-used functionality, or have so many use cases that only a fraction of the module is ever actually used. In these cases, the module could manage loading (or preloading) code as necessary. Without an ability to do this, published modules can't be chunked apart, but must always be loaded whole. You make a good point that other module systems can't use something webpack-specific, but once you go webpack, why would you go back? ; ) Interesting, so you're basically saying that enhanced-require does something similar to what we were proposing above? Meaning we could use it like this:
? |
Why do you bundle code for node.js? I'm just curious, because I can't imagine an use-case :) |
You don't. The whole point would be to have modules that define their own split-points. For node.js they should be ignored, but for webpack, they should be used to do code splitting. |
Btw, I tried out enhanced-require, and it works pretty well - tho I did run into one bug (which wouldn't really affect code I would care about anyway, I don't think). |
So you'd like to pre-bundle your (npm-)modules so they can be used on the client and the server? |
Ah ok. So is your module part of your application or of a npm-module? |
I'd like to be able to use this for npm-modules. No pre-bundling, but rather it would have the split point ready for when someone does want to bundle it. |
Mhmm. I always tried to leave bundle-specific stuff to the application because npm-modules should not depend on a specific bundler. They should be plain node-style modules. Bundling is still a new topic and there are barely any standards. The only thing that might work across bundlers is the browser-field (#151). |
The ideal solution should cause non-supporting bundlers/module-loaders to treat it exactly like node does - a no op. |
What's the suggested way to do this, 9 months on? M |
I guess it's still shimming like sokra explained it |
yep |
/me goes ahead and adds the shim to 16 files |
+1, for a better solution than shimming every file. I'm currently building an App stack that can be used in Browser & Node. As an example: A lot of Javascript engines now implement Promises, but what I'm doing is poly-filling with Bluebird for ones that don't, and keeping the native version for speed. Bluebird compressed is still 75k, so bundling here makes sense. Because polyfilling node's require is not ideal, polyfilling another global say like requireEnsure would be a lot easer and only needs to be done once at app startup. Of course this would require webpack to see requireEnsure as an alias for require.ensure ... |
I use this
in global of node |
@KpjComp FYI Bluebird is surprisingly/embarrassingly faster than native last I checked. |
@snadn: Nice one,.. Only problem is webpack is giving a critical dependencies warning for this bit -> var proto = Object.getPrototypeOf(require); @fresheneesz: I did a couple of tests, var k = 0;var proms = [];console.time('t1');for (var l = 0; l < 10000; l ++) proms.push(new Promise(function (resolve, reject) { k ++; resolve(); }));Promise.all(proms).then(function () { console.timeEnd('t1'); }) My results,.. Ah!!, forget that. What I didn't do in Bluebird was -> Node Bluebird then gives 9.998ms.. and then Chrome bluebird -> 71.670ms So indeed Native promises are embarrassingly slower,.. What gives, doesn't make any sense.. |
Closing since there are plenty of clear answers here. |
webpack defines
require.ensure
andrequire.include
, which aren't available in node.js proper. If I want to create modules that use these functions, but still run in node.js, how do I do that? They can certainly just be defined as no-ops in node.js.The text was updated successfully, but these errors were encountered: