Skip to content

Commit 34d4bcb

Browse files
committed
rename the function init, run it from main/ files
1 parent 7046a66 commit 34d4bcb

File tree

13 files changed

+77
-53
lines changed

13 files changed

+77
-53
lines changed

lib/internal/main/check_syntax.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function loadESMIfNeeded(cb) {
5252
const hasModulePreImport = getOptionValue('--import').length > 0;
5353

5454
if (hasModulePreImport) {
55-
const { loadESM } = require('internal/process/esm_loader');
55+
const { loadESM, init } = require('internal/process/esm_loader');
56+
init();
5657
loadESM(cb);
5758
return;
5859
}

lib/internal/main/eval_stdin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ const {
1818
prepareMainThreadExecution();
1919
markBootstrapComplete();
2020

21+
2122
readStdin((code) => {
2223
// This is necessary for fork() and CJS module compilation.
2324
// TODO(joyeecheung): pass this with something really internal.
2425
process._eval = code;
2526

2627
const print = getOptionValue('--print');
2728
const loadESM = getOptionValue('--import').length > 0;
28-
if (getOptionValue('--input-type') === 'module')
29+
if (getOptionValue('--input-type') === 'module') {
30+
require('internal/process/esm_loader').init();
2931
evalModule(code, print);
30-
else
32+
} else {
3133
evalScript('[stdin]',
3234
code,
3335
getOptionValue('--inspect-brk'),
3436
print,
3537
loadESM);
38+
require('internal/process/esm_loader').initIfNeeded();
39+
}
3640
});

lib/internal/main/eval_string.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ markBootstrapComplete();
2525
const source = getOptionValue('--eval');
2626
const print = getOptionValue('--print');
2727
const loadESM = getOptionValue('--import').length > 0;
28-
if (getOptionValue('--input-type') === 'module')
28+
const esmLoader = require('internal/process/esm_loader');
29+
if (getOptionValue('--input-type') === 'module') {
30+
esmLoader.init();
2931
evalModule(source, print);
30-
else {
32+
} else {
3133
// For backward compatibility, we want the identifier crypto to be the
3234
// `node:crypto` module rather than WebCrypto.
3335
const isUsingCryptoIdentifier =
@@ -54,4 +56,5 @@ else {
5456
getOptionValue('--inspect-brk'),
5557
print,
5658
loadESM);
59+
esmLoader.initIfNeeded();
5760
}

lib/internal/main/inspect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ prepareMainThreadExecution();
1111

1212

1313
markBootstrapComplete();
14+
require('internal/process/esm_loader').init();
1415

1516
// Start the debugger agent.
1617
process.nextTick(() => {

lib/internal/main/repl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) {
3636
}
3737

3838
const esmLoader = require('internal/process/esm_loader');
39+
esmLoader.init();
3940
esmLoader.loadESM(() => {
4041
console.log(`Welcome to Node.js ${process.version}.\n` +
4142
'Type ".help" for more information.');

lib/internal/main/run_main_module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ prepareMainThreadExecution(true);
1111

1212
markBootstrapComplete();
1313

14+
require('internal/process/esm_loader').init();
15+
1416
// Necessary to reset RegExp statics before user code runs.
1517
RegExpPrototypeExec(/^/, '');
1618

lib/internal/main/test_runner.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ if (isUsingInspector()) {
2222
inspectPort = process.debugPort;
2323
}
2424

25+
require('internal/process/esm_loader').init();
26+
2527
run({ concurrency, inspectPort, watch: getOptionValue('--watch'), setup: setupTestReporters })
2628
.once('test:fail', () => {
2729
process.exitCode = kGenericUserError;

lib/internal/main/worker_thread.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ port.on('message', (message) => {
162162
});
163163
ArrayPrototypeSplice(process.argv, 1, 0, name);
164164
evalScript(name, filename);
165+
require('internal/process/esm_loader').initIfNeeded();
165166
break;
166167
}
167168

168169
case 'module': {
169170
const { evalModule } = require('internal/process/execution');
171+
require('internal/process/esm_loader').init();
170172
PromisePrototypeThen(evalModule(filename), undefined, (e) => {
171173
workerOnGlobalUncaughtException(e, true);
172174
});
@@ -179,6 +181,7 @@ port.on('message', (message) => {
179181
// XXX: the monkey-patchability here should probably be deprecated.
180182
ArrayPrototypeSplice(process.argv, 1, 0, filename);
181183
const CJSLoader = require('internal/modules/cjs/loader');
184+
require('internal/process/esm_loader').initIfNeeded();
182185
CJSLoader.Module.runMain(filename);
183186
break;
184187
}

lib/internal/modules/esm/utils.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
const {
44
ArrayIsArray,
5-
ArrayPrototypePush,
65
PromisePrototypeThen,
7-
ReflectApply,
8-
SafeMap,
96
SafeSet,
107
SafeWeakMap,
118
ObjectFreeze,
@@ -17,7 +14,7 @@ const {
1714
} = require('internal/errors').codes;
1815
const { getOptionValue } = require('internal/options');
1916
const { pathToFileURL } = require('internal/url');
20-
const { kEmptyObject, createDeferredPromise } = require('internal/util');
17+
const { kEmptyObject } = require('internal/util');
2118
const {
2219
setImportModuleDynamicallyCallback,
2320
setInitializeImportMetaObjectCallback,
@@ -95,14 +92,6 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
9592
}
9693

9794
function initializeESM() {
98-
const { setESMLoader } = require('internal/process/esm_loader');
99-
// Minimal mock for letting `--require` work before we have any actual ESM loader.
100-
setESMLoader({ __proto__: null, cjsCache: new SafeMap(), import() {
101-
const { promise, resolve, reject } = createDeferredPromise();
102-
ArrayPrototypePush(this.importRequests, { arguments: arguments, resolve, reject });
103-
return promise;
104-
}, importRequests: [] });
105-
10695
initializeDefaultConditions();
10796
// Setup per-isolate callbacks that locate data or callbacks that we keep
10897
// track of for different ESM modules.
@@ -126,22 +115,10 @@ async function initializeHooks() {
126115
const hooks = new Hooks();
127116

128117
const { DefaultModuleLoader } = require('internal/modules/esm/loader');
129-
const { esmLoader, setESMLoader } = require('internal/process/esm_loader');
118+
const { init } = require('internal/process/esm_loader');
130119
class ModuleLoader extends DefaultModuleLoader {
131-
constructor() {
132-
super();
133-
for (const { 0: key, 1: value } of esmLoader.cjsCache) {
134-
// Getting back the values from the mocked loader.
135-
this.cjsCache.set(key, value);
136-
}
137-
for (let i = 0; i < esmLoader.importRequests.length; i++) {
138-
PromisePrototypeThen(
139-
ReflectApply(this.import, this, esmLoader.importRequests[i].arguments),
140-
esmLoader.importRequests[i].resolve,
141-
esmLoader.importRequests[i].reject,
142-
);
143-
}
144-
}
120+
// eslint-disable-next-line no-useless-constructor
121+
constructor() { super(); }
145122

146123
loaderType = 'internal';
147124
async #getModuleJob(specifier, parentURL, importAssertions) {
@@ -166,8 +143,6 @@ async function initializeHooks() {
166143
}
167144
const privateModuleLoader = new ModuleLoader();
168145

169-
setESMLoader(privateModuleLoader);
170-
171146
const parentURL = pathToFileURL(cwd).href;
172147

173148
for (let i = 0; i < customLoaderPaths.length; i++) {
@@ -185,6 +160,8 @@ async function initializeHooks() {
185160

186161
const preloadScripts = hooks.initializeGlobalPreload();
187162

163+
init(privateModuleLoader);
164+
188165
return { __proto__: null, hooks, preloadScripts };
189166
}
190167

lib/internal/modules/run_main.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ function shouldUseESMLoader(mainPath) {
4747
}
4848

4949
function runMainESM(mainPath) {
50-
const { loadESM, setESMLoader } = require('internal/process/esm_loader');
51-
const { createModuleLoader } = require('internal/modules/esm/loader');
50+
const { loadESM } = require('internal/process/esm_loader');
5251
const { pathToFileURL } = require('internal/url');
5352

54-
setESMLoader(createModuleLoader(true));
55-
5653
handleMainPromise(loadESM((esmLoader) => {
5754
const main = path.isAbsolute(mainPath) ?
5855
pathToFileURL(mainPath).href : mainPath;

0 commit comments

Comments
 (0)