Skip to content

Commit 687def8

Browse files
esm,loaders: tidy ESMLoader internals
1 parent 572d556 commit 687def8

1 file changed

Lines changed: 57 additions & 59 deletions

File tree

lib/internal/modules/esm/loader.js

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -179,39 +179,37 @@ function nextHookFactory(chain, meta, { validateArgs, validateOutput }) {
179179
* the main module and everything in its dependency graph.
180180
*/
181181
class ESMLoader {
182-
/**
183-
* Prior to ESM loading. These are called once before any modules are started.
184-
* @private
185-
* @property {KeyedHook[]} globalPreloaders Last-in-first-out
186-
* list of preload hooks.
187-
*/
188-
#globalPreloaders = [];
189-
190-
/**
191-
* Phase 2 of 2 in ESM loading.
192-
* @private
193-
* @property {KeyedHook[]} loaders Last-in-first-out
194-
* collection of loader hooks.
195-
*/
196-
#loaders = [
197-
{
198-
fn: defaultLoad,
199-
url: 'node:internal/modules/esm/load',
200-
},
201-
];
202-
203-
/**
204-
* Phase 1 of 2 in ESM loading.
205-
* @private
206-
* @property {KeyedHook[]} resolvers Last-in-first-out
207-
* collection of resolver hooks.
208-
*/
209-
#resolvers = [
210-
{
211-
fn: defaultResolve,
212-
url: 'node:internal/modules/esm/resolve',
213-
},
214-
];
182+
#hooks = {
183+
__proto__: null,
184+
/**
185+
* Prior to ESM loading. These are called once before any modules are started.
186+
* @private
187+
* @property {KeyedHook[]} globalPreload Last-in-first-out list of preload hooks.
188+
*/
189+
globalPreload: [],
190+
/**
191+
* Phase 2 of 2 in ESM loading.
192+
* @private
193+
* @property {KeyedHook[]} load Last-in-first-out collection of loader hooks.
194+
*/
195+
load: [
196+
{
197+
fn: defaultLoad,
198+
url: 'node:internal/modules/esm/load',
199+
},
200+
],
201+
/**
202+
* Phase 1 of 2 in ESM loading.
203+
* @private
204+
* @property {KeyedHook[]} resolve Last-in-first-out collection of resolve hooks.
205+
*/
206+
resolve: [
207+
{
208+
fn: defaultResolve,
209+
url: 'node:internal/modules/esm/resolve',
210+
},
211+
],
212+
};
215213

216214
#importMetaInitializer = initializeImportMeta;
217215

@@ -305,13 +303,13 @@ class ESMLoader {
305303
);
306304

307305
if (globalPreload) {
308-
acceptedHooks.globalPreloader = globalPreload;
306+
acceptedHooks.globalPreload = globalPreload;
309307
}
310308
if (resolve) {
311-
acceptedHooks.resolver = resolve;
309+
acceptedHooks.resolve = resolve;
312310
}
313311
if (load) {
314-
acceptedHooks.loader = load;
312+
acceptedHooks.load = load;
315313
}
316314

317315
return acceptedHooks;
@@ -333,34 +331,34 @@ class ESMLoader {
333331
url,
334332
} = customLoaders[i];
335333
const {
336-
globalPreloader,
337-
resolver,
338-
loader,
334+
globalPreload,
335+
resolve,
336+
load,
339337
} = ESMLoader.pluckHooks(exports);
340338

341-
if (globalPreloader) {
339+
if (globalPreload) {
342340
ArrayPrototypePush(
343-
this.#globalPreloaders,
341+
this.#hooks.globalPreload,
344342
{
345-
fn: globalPreloader,
343+
fn: globalPreload,
346344
url,
347345
},
348346
);
349347
}
350-
if (resolver) {
348+
if (resolve) {
351349
ArrayPrototypePush(
352-
this.#resolvers,
350+
this.#hooks.resolve,
353351
{
354-
fn: resolver,
352+
fn: resolve,
355353
url,
356354
},
357355
);
358356
}
359-
if (loader) {
357+
if (load) {
360358
ArrayPrototypePush(
361-
this.#loaders,
359+
this.#hooks.load,
362360
{
363-
fn: loader,
361+
fn: load,
364362
url,
365363
},
366364
);
@@ -411,8 +409,8 @@ class ESMLoader {
411409
async getModuleJob(specifier, parentURL, importAssertions) {
412410
let importAssertionsForResolve;
413411

414-
// By default, `this.#loaders` contains just the Node default load hook
415-
if (this.#loaders.length !== 1) {
412+
// By default, `this.#hooks.load` contains just the Node default load hook
413+
if (this.#hooks.load.length !== 1) {
416414
// We can skip cloning if there are no user-provided loaders because
417415
// the Node.js default resolve hook does not use import assertions.
418416
importAssertionsForResolve = ObjectAssign(
@@ -555,7 +553,7 @@ class ESMLoader {
555553
* @returns {{ format: ModuleFormat, source: ModuleSource }}
556554
*/
557555
async load(url, context = {}) {
558-
const chain = this.#loaders;
556+
const chain = this.#hooks.load;
559557
const meta = {
560558
chainFinished: null,
561559
context,
@@ -684,7 +682,7 @@ class ESMLoader {
684682
}
685683

686684
preload() {
687-
for (let i = this.#globalPreloaders.length - 1; i >= 0; i--) {
685+
for (let i = this.#hooks.globalPreload.length - 1; i >= 0; i--) {
688686
const channel = new MessageChannel();
689687
const {
690688
port1: insidePreload,
@@ -695,19 +693,19 @@ class ESMLoader {
695693
insideLoader.unref();
696694

697695
const {
698-
fn: preloader,
696+
fn: preload,
699697
url: specifier,
700-
} = this.#globalPreloaders[i];
698+
} = this.#hooks.globalPreload[i];
701699

702-
const preload = preloader({
700+
const preloaded = preload({
703701
port: insideLoader,
704702
});
705703

706-
if (preload == null) { return; }
704+
if (preloaded == null) { return; }
707705

708706
const hookErrIdentifier = `${specifier} globalPreload`;
709707

710-
if (typeof preload !== 'string') { // [2]
708+
if (typeof preloaded !== 'string') { // [2]
711709
throw new ERR_INVALID_RETURN_VALUE(
712710
'a string',
713711
hookErrIdentifier,
@@ -716,7 +714,7 @@ class ESMLoader {
716714
}
717715
const { compileFunction } = require('vm');
718716
const preloadInit = compileFunction(
719-
preload,
717+
preloaded,
720718
['getBuiltin', 'port', 'setImportMetaCallback'],
721719
{
722720
filename: '<preload>',
@@ -804,7 +802,7 @@ class ESMLoader {
804802
parentURL,
805803
);
806804
}
807-
const chain = this.#resolvers;
805+
const chain = this.#hooks.resolve;
808806
const context = {
809807
conditions: DEFAULT_CONDITIONS,
810808
importAssertions,

0 commit comments

Comments
 (0)