diff options
author | Guy Bedford <guybedford@gmail.com> | 2019-11-15 16:28:59 -0500 |
---|---|---|
committer | Myles Borins <mylesborins@google.com> | 2019-11-16 23:26:49 -0800 |
commit | 250a9956561de336bc408d7ed184ba3a2610e272 (patch) | |
tree | 8601ea7b525b407f00abd4c83fb79022e39aea3b /lib/internal/modules/esm | |
parent | d9efc7dc10e9cab4b5525b9ccc0750ccc3e613c1 (diff) | |
download | android-node-v8-250a9956561de336bc408d7ed184ba3a2610e272.tar.gz android-node-v8-250a9956561de336bc408d7ed184ba3a2610e272.tar.bz2 android-node-v8-250a9956561de336bc408d7ed184ba3a2610e272.zip |
esm: disable non-js exts outside package scopes
PR-URL: https://github.com/nodejs/node/pull/30501
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Diffstat (limited to 'lib/internal/modules/esm')
-rw-r--r-- | lib/internal/modules/esm/default_resolve.js | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js index 88af3cb5f8..5271c6a0fe 100644 --- a/lib/internal/modules/esm/default_resolve.js +++ b/lib/internal/modules/esm/default_resolve.js @@ -78,9 +78,19 @@ function resolve(specifier, parentURL) { } const isMain = parentURL === undefined; - if (isMain) + if (isMain) { parentURL = pathToFileURL(`${process.cwd()}/`).href; + // This is the initial entry point to the program, and --input-type has + // been passed as an option; but --input-type can only be used with + // --eval, --print or STDIN string input. It is not allowed with file + // input, to avoid user confusion over how expansive the effect of the + // flag should be (i.e. entry point only, package scope surrounding the + // entry point, etc.). + if (typeFlag) + throw new ERR_INPUT_TYPE_NOT_ALLOWED(); + } + let url = moduleWrapResolve(specifier, parentURL); if (isMain ? !preserveSymlinksMain : !preserveSymlinks) { @@ -93,27 +103,13 @@ function resolve(specifier, parentURL) { url.hash = old.hash; } - const type = getPackageType(url.href); - const ext = extname(url.pathname); - const extMap = - type !== TYPE_MODULE ? legacyExtensionFormatMap : extensionFormatMap; - let format = extMap[ext]; - - if (isMain && typeFlag) { - // This is the initial entry point to the program, and --input-type has - // been passed as an option; but --input-type can only be used with - // --eval, --print or STDIN string input. It is not allowed with file - // input, to avoid user confusion over how expansive the effect of the - // flag should be (i.e. entry point only, package scope surrounding the - // entry point, etc.). - throw new ERR_INPUT_TYPE_NOT_ALLOWED(); - } + let format = extensionFormatMap[ext]; + if (ext === '.js' || (!format && isMain)) + format = getPackageType(url.href) === TYPE_MODULE ? 'module' : 'commonjs'; if (!format) { - if (isMain) - format = type === TYPE_MODULE ? 'module' : 'commonjs'; - else if (esModuleSpecifierResolution === 'node') - format = 'commonjs'; + if (esModuleSpecifierResolution === 'node') + format = legacyExtensionFormatMap[ext]; else throw new ERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url)); } |