Is your feature request related to a problem? Please describe.
Consider the following:
const vm = require('vm');
new vm.Script(`import fs from "fs";`, { filename: 'script.mjs' });
When executed with --experimental-modules, as of 12.0.0 this throws:
script.mjs:1
import fs from "fs";
^^
SyntaxError: Unexpected identifier
Describe the solution you'd like
-
The "input-type" to be inferred from the filename .mjs
-
An additional option added to vm.Script, called type, which overrides file-based inference.
new vm.Script(`import fs from "fs";`, { type: 'module' });
-
Resolve hooks should be available, e.g.:
async function resolve(specifier, parentURL, defaultResolve) {
return defaultResolve('./script.mjs', 'file:///');
}
new vm.Script(`import fs from "fs"`, { loader: { resolve } });
Describe alternatives you've considered
Alternatively, an API to determine the type/format of a given file path using the same rules that determine the type of node --experimental-modules script.js. E.g.:
const type = require.resolveType('/path/to/file.js');
This would work for some cases but the resolve hook is more powerful.
Is your feature request related to a problem? Please describe.
Consider the following:
When executed with
--experimental-modules, as of12.0.0this throws:Describe the solution you'd like
The "input-type" to be inferred from the filename
.mjsAn additional option added to
vm.Script, calledtype, which overrides file-based inference.Resolve hooks should be available, e.g.:
Describe alternatives you've considered
Alternatively, an API to determine the
type/formatof a given file path using the same rules that determine the type ofnode --experimental-modules script.js. E.g.:This would work for some cases but the resolve hook is more powerful.