From 10a065032debfb240d473dae27c15cc85f8491eb Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 22 Nov 2017 12:01:11 +0200 Subject: [PATCH] module: fix for #17130 shared loader cjs dep --- lib/internal/loader/ModuleRequest.js | 10 ++++++++-- lib/module.js | 4 +++- test/es-module/test-esm-shared-loader-dep.mjs | 7 +++++++ test/fixtures/es-module-loaders/loader-shared-dep.mjs | 7 +++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/es-module/test-esm-shared-loader-dep.mjs create mode 100644 test/fixtures/es-module-loaders/loader-shared-dep.mjs diff --git a/lib/internal/loader/ModuleRequest.js b/lib/internal/loader/ModuleRequest.js index 72f3dd3ee570c2..13606568c3309d 100644 --- a/lib/internal/loader/ModuleRequest.js +++ b/lib/internal/loader/ModuleRequest.js @@ -2,6 +2,7 @@ const fs = require('fs'); const internalCJSModule = require('internal/module'); +const CJSModule = require('module'); const internalURLModule = require('internal/url'); const internalFS = require('internal/fs'); const NativeModule = require('native_module'); @@ -36,10 +37,15 @@ loaders.set('esm', async (url) => { // Strategy for loading a node-style CommonJS module loaders.set('cjs', async (url) => { + const pathname = internalURLModule.getPathFromURL(new URL(url)); + const module = CJSModule._cache[pathname]; + if (module && module.loaded) { + const ctx = createDynamicModule(['default'], url, undefined); + ctx.reflect.exports.default.set(module.exports); + return ctx; + } return createDynamicModule(['default'], url, (reflect) => { debug(`Loading CJSModule ${url}`); - const CJSModule = require('module'); - const pathname = internalURLModule.getPathFromURL(new URL(url)); CJSModule._load(pathname); }); }); diff --git a/lib/module.js b/lib/module.js index 77e5e3fef66f29..f23d36a91afcf2 100644 --- a/lib/module.js +++ b/lib/module.js @@ -39,6 +39,9 @@ const experimentalModules = !!process.binding('config').experimentalModules; const errors = require('internal/errors'); +module.exports = Module; + +// these are below module.exports for the circular reference const Loader = require('internal/loader/Loader'); const ModuleJob = require('internal/loader/ModuleJob'); const { createDynamicModule } = require('internal/loader/ModuleWrap'); @@ -72,7 +75,6 @@ function Module(id, parent) { this.loaded = false; this.children = []; } -module.exports = Module; Module._cache = Object.create(null); Module._pathCache = Object.create(null); diff --git a/test/es-module/test-esm-shared-loader-dep.mjs b/test/es-module/test-esm-shared-loader-dep.mjs new file mode 100644 index 00000000000000..970bfd7121a721 --- /dev/null +++ b/test/es-module/test-esm-shared-loader-dep.mjs @@ -0,0 +1,7 @@ +// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-shared-dep.mjs +/* eslint-disable required-modules */ +import assert from 'assert'; +import './test-esm-ok.mjs'; +import dep from '../fixtures/es-module-loaders/loader-dep.js'; + +assert.strictEqual(dep.format, 'esm'); diff --git a/test/fixtures/es-module-loaders/loader-shared-dep.mjs b/test/fixtures/es-module-loaders/loader-shared-dep.mjs new file mode 100644 index 00000000000000..e2a1cbd75d2535 --- /dev/null +++ b/test/fixtures/es-module-loaders/loader-shared-dep.mjs @@ -0,0 +1,7 @@ +import dep from './loader-dep.js'; +import assert from 'assert'; + +export function resolve(specifier, base, defaultResolve) { + assert.equal(dep.format, 'esm'); + return defaultResolve(specifier, base); +}