Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tweak importModuleDynamically inlining
  • Loading branch information
guybedford authored and coreyfarrell committed Dec 6, 2019
commit bcc0331b7115d7a31e09f15f148369616e4dd94b
25 changes: 10 additions & 15 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function evalModule(source, print) {
function evalScript(name, body, breakFirstLine, print) {
const CJSModule = require('internal/modules/cjs/loader').Module;
const { kVmBreakFirstLineSymbol } = require('internal/util');
const { pathToFileURL } = require('url');

const cwd = tryGetCwd();
const origModule = global.module; // Set e.g. when called from the REPL.
Expand All @@ -65,35 +66,29 @@ function evalScript(name, body, breakFirstLine, print) {
module.filename = path.join(cwd, name);
module.paths = CJSModule._nodeModulePaths(cwd);

let fileURL;
module.importModuleDynamically = async (specifier) => {
if (!fileURL) {
const { pathToFileURL } = require('url');
fileURL = pathToFileURL(path.join(cwd, name)).href;
}
global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol;
global.asyncESM = require('internal/process/esm_loader');

const asyncESM = require('internal/process/esm_loader');
const loader = await asyncESM.ESMLoader;
return loader.import(specifier, fileURL);
};
const baseUrl = pathToFileURL(module.filename).href;

global.kVmBreakFirstLineSymbol = kVmBreakFirstLineSymbol;
const script = `
global.__filename = ${JSONStringify(name)};
global.exports = exports;
global.module = module;
global.__dirname = __dirname;
global.require = require;
const { kVmBreakFirstLineSymbol } = global;
const { kVmBreakFirstLineSymbol, asyncESM } = global;
delete global.kVmBreakFirstLineSymbol;
const { importModuleDynamically } = module;
delete module.importModuleDynamically;
delete global.asyncESM;
return require("vm").runInThisContext(
${JSONStringify(body)}, {
filename: ${JSONStringify(name)},
displayErrors: true,
[kVmBreakFirstLineSymbol]: ${!!breakFirstLine},
importModuleDynamically
async importModuleDynamically (specifier) {
const loader = await asyncESM.ESMLoader;
return loader.import(specifier, ${JSONStringify(baseUrl)});
}
});\n`;
const result = module._compile(script, `${name}-wrapper`);
if (print) {
Expand Down