-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Export module for Node when MODULARIZE=1 #5239
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
Export module for Node when MODULARIZE=1 #5239
Conversation
src/detectNode.js
Outdated
@@ -0,0 +1,32 @@ | |||
var isNodeEnvironment = function() { | |||
// Same as in shell.js |
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.
indentation should be 2 spaces
src/detectNode.js
Outdated
// 2) We could be the application main() thread proxied to worker. (with Emscripten -s PROXY_TO_WORKER=1) (ENVIRONMENT_IS_WORKER == true, ENVIRONMENT_IS_PTHREAD == false) | ||
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true) | ||
|
||
if (Module['ENVIRONMENT']) { |
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.
Module
may not exist here, first because we may have a different export name, second because we just have the modularize function, and it's parameter is the Module object, but it will only be called later.
I think all we can do is the code path that does not use Module
. I think that should be enough here?
Thanks, overall looks good. A few comments above. Also, we should add a test for this (may be a test for node exporting already, where we can add to that test or add a new test alongside it). |
I think this would be handy for code using webpack/browserify/etc as well, but I think they would fail the runtime check for isNodeEnvironment() by matching positive for being a web or worker environment. Maybe test for 'require' and 'module', rather than for 'process' and not-being-web? |
@Brion a good suggestion. For this PR would you mind if I keep it as similar to existing code first? I.e. what's in We could do a second PR later and make it even more general. Perhaps this is relevant https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js |
@kripken thanks for the review! Hope it's good now. |
Looks good, just still thinking about the issue that @Brion mentioned. Is the issue also an identical existing issue with I lean to thinking of it as a separate issue, as |
Yeah it's the same in my opinion but definitely worth doing later. I.e. in a separate PR. (edit for clarity) |
This partially addresses #5093. I wouldn't say it fixes it though, because won't it not work if a browserified module is used in a browser? I think it should do as UMD does: Also in Electron it will probably act as both a browser and node. |
Added authorship Not passing this Simplify since Module may not exist. Fix tabs.
OK yeah it does make sense. Updated. |
Great, thanks. |
As mentioned in this issue #5216
Basically if you turn on modularisation the function cannot be called if you
require(...)
it in a node environment.This PR adds environment detection code if
MODULARIZE=1
and exports the module.Some tests failed but I don't believe as a result of these changes