-
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
93ed310
eb718f5
b46b689
8abdd46
f673388
0a83a98
a7717aa
145c961
581bbaa
2cd89c3
d68b648
1236377
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 |
|---|---|---|
|
|
@@ -5,10 +5,18 @@ | |
| */ | ||
|
|
||
| addToLibrary({ | ||
| #if ENVIRONMENT_MAY_BE_NODE && EXPORT_ES6 | ||
| // In ESM mode, require() is not natively available. When SOCKFS is used, | ||
| // we need require() to lazily load the 'ws' npm package for WebSocket | ||
| // support on Node.js. Set up a createRequire-based polyfill. | ||
|
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. Why do we still need |
||
| $nodeRequire: `ENVIRONMENT_IS_NODE ? (await import('node:module')).createRequire(import.meta.url) : undefined`, | ||
| $SOCKFS__deps: ['$FS', '$nodeRequire'], | ||
| #else | ||
| $SOCKFS__deps: ['$FS'], | ||
| #endif | ||
| $SOCKFS__postset: () => { | ||
| addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);'); | ||
| }, | ||
| $SOCKFS__deps: ['$FS'], | ||
| $SOCKFS: { | ||
| #if expectToReceiveOnModule('websocket') | ||
| websocketArgs: {}, | ||
|
|
@@ -216,7 +224,11 @@ addToLibrary({ | |
| var WebSocketConstructor; | ||
| #if ENVIRONMENT_MAY_BE_NODE | ||
| if (ENVIRONMENT_IS_NODE) { | ||
| #if EXPORT_ES6 | ||
| WebSocketConstructor = /** @type{(typeof WebSocket)} */(nodeRequire('ws')); | ||
| #else | ||
| WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws')); | ||
| #endif | ||
| } else | ||
| #endif // ENVIRONMENT_MAY_BE_NODE | ||
| { | ||
|
|
@@ -522,7 +534,11 @@ addToLibrary({ | |
| if (sock.server) { | ||
| throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening | ||
| } | ||
| #if EXPORT_ES6 | ||
| var WebSocketServer = nodeRequire('ws').Server; | ||
| #else | ||
| var WebSocketServer = require('ws').Server; | ||
| #endif | ||
| var host = sock.saddr; | ||
| #if SOCKET_DEBUG | ||
| dbg(`websocket: listen: ${host}:${sock.sport}`); | ||
|
|
||
| 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) }}}; | ||
| global.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.
Should we just call this
child_process? It seems unlikely enough to collide and is more in common with how most node porgrams name this (I assume)?