-
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
--moduleResolution bundler
: Require ESM for module
and remove node
from hard-coded conditions
#52940
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ import { | |
ModuleResolutionHost, | ||
ModuleResolutionKind, | ||
moduleResolutionOptionDeclarations, | ||
moduleResolutionSupportsPackageJsonExportsAndImports, | ||
noop, | ||
noopPush, | ||
normalizePath, | ||
|
@@ -705,8 +706,11 @@ export function getConditions(options: CompilerOptions, esmMode?: boolean) { | |
// conditions are only used by the node16/nodenext/bundler resolvers - there's no priority order in the list, | ||
// it's essentially a set (priority is determined by object insertion order in the object we look at). | ||
const conditions = esmMode || getEmitModuleResolutionKind(options) === ModuleResolutionKind.Bundler | ||
? ["node", "import"] | ||
: ["node", "require"]; | ||
? ["import"] | ||
: ["require"]; | ||
if (getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler) { | ||
conditions.unshift("node"); | ||
} | ||
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. Someone suggested dropping 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. Small nit suggestion: Instead of unshift can we first add "node" and then others so we arent moving array elements 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. I don't think the order matters; I just didn’t want to change a bunch of baselines unnecessarily. I did 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. Interestingly conditions can be a |
||
if (!options.noDtsResolution) { | ||
conditions.push("types"); | ||
} | ||
|
@@ -1701,7 +1705,7 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa | |
candidateIsFromPackageJsonField: false, | ||
}; | ||
|
||
if (traceEnabled && getEmitModuleResolutionKind(compilerOptions) >= ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) <= ModuleResolutionKind.NodeNext) { | ||
if (traceEnabled && moduleResolutionSupportsPackageJsonExportsAndImports(getEmitModuleResolutionKind(compilerOptions))) { | ||
trace(host, Diagnostics.Resolving_in_0_mode_with_conditions_1, features & NodeResolutionFeatures.EsmMode ? "ESM" : "CJS", conditions.map(c => `'${c}'`).join(", ")); | ||
} | ||
|
||
|
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"; | ||
|
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 {}; |
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)) | ||
|
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]'. ========" | ||
] |
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 | ||
|
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
.