-
Notifications
You must be signed in to change notification settings - Fork 30k
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: remove dynamicInstantiate loader hook #33501
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// Flags: --experimental-loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs | ||
import '../common/index.mjs'; | ||
import { readFile } from 'fs'; | ||
import { readFile, __fromLoader } from 'fs'; | ||
import assert from 'assert'; | ||
import ok from '../fixtures/es-modules/test-esm-ok.mjs'; | ||
|
||
assert(ok); | ||
assert(readFile); | ||
assert(__fromLoader); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,62 @@ | ||
import module from 'module'; | ||
|
||
export function getFormat(url, context, defaultGetFormat) { | ||
if (module.builtinModules.includes(url)) { | ||
const GET_BUILTIN = `$__get_builtin_hole_${Date.now()}`; | ||
|
||
export function getGlobalPreloadCode() { | ||
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. The only reason for needing this is because this hook is messing with the builtin 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. If we are going to push users into this hook now, then I think we should aim to work on this usability first - you shouldn't need to define a secret global for the common use case... |
||
return `Object.defineProperty(globalThis, ${JSON.stringify(GET_BUILTIN)}, { | ||
value: (builtinName) => { | ||
return getBuiltin(builtinName); | ||
}, | ||
enumerable: false, | ||
configurable: false, | ||
}); | ||
`; | ||
} | ||
|
||
export function resolve(specifier, context, defaultResolve) { | ||
const def = defaultResolve(specifier, context); | ||
if (def.url.startsWith('nodejs:')) { | ||
return { | ||
url: `custom-${def.url}`, | ||
}; | ||
} | ||
return def; | ||
} | ||
|
||
export function getSource(url, context, defaultGetSource) { | ||
if (url.startsWith('custom-nodejs:')) { | ||
const urlObj = new URL(url); | ||
return { | ||
format: 'dynamic' | ||
source: generateBuiltinModule(urlObj.pathname), | ||
format: 'module', | ||
}; | ||
} | ||
return defaultGetSource(url, context); | ||
} | ||
|
||
export function getFormat(url, context, defaultGetFormat) { | ||
if (url.startsWith('custom-nodejs:')) { | ||
return { format: 'module' }; | ||
} | ||
return defaultGetFormat(url, context, defaultGetFormat); | ||
} | ||
|
||
export function dynamicInstantiate(url) { | ||
const builtinInstance = module._load(url); | ||
const builtinExports = ['default', ...Object.keys(builtinInstance)]; | ||
return { | ||
exports: builtinExports, | ||
execute: exports => { | ||
for (let name of builtinExports) | ||
exports[name].set(builtinInstance[name]); | ||
exports.default.set(builtinInstance); | ||
} | ||
}; | ||
function generateBuiltinModule(builtinName) { | ||
const builtinInstance = module._load(builtinName); | ||
const builtinExports = [ | ||
...Object.keys(builtinInstance), | ||
]; | ||
return `\ | ||
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. this seems horrible enough to constitute figuring out some sort of replacement for dynamicInstantiate 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. It's the hackiest version of generating the code. I don't agree that the loader hooks providing proper JS code is horrible in general. It's possible to create a |
||
const $builtinInstance = ${GET_BUILTIN}(${JSON.stringify(builtinName)}); | ||
|
||
export const __fromLoader = true; | ||
|
||
export default $builtinInstance; | ||
|
||
${ | ||
builtinExports | ||
.map(name => `export const ${name} = $builtinInstance.${name};`) | ||
.join('\n') | ||
} | ||
`; | ||
} |
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.
this isn't needed
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.
Yeah, I started without it. But then the test still passed and I was super confused if anything happened. Having the warning did help me be confident that it actually ignored the hook.
That being said - happy to remove this last trace as well.