Skip to content

Commit

Permalink
module: fix for #17130 shared loader cjs dep
Browse files Browse the repository at this point in the history
PR-URL: #17131
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Bradley Farias <[email protected]>
  • Loading branch information
guybedford authored and gibfahn committed Dec 20, 2017
1 parent 3fb2183 commit c3fdef7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/internal/loader/ModuleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
});
});
Expand Down
4 changes: 3 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions test/es-module/test-esm-shared-loader-dep.mjs
Original file line number Diff line number Diff line change
@@ -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');
7 changes: 7 additions & 0 deletions test/fixtures/es-module-loaders/loader-shared-dep.mjs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c3fdef7

Please sign in to comment.