-
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
86d219c
c85c69e
6d7219d
ba0aa88
56b9a6d
19d4d53
cb5bec6
79183e0
d6d7268
7474309
5dd4a01
6221db1
23d0a46
80e0bd5
44f7423
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 |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -182,13 +197,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 | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // Polyfill require() for ESM mode so that EM_ASM/EM_JS code using | ||
| // require('fs'), require('path'), etc. works in both CJS and ESM. | ||
| // createRequire is available since Node 12.2.0. | ||
| if (typeof require === 'undefined') { | ||
| var { createRequire } = await import('module'); | ||
| var require = createRequire(import.meta.url); | ||
| } |
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.
Why do we still need
createRequirein this case?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.
sync context requires createRequire, can't use await import()