| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3557659 commit d4aa656
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,6 @@ const defaultResolve = require('internal/modules/esm/default_resolve'); | |||
| 25 | 25 | const createDynamicModule = require( | |
| 26 | 26 | 'internal/modules/esm/create_dynamic_module'); | |
| 27 | 27 | const { translators } = require('internal/modules/esm/translators'); | |
| 28 | - const { ModuleWrap } = internalBinding('module_wrap'); | ||
| 29 | 28 | const { getOptionValue } = require('internal/options'); | |
| 30 | 29 | ||
| 31 | 30 | const debug = require('internal/util/debuglog').debuglog('esm'); | |
@@ -117,7 +116,17 @@ class Loader { | |||
| 117 | 116 | source, | |
| 118 | 117 | url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href | |
| 119 | 118 | ) { | |
| 120 | - const evalInstance = (url) => new ModuleWrap(url, undefined, source, 0, 0); | ||
| 119 | + const evalInstance = (url) => { | ||
| 120 | + const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); | ||
| 121 | + const module = new ModuleWrap(url, undefined, source, 0, 0); | ||
| 122 | + callbackMap.set(module, { | ||
| 123 | + importModuleDynamically: (specifier, { url }) => { | ||
| 124 | + return this.import(specifier, url); | ||
| 125 | + } | ||
| 126 | + }); | ||
| 127 | + | ||
| 128 | + return module; | ||
| 129 | + }; | ||
| 121 | 130 | const job = new ModuleJob(this, url, evalInstance, false, false); | |
| 122 | 131 | this.moduleMap.set(url, job); | |
| 123 | 132 | const { module, result } = await job.run(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,27 +57,38 @@ function evalModule(source, print) { | |||
| 57 | 57 | function evalScript(name, body, breakFirstLine, print) { | |
| 58 | 58 | const CJSModule = require('internal/modules/cjs/loader').Module; | |
| 59 | 59 | const { kVmBreakFirstLineSymbol } = require('internal/util'); | |
| 60 | + const { pathToFileURL } = require('url'); | ||
| 60 | 61 | ||
| 61 | 62 | const cwd = tryGetCwd(); | |
| 62 | 63 | const origModule = global.module; // Set e.g. when called from the REPL. | |
| 63 | 64 | ||
| 64 | 65 | const module = new CJSModule(name); | |
| 65 | 66 | module.filename = path.join(cwd, name); | |
| 66 | 67 | module.paths = CJSModule._nodeModulePaths(cwd); | |
| 68 | + | ||
| 67 | 69 | global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol; | |
| 70 | + global.asyncESM = require('internal/process/esm_loader'); | ||
| 71 | + | ||
| 72 | + const baseUrl = pathToFileURL(module.filename).href; | ||
| 73 | + | ||
| 68 | 74 | const script = ` | |
| 69 | 75 | global.__filename = ${JSONStringify(name)}; | |
| 70 | 76 | global.exports = exports; | |
| 71 | 77 | global.module = module; | |
| 72 | 78 | global.__dirname = __dirname; | |
| 73 | 79 | global.require = require; | |
| 74 | - const { kVmBreakFirstLineSymbol } = global; | ||
| 80 | + const { kVmBreakFirstLineSymbol, asyncESM } = global; | ||
| 75 | 81 | delete global.kVmBreakFirstLineSymbol; | |
| 82 | + delete global.asyncESM; | ||
| 76 | 83 | return require("vm").runInThisContext( | |
| 77 | 84 | ${JSONStringify(body)}, { | |
| 78 | 85 | filename: ${JSONStringify(name)}, | |
| 79 | 86 | displayErrors: true, | |
| 80 | - [kVmBreakFirstLineSymbol]: ${!!breakFirstLine} | ||
| 87 | + [kVmBreakFirstLineSymbol]: ${!!breakFirstLine}, | ||
| 88 | + async importModuleDynamically (specifier) { | ||
| 89 | + const loader = await asyncESM.ESMLoader; | ||
| 90 | + return loader.import(specifier, ${JSONStringify(baseUrl)}); | ||
| 91 | + } | ||
| 81 | 92 | });\n`; | |
| 82 | 93 | const result = module._compile(script, `${name}-wrapper`); | |
| 83 | 94 | if (print) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,3 +283,23 @@ child.exec( | |||
| 283 | 283 | assert.ifError(err); | |
| 284 | 284 | assert.strictEqual(stdout, '.mjs file\n'); | |
| 285 | 285 | })); | |
| 286 | + | ||
| 287 | + | ||
| 288 | + // Assert that packages can be dynamic imported initial cwd-relative with --eval | ||
| 289 | + child.exec( | ||
| 290 | + `${nodejs} ${execOptions} ` + | ||
| 291 | + '--eval "process.chdir(\'..\');' + | ||
| 292 | + 'import(\'./test/fixtures/es-modules/mjs-file.mjs\')"', | ||
| 293 | + common.mustCall((err, stdout) => { | ||
| 294 | + assert.ifError(err); | ||
| 295 | + assert.strictEqual(stdout, '.mjs file\n'); | ||
| 296 | + })); | ||
| 297 | + | ||
| 298 | + child.exec( | ||
| 299 | + `${nodejs} ` + | ||
| 300 | + '--eval "process.chdir(\'..\');' + | ||
| 301 | + 'import(\'./test/fixtures/es-modules/mjs-file.mjs\')"', | ||
| 302 | + common.mustCall((err, stdout) => { | ||
| 303 | + assert.ifError(err); | ||
| 304 | + assert.strictEqual(stdout, '.mjs file\n'); | ||
| 305 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments