diff options
Diffstat (limited to 'lib/internal/modules/esm')
-rw-r--r-- | lib/internal/modules/esm/loader.js | 9 | ||||
-rw-r--r-- | lib/internal/modules/esm/module_job.js | 6 | ||||
-rw-r--r-- | lib/internal/modules/esm/translators.js | 42 |
3 files changed, 24 insertions, 33 deletions
diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 9800e8a550..138cf8b5ec 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -117,12 +117,7 @@ class Loader { source, url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href ) { - const evalInstance = async (url) => { - return { - module: new ModuleWrap(source, url), - reflect: undefined - }; - }; + const evalInstance = (url) => new ModuleWrap(source, url); const job = new ModuleJob(this, url, evalInstance, false); this.moduleMap.set(url, job); const { module, result } = await job.run(); @@ -165,7 +160,7 @@ class Loader { return createDynamicModule([], exports, url, (reflect) => { debug(`Loading dynamic ${url}`); execute(reflect.exports); - }); + }).module; }; } else { if (!translators.has(format)) diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 6f265ed460..ef11e2ec83 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -30,19 +30,17 @@ class ModuleJob { // onto `this` by `link()` below once it has been resolved. this.modulePromise = moduleProvider.call(loader, url, isMain); this.module = undefined; - this.reflect = undefined; // Wait for the ModuleWrap instance being linked with all dependencies. const link = async () => { - ({ module: this.module, - reflect: this.reflect } = await this.modulePromise); + this.module = await this.modulePromise; assert(this.module instanceof ModuleWrap); const dependencyJobs = []; const promises = this.module.link(async (specifier) => { const jobPromise = this.loader.getModuleJob(specifier, url); dependencyJobs.push(jobPromise); - return (await (await jobPromise).modulePromise).module; + return (await jobPromise).modulePromise; }); if (promises !== undefined) diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index e8eddcfd21..b4d41685ed 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -32,6 +32,8 @@ const { const readFileAsync = promisify(fs.readFile); const JsonParse = JSON.parse; const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); +const moduleWrap = internalBinding('module_wrap'); +const { ModuleWrap } = moduleWrap; const debug = debuglog('esm'); @@ -77,22 +79,18 @@ translators.set('module', async function moduleStrategy(url) { const source = `${await getSource(url)}`; maybeCacheSourceMap(url, source); debug(`Translating StandardModule ${url}`); - const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); const module = new ModuleWrap(source, url); - callbackMap.set(module, { + moduleWrap.callbackMap.set(module, { initializeImportMeta, importModuleDynamically, }); - return { - module, - reflect: undefined, - }; + return module; }); // Strategy for loading a node-style CommonJS module const isWindows = process.platform === 'win32'; const winSepRegEx = /\//g; -translators.set('commonjs', async function commonjsStrategy(url, isMain) { +translators.set('commonjs', function commonjsStrategy(url, isMain) { debug(`Translating CJSModule ${url}`); const pathname = internalURLModule.fileURLToPath(new URL(url)); const cached = this.cjsCache.get(url); @@ -105,17 +103,17 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) { ]; if (module && module.loaded) { const exports = module.exports; - return createDynamicModule([], ['default'], url, (reflect) => { - reflect.exports.default.set(exports); - }); + return new ModuleWrap(function() { + this.setExport('default', exports); + }, ['default'], url); } - return createDynamicModule([], ['default'], url, () => { + return new ModuleWrap(function() { debug(`Loading CJSModule ${url}`); // We don't care about the return val of _load here because Module#load // will handle it for us by checking the loader registry and filling the // exports like above CJSModule._load(pathname, undefined, isMain); - }); + }, ['default'], url); }); // Strategy for loading a node builtin CommonJS module that isn't @@ -145,9 +143,9 @@ translators.set('json', async function jsonStrategy(url) { module = CJSModule._cache[modulePath]; if (module && module.loaded) { const exports = module.exports; - return createDynamicModule([], ['default'], url, (reflect) => { - reflect.exports.default.set(exports); - }); + return new ModuleWrap(function() { + this.setExport('default', exports); + }, ['default'], url); } } const content = `${await getSource(url)}`; @@ -158,9 +156,9 @@ translators.set('json', async function jsonStrategy(url) { module = CJSModule._cache[modulePath]; if (module && module.loaded) { const exports = module.exports; - return createDynamicModule(['default'], url, (reflect) => { - reflect.exports.default.set(exports); - }); + return new ModuleWrap(function() { + this.setExport('default', exports); + }, ['default'], url); } } try { @@ -180,10 +178,10 @@ translators.set('json', async function jsonStrategy(url) { if (pathname) { CJSModule._cache[modulePath] = module; } - return createDynamicModule([], ['default'], url, (reflect) => { + return new ModuleWrap(function() { debug(`Parsing JSONModule ${url}`); - reflect.exports.default.set(module.exports); - }); + this.setExport('default', module.exports); + }, ['default'], url); }); // Strategy for loading a wasm module @@ -206,5 +204,5 @@ translators.set('wasm', async function(url) { const { exports } = new WebAssembly.Instance(compiled, reflect.imports); for (const expt of Object.keys(exports)) reflect.exports[expt].set(exports[expt]); - }); + }).module; }); |