-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: better error for named exports from cjs
We do not support importing named exports from a CJS module. This change decorates the error message for missing named exports in the case where the module being imported is expected to be CJS by the ESM loader. Signed-off-by: Myles Borins <[email protected]> PR-URL: #33256 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
- Loading branch information
1 parent
9dde1db
commit 77caf92
Showing
15 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import '../common/index.mjs'; | ||
import { rejects } from 'assert'; | ||
|
||
const fixtureBase = '../fixtures/es-modules/package-cjs-named-error'; | ||
|
||
const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' + | ||
'be of type CommonJS, which does not support named exports. CommonJS ' + | ||
'modules can be imported by importing the default export.\n' + | ||
'For example:\n' + | ||
'import pkg from \'./fail.cjs\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' is ' + | ||
'expected to be of type CommonJS, which does not support named exports. ' + | ||
'CommonJS modules can be imported by importing the default export.\n' + | ||
'For example:\n' + | ||
'import pkg from \'./json-hack/fail.js\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
const expectedBare = 'The requested module \'deep-fail\' is expected to ' + | ||
'be of type CommonJS, which does not support named exports. CommonJS ' + | ||
'modules can be imported by importing the default export.\n' + | ||
'For example:\n' + | ||
'import pkg from \'deep-fail\';\n' + | ||
'const { comeOn } = pkg;'; | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/single-quote.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedRelative | ||
}, 'should support relative specifiers with single quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/double-quote.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedRelative | ||
}, 'should support relative specifiers with double quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/json-hack.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedPackageHack | ||
}, 'should respect recursive package.json for module type'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/bare-import-single.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedBare | ||
}, 'should support bare specifiers with single quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/bare-import-double.mjs`); | ||
}, { | ||
name: 'SyntaxError', | ||
message: expectedBare | ||
}, 'should support bare specifiers with double quotes'); | ||
|
||
rejects(async () => { | ||
await import(`${fixtureBase}/escaped-single-quote.mjs`); | ||
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/bare-import-double.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from "deep-fail"; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/bare-import-single.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from 'deep-fail'; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/double-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from "./fail.cjs"; |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/escaped-single-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { value } from "./oh'no.cjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
comeOn: 'fhqwhgads' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from './json-hack/fail.js'; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/json-hack/fail.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
comeOn: 'fhqwhgads' | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/json-hack/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
3 changes: 3 additions & 0 deletions
3
test/fixtures/es-modules/package-cjs-named-error/node_modules/deep-fail/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
test/fixtures/es-modules/package-cjs-named-error/node_modules/deep-fail/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
value: 123 | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/fixtures/es-modules/package-cjs-named-error/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "package-cjs-named-error", | ||
"main": "index.mjs", | ||
"type": "module" | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/es-modules/package-cjs-named-error/single-quote.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { comeOn } from './fail.cjs'; |