-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
--moduleResolution bundler
: Require ESM for module
and remove node
from hard-coded conditions
#52940
Merged
Merged
--moduleResolution bundler
: Require ESM for module
and remove node
from hard-coded conditions
#52940
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5019,7 +5019,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
* @see https://github.com/microsoft/TypeScript/issues/42151 | ||
*/ | ||
if (emitModuleKindIsNonNodeESM(moduleKind) || mode === ModuleKind.ESNext) { | ||
return importSourceWithoutExtension + (tsExtension === Extension.Mts ? ".mjs" : tsExtension === Extension.Cts ? ".cjs" : ".js"); | ||
const preferTs = isDeclarationFileName(moduleReference) && shouldAllowImportingTsExtension(compilerOptions); | ||
const ext = | ||
tsExtension === Extension.Mts || tsExtension === Extension.Dmts ? preferTs ? ".mts" : ".mjs" : | ||
tsExtension === Extension.Cts || tsExtension === Extension.Dmts ? preferTs ? ".cts" : ".cjs" : | ||
preferTs ? ".ts" : ".js"; | ||
return importSourceWithoutExtension + ext; | ||
} | ||
return importSourceWithoutExtension; | ||
} | ||
|
@@ -43963,9 +43968,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// Import equals declaration is deprecated in es6 or above | ||
grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); | ||
} | ||
else if (!(node.flags & NodeFlags.Ambient) && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) { | ||
grammarErrorOnNode(node, Diagnostics.Import_assignment_is_not_allowed_when_moduleResolution_is_set_to_bundler_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); | ||
} | ||
} | ||
Comment on lines
-43966
to
43971
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can drop this error because it’s redundant with the one that already existed for using |
||
} | ||
} | ||
|
@@ -44208,9 +44210,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
// system modules does not support export assignment | ||
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); | ||
} | ||
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler && !(node.flags & NodeFlags.Ambient)) { | ||
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_moduleResolution_is_set_to_bundler_Consider_using_export_default_or_another_module_format_instead); | ||
} | ||
} | ||
} | ||
|
||
|
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
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
2 changes: 2 additions & 0 deletions
2
tests/baselines/reference/allowImportingTsExtensions(moduleresolution=bundler).errors.txt
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
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/bundlerConditionsExcludesNode.errors.txt
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,44 @@ | ||
error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? | ||
The file is in the program because: | ||
Root file specified for compilation | ||
error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? | ||
The file is in the program because: | ||
Root file specified for compilation | ||
|
||
|
||
!!! error TS6504: File '/node_modules/conditions/index.node.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? | ||
!!! error TS6504: The file is in the program because: | ||
!!! error TS6504: Root file specified for compilation | ||
!!! error TS6504: File '/node_modules/conditions/index.web.js' is a JavaScript file. Did you mean to enable the 'allowJs' option? | ||
!!! error TS6504: The file is in the program because: | ||
!!! error TS6504: Root file specified for compilation | ||
==== /node_modules/conditions/package.json (0 errors) ==== | ||
{ | ||
"name": "conditions", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.cjs", | ||
"types": "index.d.cts", | ||
"exports": { | ||
".": { | ||
"node": "./index.node.js", | ||
"default": "./index.web.js" | ||
} | ||
} | ||
} | ||
|
||
==== /node_modules/conditions/index.node.js (0 errors) ==== | ||
export const node = 0; | ||
|
||
==== /node_modules/conditions/index.node.d.ts (0 errors) ==== | ||
export const node: number; | ||
|
||
==== /node_modules/conditions/index.web.js (0 errors) ==== | ||
export const web = 0; | ||
|
||
==== /node_modules/conditions/index.web.d.ts (0 errors) ==== | ||
export const web: number; | ||
|
||
==== /main.ts (0 errors) ==== | ||
import { web } from "conditions"; | ||
|
35 changes: 35 additions & 0 deletions
35
tests/baselines/reference/bundlerConditionsExcludesNode.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,35 @@ | ||
//// [tests/cases/conformance/moduleResolution/bundler/bundlerConditionsExcludesNode.ts] //// | ||
|
||
//// [package.json] | ||
{ | ||
"name": "conditions", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.cjs", | ||
"types": "index.d.cts", | ||
"exports": { | ||
".": { | ||
"node": "./index.node.js", | ||
"default": "./index.web.js" | ||
} | ||
} | ||
} | ||
|
||
//// [index.node.js] | ||
export const node = 0; | ||
|
||
//// [index.node.d.ts] | ||
export const node: number; | ||
|
||
//// [index.web.js] | ||
export const web = 0; | ||
|
||
//// [index.web.d.ts] | ||
export const web: number; | ||
|
||
//// [main.ts] | ||
import { web } from "conditions"; | ||
|
||
|
||
//// [main.js] | ||
export {}; |
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/bundlerConditionsExcludesNode.symbols
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,12 @@ | ||
=== /node_modules/conditions/index.node.d.ts === | ||
export const node: number; | ||
>node : Symbol(node, Decl(index.node.d.ts, 0, 12)) | ||
|
||
=== /node_modules/conditions/index.web.d.ts === | ||
export const web: number; | ||
>web : Symbol(web, Decl(index.web.d.ts, 0, 12)) | ||
|
||
=== /main.ts === | ||
import { web } from "conditions"; | ||
>web : Symbol(web, Decl(main.ts, 0, 8)) | ||
|
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/bundlerConditionsExcludesNode.trace.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,20 @@ | ||
[ | ||
"======== Resolving module 'conditions' from '/main.ts'. ========", | ||
"Explicitly specified module resolution kind: 'Bundler'.", | ||
"Resolving in CJS mode with conditions 'import', 'types'.", | ||
"File '/package.json' does not exist.", | ||
"Loading module 'conditions' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.", | ||
"Found 'package.json' at '/node_modules/conditions/package.json'.", | ||
"Entering conditional exports.", | ||
"Saw non-matching condition 'node'.", | ||
"Matched 'exports' condition 'default'.", | ||
"Using 'exports' subpath '.' with target './index.web.js'.", | ||
"File name '/node_modules/conditions/index.web.js' has a '.js' extension - stripping it.", | ||
"File '/node_modules/conditions/index.web.ts' does not exist.", | ||
"File '/node_modules/conditions/index.web.tsx' does not exist.", | ||
"File '/node_modules/conditions/index.web.d.ts' exists - use it as a name resolution result.", | ||
"Resolved under condition 'default'.", | ||
"Exiting conditional exports.", | ||
"Resolving real path for '/node_modules/conditions/index.web.d.ts', result '/node_modules/conditions/index.web.d.ts'.", | ||
"======== Module name 'conditions' was successfully resolved to '/node_modules/conditions/index.web.d.ts' with Package ID 'conditions/[email protected]'. ========" | ||
] |
12 changes: 12 additions & 0 deletions
12
tests/baselines/reference/bundlerConditionsExcludesNode.types
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,12 @@ | ||
=== /node_modules/conditions/index.node.d.ts === | ||
export const node: number; | ||
>node : number | ||
|
||
=== /node_modules/conditions/index.web.d.ts === | ||
export const web: number; | ||
>web : number | ||
|
||
=== /main.ts === | ||
import { web } from "conditions"; | ||
>web : number | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has never made a ton of sense to predicate adding an extension to the suggested import source based on
module
and notmoduleResolution
, but rather than rethink that whole issue, I just made a narrow fix for when.ts
is allowed and intended but the message suggests.js
.