Skip to content

Commit 6b74d2a

Browse files
Bartek Wronaclaude
andcommitted
fixup! Replace require() with library symbols in EXPORT_ES6 library files
Move $nodeOs to libcore.js, use makeNodeImport, always use symbols via __deps Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3c0de69 commit 6b74d2a

File tree

8 files changed

+18
-64
lines changed

8 files changed

+18
-64
lines changed

src/lib/libatomic.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,12 @@ addToLibrary({
159159

160160
emscripten_has_threading_support: () => !!globalThis.SharedArrayBuffer,
161161

162-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE
162+
#if ENVIRONMENT_MAY_BE_NODE
163163
emscripten_num_logical_cores__deps: ['$nodeOs'],
164164
#endif
165165
emscripten_num_logical_cores: () =>
166166
#if ENVIRONMENT_MAY_BE_NODE
167-
#if EXPORT_ES6
168167
ENVIRONMENT_IS_NODE ? nodeOs.cpus().length :
169-
#else
170-
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
171-
#endif
172168
#endif
173169
navigator['hardwareConcurrency'],
174170
});

src/lib/libcore.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ addToLibrary({
343343
},
344344
#endif
345345

346-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE
347-
$nodeChildProcess: "ENVIRONMENT_IS_NODE ? await import('node:child_process') : undefined",
346+
#if ENVIRONMENT_MAY_BE_NODE
347+
$nodeOs: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:os') }}} : undefined",
348+
$nodeChildProcess: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:child_process') }}} : undefined",
348349
_emscripten_system__deps: ['$nodeChildProcess'],
349350
#endif
350351
_emscripten_system: (command) => {
@@ -355,11 +356,7 @@ addToLibrary({
355356
var cmdstr = UTF8ToString(command);
356357
if (!cmdstr.length) return 0; // this is what glibc seems to do (shell works test?)
357358

358-
#if EXPORT_ES6
359359
var cp = nodeChildProcess;
360-
#else
361-
var cp = require('node:child_process');
362-
#endif
363360
var ret = cp.spawnSync(cmdstr, [], {shell:true, stdio:'inherit'});
364361

365362
var _W_EXITCODE = (ret, sig) => ((ret) << 8 | (sig));

src/lib/libnode_imports.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/lib/libnodepath.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
// operations. Hence, using `nodePath` should be safe here.
1313

1414
addToLibrary({
15-
#if EXPORT_ES6
16-
$nodePath: "await import('node:path')",
17-
#else
18-
$nodePath: "require('node:path')",
19-
#endif
15+
$nodePath: "{{{ makeNodeImport('node:path') }}}",
2016
$PATH__deps: ['$nodePath'],
2117
$PATH: `{
2218
isAbs: nodePath.isAbsolute,

src/lib/libsockfs.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
*/
66

77
addToLibrary({
8-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE
9-
$nodeWs: "ENVIRONMENT_IS_NODE ? (await import('ws')).default : undefined",
8+
#if ENVIRONMENT_MAY_BE_NODE
9+
#if EXPORT_ES6
10+
$nodeWs: "ENVIRONMENT_IS_NODE ? ({{{ makeNodeImport('ws') }}}).default : undefined",
11+
#else
12+
$nodeWs: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('ws') }}} : undefined",
1013
#endif
11-
$SOCKFS__postset: () => {
12-
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
13-
},
14-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE
1514
$SOCKFS__deps: ['$FS', '$nodeWs'],
1615
#else
1716
$SOCKFS__deps: ['$FS'],
1817
#endif
18+
$SOCKFS__postset: () => {
19+
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
20+
},
1921
$SOCKFS: {
2022
#if expectToReceiveOnModule('websocket')
2123
websocketArgs: {},
@@ -223,11 +225,7 @@ addToLibrary({
223225
var WebSocketConstructor;
224226
#if ENVIRONMENT_MAY_BE_NODE
225227
if (ENVIRONMENT_IS_NODE) {
226-
#if EXPORT_ES6
227228
WebSocketConstructor = /** @type{(typeof WebSocket)} */(nodeWs);
228-
#else
229-
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
230-
#endif
231229
} else
232230
#endif // ENVIRONMENT_MAY_BE_NODE
233231
{
@@ -533,11 +531,7 @@ addToLibrary({
533531
if (sock.server) {
534532
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
535533
}
536-
#if EXPORT_ES6
537534
var WebSocketServer = nodeWs.Server;
538-
#else
539-
var WebSocketServer = require('ws').Server;
540-
#endif
541535
var host = sock.saddr;
542536
#if SOCKET_DEBUG
543537
dbg(`websocket: listen: ${host}:${sock.sport}`);

src/lib/libwasi.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,27 +569,22 @@ var WasiLibrary = {
569569

570570
// random.h
571571

572-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
573-
$nodeCrypto: "ENVIRONMENT_IS_NODE ? await import('node:crypto') : undefined",
572+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
573+
$nodeCrypto: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:crypto') }}} : undefined",
574574
#endif
575575

576-
#if ENVIRONMENT_MAY_BE_SHELL && EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
576+
#if ENVIRONMENT_MAY_BE_SHELL && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
577577
$initRandomFill__deps: ['$base64Decode', '$nodeCrypto'],
578578
#elif ENVIRONMENT_MAY_BE_SHELL
579579
$initRandomFill__deps: ['$base64Decode'],
580-
#elif EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
580+
#elif ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
581581
$initRandomFill__deps: ['$nodeCrypto'],
582582
#endif
583583
$initRandomFill: () => {
584584
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
585585
// This block is not needed on v19+ since crypto.getRandomValues is builtin
586586
if (ENVIRONMENT_IS_NODE) {
587-
#if EXPORT_ES6
588-
return (view) => nodeCrypto.randomFillSync(view);
589-
#else
590-
var nodeCrypto = require('node:crypto');
591587
return (view) => nodeCrypto.randomFillSync(view);
592-
#endif
593588
}
594589
#endif // ENVIRONMENT_MAY_BE_NODE
595590

src/lib/libwasm_worker.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,12 @@ if (ENVIRONMENT_IS_WASM_WORKER
289289
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
290290
},
291291

292-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_NODE
292+
#if ENVIRONMENT_MAY_BE_NODE
293293
emscripten_navigator_hardware_concurrency__deps: ['$nodeOs'],
294294
#endif
295295
emscripten_navigator_hardware_concurrency: () => {
296296
#if ENVIRONMENT_MAY_BE_NODE
297-
#if EXPORT_ES6
298297
if (ENVIRONMENT_IS_NODE) return nodeOs.cpus().length;
299-
#else
300-
if (ENVIRONMENT_IS_NODE) return require('node:os').cpus().length;
301-
#endif
302298
#endif
303299
return navigator['hardwareConcurrency'];
304300
},

src/modules.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ function calculateLibraries() {
166166
libraries.push('libatomic.js');
167167
}
168168

169-
if (EXPORT_ES6) {
170-
libraries.push('libnode_imports.js');
171-
}
172-
173169
if (MAX_WEBGL_VERSION >= 2) {
174170
// libwebgl2.js must be included only after libwebgl.js, so if we are
175171
// about to include libwebgl2.js, first squeeze in libwebgl.js.

0 commit comments

Comments
 (0)