Skip to content

Commit eb5e113

Browse files
committed
Avoid importing Node's modules on demand
Remove `requireNodeFS()` in favor of always importing these Node modules. These `require()`'s will always be used on Node.js, except when linking with `-sSINGLE_FILE`. But in that case, `stdin` usage, or using the `-sMAIN_MODULE` and/or `-sNODERAWFS` options would still depend on these imports.
1 parent 84ad56f commit eb5e113

File tree

4 files changed

+4
-18
lines changed

4 files changed

+4
-18
lines changed

src/library_nodefs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
mergeInto(LibraryManager.library, {
88
$NODEFS__deps: ['$FS', '$PATH', '$ERRNO_CODES', '$mmapAlloc'],
9-
$NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { requireNodeFS(); NODEFS.staticInit(); }',
9+
$NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { NODEFS.staticInit(); }',
1010
$NODEFS: {
1111
isWindows: false,
1212
staticInit: () => {

src/node_shell_read.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
var fs;
8-
var nodePath;
9-
10-
var requireNodeFS = () => {
11-
// Use nodePath as the indicator for these not being initialized,
12-
// since in some environments a global fs may have already been
13-
// created.
14-
if (!nodePath) {
15-
fs = require('fs');
16-
nodePath = require('path');
17-
}
18-
};
7+
// These modules will usually be used on Node.js.
8+
var fs = require('fs');
9+
var nodePath = require('path');
1910

2011
read_ = (filename, binary) => {
2112
#if SUPPORT_BASE64_EMBEDDING
@@ -24,7 +15,6 @@ read_ = (filename, binary) => {
2415
return binary ? ret : ret.toString();
2516
}
2617
#endif
27-
requireNodeFS();
2818
filename = nodePath['normalize'](filename);
2919
return fs.readFileSync(filename, binary ? undefined : 'utf8');
3020
};
@@ -47,7 +37,6 @@ readAsync = (filename, onload, onerror) => {
4737
onload(ret);
4838
}
4939
#endif
50-
requireNodeFS();
5140
filename = nodePath['normalize'](filename);
5241
fs.readFile(filename, function(err, data) {
5342
if (err) onerror(err);

src/preamble.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,6 @@ function instantiateSync(file, info) {
771771
// to load ok, but we do actually recompile the binary every time).
772772
var cachedCodeFile = '{{{ WASM_BINARY_FILE }}}.' + v8.cachedDataVersionTag() + '.cached';
773773
cachedCodeFile = locateFile(cachedCodeFile);
774-
requireNodeFS();
775774
var hasCached = fs.existsSync(cachedCodeFile);
776775
if (hasCached) {
777776
#if RUNTIME_LOGGING

src/shell.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ if (ENVIRONMENT_IS_NODE) {
258258
#if WASM == 2
259259
// If target shell does not support Wasm, load the JS version of the code.
260260
if (typeof WebAssembly == 'undefined') {
261-
requireNodeFS();
262261
eval(fs.readFileSync(locateFile('{{{ TARGET_BASENAME }}}.wasm.js'))+'');
263262
}
264263
#endif
@@ -423,7 +422,6 @@ if (ENVIRONMENT_IS_NODE) {
423422
var defaultPrint = console.log.bind(console);
424423
var defaultPrintErr = console.warn.bind(console);
425424
if (ENVIRONMENT_IS_NODE) {
426-
requireNodeFS();
427425
defaultPrint = (str) => fs.writeSync(1, str + '\n');
428426
defaultPrintErr = (str) => fs.writeSync(2, str + '\n');
429427
}

0 commit comments

Comments
 (0)