You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for -sEXPORT_ES6/*.mjs on Node.js (#17915)
As described in #11792, `require()` and `__dirname` doesn't exist in
an ES6 module. Emscripten uses this to import built-in core Node.js
modules. For example, the `node:fs` module is used for synchronously
importing the `*.wasm` binary, when not linking with `-sSINGLE_FILE`.
To work around this, ES6 modules on Node.js may import
`createRequire()` from `node:module` to construct the `require()`
function, allowing modules to be imported in a CommonJS manner.
Emscripten targets a variety of environments, which can be
categorized as:
1. Multi-environment builds, which is the default when
`-sENVIRONMENT=*` is not specified at link time.
2. Single-environment, e.g. only web or Node.js as target.
For use case (1), this commit ensures that an `async` function is
emitted, allowing Node.js modules to be dynamically imported. This is
necessary given that static import declarations cannot be put in
conditionals. Inside the module, for Node.js only, it's using the
above-mentioned `createRequire()`-construction.
For use case (2), when only Node.js is targeted, a static import
declaration utilize the same `createRequire()`-construction.
For both use cases, `-sUSE_ES6_IMPORT_META=0` is not allowed, when
Node.js is among the targets, since it is not possible to mimic
`__dirname` when `import.meta` support is not available.
This commit does not change anything for use case (2), when only the
web is targeted (`-sENVIRONMENT=web`).
Resolves: #11792.
Copy file name to clipboardExpand all lines: src/shell.js
+23-6
Original file line number
Diff line number
Diff line change
@@ -133,9 +133,6 @@ var ENVIRONMENT_IS_WASM_WORKER = Module['$ww'];
133
133
#if SHARED_MEMORY&&!MODULARIZE
134
134
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
135
135
// before the page load. In non-MODULARIZE modes generate it here.
0 commit comments