Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
vm: add dynamic import support#22381
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| 'use strict'; | ||
| const { NativeModule } = require('internal/bootstrap/loaders'); | ||
| const { ModuleWrap } = internalBinding('module_wrap'); | ||
| const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); | ||
| const { | ||
| stripShebang, | ||
| stripBOM | ||
| @@ -15,6 +15,8 @@ const { _makeLong } = require('path'); | ||
| const { SafeMap } = require('internal/safe_globals'); | ||
| const { URL } = require('url'); | ||
| const { debuglog, promisify } = require('util'); | ||
| const esmLoader = require('internal/process/esm_loader'); | ||
| const readFileAsync = promisify(fs.readFile); | ||
| const readFileSync = fs.readFileSync; | ||
| const StringReplace = Function.call.bind(String.prototype.replace); | ||
| @@ -25,13 +27,27 @@ const debug = debuglog('esm'); | ||
| const translators = new SafeMap(); | ||
| module.exports = translators; | ||
| function initializeImportMeta(meta, { url }) { | ||
| meta.url = url; | ||
| } | ||
| async function importModuleDynamically(specifier, { url }) { | ||
| const loader = await esmLoader.loaderPromise; | ||
| return loader.import(specifier, url); | ||
| } | ||
| // Strategy for loading a standard JavaScript module | ||
| translators.set('esm', async (url) => { | ||
| const source = `${await readFileAsync(new URL(url))}`; | ||
| debug(`Translating StandardModule ${url}`); | ||
| const module = new ModuleWrap(stripShebang(source), url); | ||
| callbackMap.set(module, { | ||
| initializeImportMeta, | ||
| importModuleDynamically, | ||
| }); | ||
devsnek marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. TimothyGu marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| return { | ||
| module: new ModuleWrap(stripShebang(source), url), | ||
| reflect: undefined | ||
| module, | ||
| reflect: undefined, | ||
| }; | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,40 +2,42 @@ | ||
| const { | ||
| setImportModuleDynamicallyCallback, | ||
| setInitializeImportMetaObjectCallback | ||
| setInitializeImportMetaObjectCallback, | ||
| ||
| callbackMap, | ||
| } = internalBinding('module_wrap'); | ||
| const { pathToFileURL } = require('internal/url'); | ||
| const Loader = require('internal/modules/esm/loader'); | ||
| const path = require('path'); | ||
| const { URL } = require('url'); | ||
| const { | ||
| initImportMetaMap, | ||
| wrapToModuleMap | ||
| wrapToModuleMap, | ||
| } = require('internal/vm/source_text_module'); | ||
| const { | ||
| ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING, | ||
| } = require('internal/errors').codes; | ||
| function normalizeReferrerURL(referrer) { | ||
| if (typeof referrer === 'string' && path.isAbsolute(referrer)) { | ||
| return pathToFileURL(referrer).href; | ||
| function initializeImportMetaObject(wrap, meta) { | ||
| if (callbackMap.has(wrap)) { | ||
| const { initializeImportMeta } = callbackMap.get(wrap); | ||
| if (initializeImportMeta !== undefined) { | ||
| initializeImportMeta(meta, wrapToModuleMap.get(wrap) || wrap); | ||
| } | ||
| } | ||
| return new URL(referrer).href; | ||
| } | ||
| function initializeImportMetaObject(wrap, meta) { | ||
| const vmModule = wrapToModuleMap.get(wrap); | ||
| if (vmModule === undefined) { | ||
| // This ModuleWrap belongs to the Loader. | ||
| meta.url = wrap.url; | ||
| } else { | ||
| const initializeImportMeta = initImportMetaMap.get(vmModule); | ||
| if (initializeImportMeta !== undefined) { | ||
| // This ModuleWrap belongs to vm.SourceTextModule, | ||
| // initializer callback was provided. | ||
| initializeImportMeta(meta, vmModule); | ||
| async function importModuleDynamicallyCallback(wrap, specifier) { | ||
| if (callbackMap.has(wrap)) { | ||
| const { importModuleDynamically } = callbackMap.get(wrap); | ||
| if (importModuleDynamically !== undefined) { | ||
| return importModuleDynamically( | ||
| specifier, wrapToModuleMap.get(wrap) || wrap); | ||
| } | ||
| } | ||
| throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING(); | ||
| } | ||
| setInitializeImportMetaObjectCallback(initializeImportMetaObject); | ||
| setImportModuleDynamicallyCallback(importModuleDynamicallyCallback); | ||
| let loaderResolve; | ||
| exports.loaderPromise = new Promise((resolve, reject) => { | ||
| loaderResolve = resolve; | ||
| @@ -44,8 +46,6 @@ exports.loaderPromise = new Promise((resolve, reject) => { | ||
| exports.ESMLoader = undefined; | ||
| exports.setup = function() { | ||
| setInitializeImportMetaObjectCallback(initializeImportMetaObject); | ||
| let ESMLoader = new Loader(); | ||
| const loaderPromise = (async () => { | ||
| const userLoader = process.binding('config').userLoader; | ||
| @@ -60,10 +60,5 @@ exports.setup = function() { | ||
| })(); | ||
| loaderResolve(loaderPromise); | ||
| setImportModuleDynamicallyCallback(async (referrer, specifier) => { | ||
| const loader = await loaderPromise; | ||
| return loader.import(specifier, normalizeReferrerURL(referrer)); | ||
| }); | ||
| exports.ESMLoader = ESMLoader; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.