Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4e4add3
esm: remove .json support
guybedford Aug 28, 2018
e51b25b
esm: remove .node support
MylesBorins Sep 12, 2018
0416d3b
esm: remove .js support
MylesBorins Oct 2, 2018
4dfe731
esm: remove node specifier resolution algorithm
MylesBorins Oct 2, 2018
c8bbebe
doc: document minimal kernel
MylesBorins Oct 2, 2018
68a0551
esm: Remove --loader.
jdalton Oct 2, 2018
ac001a7
esm: minimal resolver spec and implementation refinements
guybedford Oct 24, 2018
07a7116
specify import file specifier resolution proposal
guybedford Nov 29, 2018
fedb9f0
irp type implementation
guybedford Jan 24, 2019
5b9c060
esm: top-level --type, -m flags, spec updates
guybedford Feb 10, 2019
6657f68
esm: implement top-level --type flag, type:esm -> type:module
guybedford Feb 11, 2019
1a8df02
esm: check for mismatch between --type and input file extension
GeoffreyBooth Feb 24, 2019
85df511
esm: rename internal module type strings to match public api for --type
GeoffreyBooth Feb 24, 2019
80a4ba4
esm: --type no longer overrides package.json; throw on conflict
GeoffreyBooth Feb 24, 2019
64963d3
esm: add -m alias for --type=module
GeoffreyBooth Feb 17, 2019
1acff8a
esm: tests for --type error cases
GeoffreyBooth Feb 25, 2019
a3ac35d
esm: include format mismatch errors in spec
guybedford Feb 26, 2019
3f5af33
esm: implement symlink handling in package scope
guybedford Feb 26, 2019
d3b7bbc
esm: merge both 'type mismatch' errors into one
GeoffreyBooth Feb 27, 2019
381762e
esm: tests for correctly detecting module types of symlinks
GeoffreyBooth Feb 27, 2019
7359c43
esm: unify type mismatch errors
guybedford Feb 27, 2019
724bf02
esm: fix errors for --type / "type" mismatches
GeoffreyBooth Feb 27, 2019
19ec40f
test: add additional fixtures and tests for irp
MylesBorins Feb 26, 2019
59cf1f4
misc: ci fixes for latest commits
guybedford Mar 5, 2019
c73a555
test: fixup symlinks test in windows
guybedford Mar 5, 2019
04892d5
esm: Revert "esm: Remove --loader."
guybedford Mar 4, 2019
4406f99
esm: add experimental warning for --loader
guybedford Mar 5, 2019
0237465
fixup: fix broken tests
MylesBorins Mar 6, 2019
47bdf9d
doc: esm flags, package scope and file extensions rules (wip)
GeoffreyBooth Mar 5, 2019
0f701a0
doc: esm features, differences/interoperability with commonjs
GeoffreyBooth Mar 6, 2019
80d6ab1
doc: package entry points in esm
GeoffreyBooth Mar 6, 2019
ecc07e2
doc: notes from call
GeoffreyBooth Mar 7, 2019
fcc8c86
doc: improve createRequire example
MylesBorins Mar 7, 2019
8de9da0
doc: updates from review
MylesBorins Mar 8, 2019
f6e1a43
doc: fixup describing "main" as package entry point
GeoffreyBooth Mar 8, 2019
bec588f
esm: add --es-module-specifier-resolution
MylesBorins Mar 5, 2019
4a95e44
esm: scoped --type, cpp refactoring
guybedford Mar 10, 2019
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
Next Next commit
esm: fix errors for --type / "type" mismatches
  • Loading branch information
GeoffreyBooth authored and nodejs-ci committed Mar 7, 2019
commit 724bf02a29c8b8b21f6d99ae1a810380017fa508
4 changes: 1 addition & 3 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,7 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
> 1. Return _"commonjs"_.
> 1. If _pjson.type_ exists and is _"module"_, then
> 1. If _url_ ends in _".cjs"_, then
> 1. Throw a _Type Mismatch_ error.
> 1. If _url_ does not end in _".js"_ or _".mjs"_, then
> 1. Throw an _Unsupported File Extension_ error.
> 1. Return _"commonjs"_.
> 1. Return _"module"_.
> 1. Otherwise,
> 1. If _url_ ends in _".mjs"_, then
Expand Down
24 changes: 11 additions & 13 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,21 +956,19 @@ E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
E('ERR_TRANSFORM_WITH_LENGTH_0',
'Calling transform done when writableState.length != 0', Error);
E('ERR_TTY_INIT_FAILED', 'TTY initialization failed', SystemError);
E('ERR_TYPE_MISMATCH', (filename, moduleType, scopeConflict, extConflict) => {
const typeValue = moduleType ? 'module' : 'commonjs';
E('ERR_TYPE_MISMATCH', (filename, ext, typeFlag, conflict) => {
const typeString =
typeFlag === 'module' ? '--type=module or -m' : '--type=commonjs';
// --type mismatches file extension
if (!scopeConflict && extConflict)
return `--type=${typeValue + (typeValue === 'module' ? ' / -m' : '')}` +
` flag conflicts with the file extension loading ${filename}`;
if (conflict === 'extension')
return `Extension ${ext} is not supported for ` +
`${typeString} loading ${filename}`;
// --type mismatches package.json "type"
else if (scopeConflict && !extConflict)
return `--type=${typeValue + (typeValue === 'module' ? ' / ` -m' : '')}` +
' flag conflicts with the package.json "type": "' +
(moduleType ? 'commonjs' : 'module') + `" loading ${filename}`;
// package.json "type" mismatches file extension
else if (extConflict && scopeConflict)
return `"type": "${typeValue}" in package.json conflicts with the file ` +
`extension loading ${filename}`;
else if (conflict === 'scope')
return `Cannot use ${typeString} because nearest parent package.json ` +
((typeFlag === 'module') ?
'includes "type": "commonjs"' : 'includes "type": "module",') +
` which controls the type to use for ${filename}`;
}, TypeError);
E('ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET',
'`process.setupUncaughtExceptionCapture()` was called while a capture ' +
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ function getModuleFormat(url, isMain, parentURL) {

const ext = extname(url.pathname);

if (!legacy && ext === '.cjs')
throw new ERR_TYPE_MISMATCH(true, true, fileURLToPath(url));

let format = (legacy ? legacyExtensionFormatMap : extensionFormatMap)[ext];

if (!format) {
Expand All @@ -89,14 +86,17 @@ function getModuleFormat(url, isMain, parentURL) {
}

// Check for mismatch between --type and file extension,
// and between --type and package.json.
// and between --type and the "type" field in package.json.
if (isMain && format !== 'module' && asyncESM.typeFlag === 'module') {
// Conflict between package scope type and --type
if (ext === '.js') {
throw new ERR_TYPE_MISMATCH(fileURLToPath(url), true, true, false);
if (pcfg && pcfg.type)
throw new ERR_TYPE_MISMATCH(
fileURLToPath(url), ext, asyncESM.typeFlag, 'scope');
// Conflict between explicit extension (.mjs, .cjs) and --type
} else {
throw new ERR_TYPE_MISMATCH(fileURLToPath(url), true, false, true);
throw new ERR_TYPE_MISMATCH(
fileURLToPath(url), ext, asyncESM.typeFlag, 'extension');
}
}

Expand Down
9 changes: 5 additions & 4 deletions test/es-module/test-esm-type-flag-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ expect('', packageTypeModuleMain, 'package-type-module');
expect('', packageTypeCommonJsMain, 'package-type-commonjs');
expect('', packageWithoutTypeMain, 'package-without-type');

// Check that running with --type and no package.json "type" works
expect('--type=commonjs', packageWithoutTypeMain, 'package-without-type');
expect('--type=module', packageWithoutTypeMain, 'package-without-type');
expect('-m', packageWithoutTypeMain, 'package-without-type');

// Check that running with conflicting --type flags throws errors
expect('--type=commonjs', mjsFile, 'ERR_REQUIRE_ESM', true);
expect('--type=module', cjsFile, 'ERR_TYPE_MISMATCH', true);
Expand All @@ -29,10 +34,6 @@ expect('--type=module', packageTypeCommonJsMain,
'ERR_TYPE_MISMATCH', true);
expect('-m', packageTypeCommonJsMain,
'ERR_TYPE_MISMATCH', true);
expect('--type=module', packageWithoutTypeMain,
'ERR_TYPE_MISMATCH', true);
expect('-m', packageWithoutTypeMain,
'ERR_TYPE_MISMATCH', true);

function expect(opt = '', inputFile, want, wantsError = false) {
// TODO: Remove when --experimental-modules is unflagged
Expand Down