Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP: continue to detangle ModuleJob & ESMLoader
  • Loading branch information
JakobJingleheimer committed Jun 15, 2023
commit 378c5a598a5592ca215658789cf782278fc16fe4
32 changes: 17 additions & 15 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,34 +132,35 @@ class DefaultModuleLoader {
return this.getJobFromResolveResult(resolveResult, parentURL, importAssertions);
}

getJobFromResolveResult(resolveResult, parentURL, importAssertions) {
getJobFromResolveResult(
resolveResult,
parentURL,
importAssertions = resolveResult.importAssertions,
) {
const { url, format } = resolveResult;
const resolvedImportAssertions = resolveResult.importAssertions ?? importAssertions;

let job = this.moduleMap.get(url, resolvedImportAssertions.type);
debug('getModuleJob', { url, format, parentURL, importAssertions, moduleMap: this.moduleMap });
let job = this.moduleMap.get(url, importAssertions.type);

// CommonJS will set functions for lazy job evaluation.
if (typeof job === 'function') {
this.moduleMap.set(url, undefined, job = job());
Comment thread
JakobJingleheimer marked this conversation as resolved.
}

job ??= this.#createModuleJob(url, resolvedImportAssertions, parentURL, format);
job ??= this.#createModuleJob(url, parentURL, importAssertions, format);

return job;
}

/**
* Create and cache an object representing a loaded module.
* @param {string} url The absolute URL that was resolved for this module
* @param {Record<string, string>} importAssertions Validations for the
* module import.
* @param {string} [parentURL] The absolute URL of the module importing this
* one, unless this is the Node.js entry point
* @param {string} [format] The format hint possibly returned by the
* `resolve` hook
* @param {string} [parentURL] The absolute URL of the module importing this one, unless this is
* the Node.js entry point
* @param {Record<string, string>} importAssertions Validations for the module import.
* @param {string} [format] The format hint possibly returned by the `resolve` hook
* @returns {Promise<ModuleJob>} The (possibly pending) module job
*/
async #createModuleJob(url, importAssertions, parentURL, format) {
async #createModuleJob(url, parentURL, importAssertions, format) {
const inspectBrk = (
parentURL === undefined &&
getOptionValue('--inspect-brk')
Expand All @@ -184,14 +185,15 @@ class DefaultModuleLoader {
throw new ERR_UNKNOWN_MODULE_FORMAT(finalFormat, responseURL);
}

const isMain = parentURL === undefined;
const moduleWrapper = FunctionPrototypeCall(translator, this, responseURL, source, isMain);

const ModuleJob = require('internal/modules/esm/module_job');
const job = new ModuleJob(
url,
moduleWrapper,
importAssertions,
parentURL === undefined,
isMain,
inspectBrk,
);

Expand All @@ -209,8 +211,8 @@ class DefaultModuleLoader {
* module import.
* @returns {Promise<ModuleExports>}
*/
async import(specifier, parentURL, importAssertions) {
const moduleJob = this.getModuleJob(specifier, parentURL, importAssertions);
async import(specifier, parentURL = undefined, importAssertions = { __proto__: null }) {
const moduleJob = await this.getModuleJob(specifier, parentURL, importAssertions);
const { module } = await moduleJob.run();
return module.getNamespace();
}
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ function runMainESM(mainPath) {

handleMainPromise(loadESM((esmLoader) => {
const main = path.isAbsolute(mainPath) ?
pathToFileurl("https://github.com/nodejs/node/pull/48439/commits/mainPath").href : mainPath;
return esmLoader.import(main, undefined, { __proto__: null });
pathToFileurl("https://github.com/nodejs/node/pull/48439/commits/mainPath").href :
mainPath;
return esmLoader.import(main);
}));
}

Expand Down