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 <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
  • Loading branch information
guybedford authored and MylesBorins committed Dec 12, 2017
1 parent 6624ac3 commit 5736dc4
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;

const builtinModules = Object.keys(NativeModule._source)
.filter(NativeModule.nonInternalExists);
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 5736dc4

Please sign in to comment.