| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 77c69f5 commit cad5c2b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,6 @@ const defaultResolve = require('internal/modules/esm/default_resolve'); | |||
| 21 | 21 | const createDynamicModule = require( | |
| 22 | 22 | 'internal/modules/esm/create_dynamic_module'); | |
| 23 | 23 | const { translators } = require('internal/modules/esm/translators'); | |
| 24 | - const { ModuleWrap } = internalBinding('module_wrap'); | ||
| 25 | 24 | const { getOptionValue } = require('internal/options'); | |
| 26 | 25 | ||
| 27 | 26 | const debug = require('internal/util/debuglog').debuglog('esm'); | |
@@ -118,7 +117,17 @@ class Loader { | |||
| 118 | 117 | source, | |
| 119 | 118 | url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href | |
| 120 | 119 | ) { | |
| 121 | - const evalInstance = (url) => new ModuleWrap(url, undefined, source, 0, 0); | ||
| 120 | + const evalInstance = (url) => { | ||
| 121 | + const { ModuleWrap, callbackMap } = internalBinding('module_wrap'); | ||
| 122 | + const module = new ModuleWrap(url, undefined, source, 0, 0); | ||
| 123 | + callbackMap.set(module, { | ||
| 124 | + importModuleDynamically: (specifier, { url }) => { | ||
| 125 | + return this.import(specifier, url); | ||
| 126 | + } | ||
| 127 | + }); | ||
| 128 | + | ||
| 129 | + return module; | ||
| 130 | + }; | ||
| 122 | 131 | const job = new ModuleJob(this, url, evalInstance, false, false); | |
| 123 | 132 | this.moduleMap.set(url, job); | |
| 124 | 133 | const { module, result } = await job.run(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,27 +55,38 @@ function evalModule(source, print) { | |||
| 55 | 55 | function evalScript(name, body, breakFirstLine, print) { | |
| 56 | 56 | const CJSModule = require('internal/modules/cjs/loader').Module; | |
| 57 | 57 | const { kVmBreakFirstLineSymbol } = require('internal/util'); | |
| 58 | + const { pathToFileURL } = require('url'); | ||
| 58 | 59 | ||
| 59 | 60 | const cwd = tryGetCwd(); | |
| 60 | 61 | const origModule = global.module; // Set e.g. when called from the REPL. | |
| 61 | 62 | ||
| 62 | 63 | const module = new CJSModule(name); | |
| 63 | 64 | module.filename = path.join(cwd, name); | |
| 64 | 65 | module.paths = CJSModule._nodeModulePaths(cwd); | |
| 66 | + | ||
| 65 | 67 | global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol; | |
| 68 | + global.asyncESM = require('internal/process/esm_loader'); | ||
| 69 | + | ||
| 70 | + const baseUrl = pathToFileURL(module.filename).href; | ||
| 71 | + | ||
| 66 | 72 | const script = ` | |
| 67 | 73 | global.__filename = ${JSON.stringify(name)}; | |
| 68 | 74 | global.exports = exports; | |
| 69 | 75 | global.module = module; | |
| 70 | 76 | global.__dirname = __dirname; | |
| 71 | 77 | global.require = require; | |
| 72 | - const { kVmBreakFirstLineSymbol } = global; | ||
| 78 | + const { kVmBreakFirstLineSymbol, asyncESM } = global; | ||
| 73 | 79 | delete global.kVmBreakFirstLineSymbol; | |
| 80 | + delete global.asyncESM; | ||
| 74 | 81 | return require("vm").runInThisContext( | |
| 75 | 82 | ${JSON.stringify(body)}, { | |
| 76 | 83 | filename: ${JSON.stringify(name)}, | |
| 77 | 84 | displayErrors: true, | |
| 78 | - [kVmBreakFirstLineSymbol]: ${!!breakFirstLine} | ||
| 85 | + [kVmBreakFirstLineSymbol]: ${!!breakFirstLine}, | ||
| 86 | + async importModuleDynamically (specifier) { | ||
| 87 | + const loader = await asyncESM.ESMLoader; | ||
| 88 | + return loader.import(specifier, ${JSON.stringify(baseUrl)}); | ||
| 89 | + } | ||
| 79 | 90 | });\n`; | |
| 80 | 91 | const result = module._compile(script, `${name}-wrapper`); | |
| 81 | 92 | 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} ${execOptions} ` + | ||
| 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