From 1f5ee33dcb74202a0f29ed1ee2e941b36138f9b0 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/17131 Reviewed-By: Michaƫl Zasso Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Timothy Gu Reviewed-By: Bradley Farias --- lib/internal/loader/ModuleRequest.js | 13 +++++++++++-- lib/module.js | 4 +++- test/es-module/test-esm-shared-loader-dep.mjs | 7 +++++++ .../es-module-loaders/loader-shared-dep.mjs | 7 +++++++ 4 files changed, 28 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..93baf2a597fc1a 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'); @@ -35,11 +36,19 @@ loaders.set('esm', async (url) => { }); // Strategy for loading a node-style CommonJS module +const isWindows = process.platform === 'win32'; +const winSepRegEx = /\//g; loaders.set('cjs', async (url) => { + const pathname = internalURLModule.getPathFromURL(new URL(url)); + const module = CJSModule._cache[ + isWindows ? pathname.replace(winSepRegEx, '\\') : 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 a2d772a381e534..b22ba4fe66337a 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; const builtinModules = Object.keys(NativeModule._source) .filter(NativeModule.nonInternalExists); 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); +}