-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Eliminate createRequire/require from EXPORT_ES6 output
#26384
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
base: main
Are you sure you want to change the base?
Changes from all commits
ed92d7f
1aa83dd
858e996
f543d1e
d879569
c91fc51
7c85b0e
265d866
9ad25fe
b8d8fbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -975,6 +975,39 @@ function makeModuleReceiveWithVar(localName, moduleName, defaultValue) { | |
| return ret; | ||
| } | ||
|
|
||
| function makeNodeImport(module, guard = true) { | ||
| assert(ENVIRONMENT_MAY_BE_NODE, 'makeNodeImport called when environment can never be node'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have this assert in |
||
| var expr; | ||
| if (EXPORT_ES6) { | ||
| // Emit a plain top-level `await import()`. When Closure is enabled, | ||
| // `mangleUnsupportedSyntax()` rewrites `await import` to the | ||
| // EMSCRIPTEN$AWAIT$IMPORT placeholder before Closure runs and | ||
| // `fix_js_mangling()` restores it afterwards (Closure can't parse | ||
| // top-level await); without Closure it is left as-is. | ||
| // | ||
| // The `/* webpackIgnore: true */` hint is required because webpack does | ||
| // NOT auto-handle `node:`-prefixed URIs for dynamic import; without it, | ||
| // webpack fails with `UnhandledSchemeError: Reading from "node:xxx" is | ||
| // not handled by plugins` (see test_webpack_esm_output_clean). | ||
| // `/* @vite-ignore */` similarly silences vite's dynamic-import analysis. | ||
| expr = `await import(/* webpackIgnore: true */ /* @vite-ignore */ '${module}')`; | ||
| } else { | ||
| expr = `require('${module}')`; | ||
| } | ||
| if (guard) { | ||
| return `ENVIRONMENT_IS_NODE ? ${expr} : undefined`; | ||
| } | ||
| return expr; | ||
| } | ||
|
|
||
| function makeNodeFilePath(filename) { | ||
| assert(ENVIRONMENT_MAY_BE_NODE, 'makeNodeFilePath called when environment can never be node'); | ||
| if (EXPORT_ES6) { | ||
| return `new URL('${filename}', import.meta.url)`; | ||
| } | ||
| return `__dirname + '/${filename}'`; | ||
| } | ||
|
|
||
| function makeRemovedFSAssert(fsName) { | ||
| assert(ASSERTIONS); | ||
| const lower = fsName.toLowerCase(); | ||
|
|
@@ -1253,6 +1286,8 @@ addToCompileTimeContext({ | |
| makeModuleReceive, | ||
| makeModuleReceiveExpr, | ||
| makeModuleReceiveWithVar, | ||
| makeNodeFilePath, | ||
| makeNodeImport, | ||
| makeRemovedFSAssert, | ||
| makeRetainedCompilerSettings, | ||
| makeReturn64, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,7 @@ var ENVIRONMENT_IS_WORKER = !!globalThis.WorkerGlobalScope; | |
|
|
||
| #if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS) | ||
| if (ENVIRONMENT_IS_NODE) { | ||
| var worker_threads = require('node:worker_threads'); | ||
| var worker_threads = {{{ makeNodeImport('node:worker_threads', false) }}}; | ||
| globalThis.Worker = worker_threads.Worker; | ||
| ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread; | ||
| } | ||
|
|
@@ -104,9 +104,14 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) { | |
| var defaultPrint = console.log.bind(console); | ||
| var defaultPrintErr = console.error.bind(console); | ||
| if (ENVIRONMENT_IS_NODE) { | ||
| var fs = require('node:fs'); | ||
| var fs = {{{ makeNodeImport('node:fs', false) }}}; | ||
| defaultPrint = (...args) => fs.writeSync(1, args.join(' ') + '\n'); | ||
| defaultPrintErr = (...args) => fs.writeSync(2, args.join(' ') + '\n'); | ||
| #if (ASSERTIONS || RUNTIME_DEBUG || AUTODEBUG) | ||
| var utils = {{{ makeNodeImport('node:util', false) }}}; | ||
| var dbg_node_fs = fs; | ||
| var dbg_node_utils = utils; | ||
| #endif | ||
| } | ||
| var out = defaultPrint; | ||
| var err = defaultPrintErr; | ||
|
|
@@ -115,6 +120,16 @@ var out = (...args) => console.log(...args); | |
| var err = (...args) => console.error(...args); | ||
| #endif | ||
|
|
||
| #if !PTHREADS && WASM_WORKERS && ENVIRONMENT_MAY_BE_NODE && (ASSERTIONS || RUNTIME_DEBUG || AUTODEBUG) | ||
| // Initialize dbg() node module references for WASM_WORKERS without PTHREADS. | ||
| // (With PTHREADS these are set in the print setup block above.) | ||
| var dbg_node_fs, dbg_node_utils; | ||
| if (ENVIRONMENT_IS_NODE) { | ||
| dbg_node_fs = {{{ makeNodeImport('node:fs', false) }}}; | ||
| dbg_node_utils = {{{ makeNodeImport('node:util', false) }}}; | ||
| } | ||
| #endif | ||
|
|
||
| // Override this function in a --pre-js file to get a signal for when | ||
| // compilation is ready. In that callback, call the function run() to start | ||
| // the program. | ||
|
|
@@ -179,13 +194,13 @@ if (!ENVIRONMENT_IS_PTHREAD) { | |
| // Wasm or Wasm2JS loading: | ||
|
|
||
| if (ENVIRONMENT_IS_NODE) { | ||
| var fs = require('node:fs'); | ||
| var fs = {{{ makeNodeImport('node:fs', false) }}}; | ||
| #if WASM == 2 | ||
| if (globalThis.WebAssembly) Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this code (which uses If not, this seems like maybe a separate fix that we could land in isolation. e.g.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The fix uses
I kept it in this PR because the changes are on the same lines as the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+''); | ||
| if (globalThis.WebAssembly) Module['wasm'] = fs.readFileSync({{{ makeNodeFilePath(TARGET_BASENAME + '.wasm') }}}); | ||
| else eval(fs.readFileSync({{{ makeNodeFilePath(TARGET_BASENAME + '.wasm.js') }}})+''); | ||
| #else | ||
| #if !WASM2JS | ||
| Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm'); | ||
| Module['wasm'] = fs.readFileSync({{{ makeNodeFilePath(TARGET_BASENAME + '.wasm') }}}); | ||
| #endif | ||
| #endif | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.