Support various module loaders with MODULARIZE#6006
Support various module loaders with MODULARIZE#6006kripken merged 9 commits intoemscripten-core:incomingfrom
Conversation
|
Thanks! So, I don't really understand these ecosystems myself. How important is it to support AMD? We haven't gotten a request for this support, but I'm not sure if that means anything... maybe not enough people are using MODULARIZE... |
|
Not sure how urgent this is for the vast amount of emscripten users, but when I create JavaScript modules, I always make sure that it is a proper UMD module so it "just works". The reason for proposing this here is that AMD-support is gone from binaryen.js after the switch to MODULARIZE, and I'd like to still provide an UMD module using the binaryen.js buildbot that publishes to npm. Ultimately, I'd guess that AMD will be gone for good at some point in time, with the new import/export syntax modern JavaScript has, but it's still somewhat too early to phase it out, imho. Nonetheless, no matter how you decide, it's also easy to patch AMD support into the buildbot by simply concatenating one additional line of code. |
@dcodeIO Indeed, it will be nice to see standard UMD. What will it take to properly wrap emcc generated code with UMD? The proposed fix might have some unwanted effect when a packager will auto-detect the module format and/or replace e.g. |
|
Afaik there is already a check for CommonJS (just changed it to a more standard one in this PR) and there's always a global var. So the only missing thing basically is to check for the presence of the |
|
I was playing UMD variant proposed here at https://github.com/yurydelendik/old-man-sandbox/tree/master/emcc-6006 . See test.js and result produced by webpack after its consuming the input. Notice that webpack was confused about this AMD format and added some polyfill to the output. I also added "test.old.js" to see old format output and "test.proposed.js" to wrap body in standard UMD format. The latter looks like a better choice. |
|
One issue I see with if (typeof module === "object" && module.exports)is that Not sure about the polyfill code being added by webpack. Maybe it's safe enough when just doing: if (typeof module === "object" && module && module.exports) |
|
The webpack generates different UMD header, see webpack/webpack#5826 . We can use something along these lines of: |
|
I see. Though, this makes the Mitigation: |
If somebody will produce such environment, they will have issues with almost all UMD modules. There is no point to protect us from such use case IMHO. |
I agree.
Personally I'd say that something that's so easy to mitigate should be mitigated (unless it still confuses webpack, ofc), but that's debatable and I'm fine with both. |
It's better to choose something standard (canonical UMD, webpack UMD, etc.), so packagers will not be confused that much and they could provide the best transformation for the generated module. |
|
Alright, let's just use webpack's version then. |
Note that this still relies on the 'var' being defined priorily, which already catches the omitted last case of assigning to 'root' and replaces 'factory()'.
While there are externs for module and module.exports, it appears that there are no AMD-specific externs (anymore)
|
(Updated example at https://github.com/yurydelendik/old-man-sandbox/blob/master/emcc-6006/result.js) The proposed patch looks good to me. |
|
Looks good to me too. Can we add a test for the new functionality? We do test modularize and require in node already, so that should already be covered. |
|
Should be good now incl. a test, wdyt? |
|
Thanks, looks good. Please just add yourself to AUTHORS before we merge (can make the commit message with |
|
Sorry, messed up the CI. Not, my, day. |
|
This change unfortunately breaks the import from typescript: if I change the exports from: (emscripten v1.37.34, latest NodeJS, TypeScript, rollup). |
|
@ncave can you provide a little bit more information such as minimal test case to demonstrate the issue? or reason why it breaks? |
|
Did something change with closure externs? The code, as is, relies on externs for |
|
@yurydelendik Here is what gets generated: And how it's being consumed: works again, as already mentioned, if exports are changed back to: |
|
@ncave why there is |
|
@yurydelendik Doh, I was adding that before to make it work, I guess I need to find another way. Thanks a lot, sorry about that. |
|
Small working TS example: main.ts: mywasm.js: |
|
@yurydelendik Thanks, I really appreciate your help, sorry about mucking the PR instead of an opening an issue! Looks like my problem was mainly with rollup. |
As discussed in WebAssembly/binaryen#1341 this change adds support for AMD loaders to MODULARIZE-ed outputs.