-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: Fix for #17130 CJS dependency shared with loader #17131
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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}`); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come reflect.exports.default.set(exports); ...like it is for others and the cached case above 👆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using _load calls around a bunch and eventually ends up here Lines 577 to 581 in 317f596
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @devsnek Ah, any way that could be tidied up so that all the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this is already as tidy as it will get, but i think a comment above line 49 would go a long way towards explaining what is going on. personally i had no idea how line 49 worked until when you asked because i decided it was time to figure it out. maybe something like |
||||||||||||
const CJSModule = require('module'); | ||||||||||||
const pathname = internalURLModule.getPathFromURL(new URL(url)); | ||||||||||||
CJSModule._load(pathname); | ||||||||||||
}); | ||||||||||||
}); | ||||||||||||
|
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'); |
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've also hoisted up this require by fixing the circular reference in module.js.