diff --git a/tsc/internal/module/resolver.go b/tsc/internal/module/resolver.go index e988f6b86b472..6dabce74bda89 100644 --- a/tsc/internal/module/resolver.go +++ b/tsc/internal/module/resolver.go @@ -1812,6 +1812,9 @@ func (r *resolutionState) tryFile(fileName resolutionCandidate) (tspath.RootedFi } ext := tspath.RootedFilePathFromPath(fileName.path).Extension() + if ext == "" && len(r.resolver.extraExtensions) > 0 { + ext = tspath.RootedFilePathFromPath(fileName.path).LongestExtension(r.resolver.extraExtensions, tspath.CaseSensitive) + } fileNameNoExtension := fileName.RemoveExtension(ext) for _, suffix := range r.compilerOptions.ModuleSuffixes { path := fileNameNoExtension.AppendSuffix(suffix + ext) diff --git a/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.contentmapper b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.contentmapper new file mode 100644 index 0000000000000..a7d55c0e91beb --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.contentmapper @@ -0,0 +1,24 @@ +//// [/card.web.box] (ScriptKind: ScriptKindTS, ContentMapper: [.box .long.box]) +--- Original --- +export const platform = "web"; +--- Transformed --- +const __VERSION = "1.0.0"; +export const platform = "web"; + +//// [/card.web.long.box] (ScriptKind: ScriptKindTS, ContentMapper: [.box .long.box]) +--- Original --- +export const platform = "web"; +--- Transformed --- +const __VERSION = "1.0.0"; +export const platform = "web"; + +//// [/fallback.box] (ScriptKind: ScriptKindTS, ContentMapper: [.box .long.box]) +--- Original --- +export const platform = "default"; +--- Transformed --- +const __VERSION = "1.0.0"; +export const platform = "default"; + +=== Diagnostics === + + diff --git a/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.errors.txt b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.errors.txt new file mode 100644 index 0000000000000..bb3985a448f53 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.errors.txt @@ -0,0 +1,56 @@ +/main.ts(11,51): error TS2307: Cannot find module './card' or its corresponding type declarations. + + +==== /tsconfig.json (0 errors) ==== + { + "compilerOptions": { + "module": "esnext", + "moduleResolution": "bundler", + "moduleSuffixes": [".web", ""], + "allowImportingTsExtensions": true, + "strict": true, + "traceResolution": true + }, + "files": ["main.ts"], + "contentMappers": [ + { "package": "mapper", "extensions": [".box", ".long.box"] } + ] + } + +==== /main.ts (1 errors) ==== + import { platform } from "./card.box"; + import { platform as longPlatform } from "./card.long.box"; + import { platform as fallbackPlatform } from "./fallback.box"; + import { platform as standardPlatform } from "./target.ts"; + + const check: "web" = platform; + const longCheck: "web" = longPlatform; + const fallbackCheck: "default" = fallbackPlatform; + const standardCheck: "web" = standardPlatform; + + export { platform as extensionlessPlatform } from "./card"; + ~~~~~~~~ +!!! error TS2307: Cannot find module './card' or its corresponding type declarations. + +==== /node_modules/mapper/package.json (0 errors) ==== + { + "name": "mapper", + "version": "1.0.0", + "typescript": { "contentMapper": { "exec": ["compiler-test-mapper"] } } + } + +==== /card.box (0 errors) ==== + export const platform = "default"; + +==== /card.long.box (0 errors) ==== + export const platform = "default"; + +==== /card.long.web.box (0 errors) ==== + export const platform = "wrong"; + +==== /target.ts (0 errors) ==== + export const platform = "default"; + +==== /target.web.ts (0 errors) ==== + export const platform = "web"; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.trace.json b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.trace.json new file mode 100644 index 0000000000000..5e1cf271a866c --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contentMapperModuleSuffixes.trace.json @@ -0,0 +1,45 @@ +======== Resolving module './card.box' from '/main.ts'. ======== +Explicitly specified module resolution kind: 'Bundler'. +Resolving in CJS mode with conditions 'import', 'types'. +Loading module as file / folder, candidate module location '/card.box', target file types: TypeScript, JavaScript, Declaration, JSON. +File name '/card.box' has a '.box' extension - stripping it. +File '/card.web.box' exists - use it as a name resolution result. +======== Module name './card.box' was successfully resolved to '/card.web.box'. ======== +======== Resolving module './card.long.box' from '/main.ts'. ======== +Explicitly specified module resolution kind: 'Bundler'. +Resolving in CJS mode with conditions 'import', 'types'. +Loading module as file / folder, candidate module location '/card.long.box', target file types: TypeScript, JavaScript, Declaration, JSON. +File name '/card.long.box' has a '.long.box' extension - stripping it. +File '/card.web.long.box' exists - use it as a name resolution result. +======== Module name './card.long.box' was successfully resolved to '/card.web.long.box'. ======== +======== Resolving module './fallback.box' from '/main.ts'. ======== +Explicitly specified module resolution kind: 'Bundler'. +Resolving in CJS mode with conditions 'import', 'types'. +Loading module as file / folder, candidate module location '/fallback.box', target file types: TypeScript, JavaScript, Declaration, JSON. +File name '/fallback.box' has a '.box' extension - stripping it. +File '/fallback.web.box' does not exist. +File '/fallback.box' exists - use it as a name resolution result. +======== Module name './fallback.box' was successfully resolved to '/fallback.box'. ======== +======== Resolving module './target.ts' from '/main.ts'. ======== +Explicitly specified module resolution kind: 'Bundler'. +Resolving in CJS mode with conditions 'import', 'types'. +Loading module as file / folder, candidate module location '/target.ts', target file types: TypeScript, JavaScript, Declaration, JSON. +File name '/target.ts' has a '.ts' extension - stripping it. +File '/target.web.ts' exists - use it as a name resolution result. +======== Module name './target.ts' was successfully resolved to '/target.web.ts'. ======== +======== Resolving module './card' from '/main.ts'. ======== +Explicitly specified module resolution kind: 'Bundler'. +Resolving in CJS mode with conditions 'import', 'types'. +Loading module as file / folder, candidate module location '/card', target file types: TypeScript, JavaScript, Declaration, JSON. +File '/card.web.ts' does not exist. +File '/card.ts' does not exist. +File '/card.web.tsx' does not exist. +File '/card.tsx' does not exist. +File '/card.web.d.ts' does not exist. +File '/card.d.ts' does not exist. +File '/card.web.js' does not exist. +File '/card.js' does not exist. +File '/card.web.jsx' does not exist. +File '/card.jsx' does not exist. +Directory '/card' does not exist, skipping all lookups in it. +======== Module name './card' was not resolved. ======== diff --git a/tsc/testdata/tests/cases/compiler/contentMapperModuleSuffixes.ts b/tsc/testdata/tests/cases/compiler/contentMapperModuleSuffixes.ts new file mode 100644 index 0000000000000..6649b19c64d34 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/contentMapperModuleSuffixes.ts @@ -0,0 +1,63 @@ +// @runExternalCode: true +// @noTypesAndSymbols: true +// @noEmit: true + +// @Filename: /tsconfig.json +{ + "compilerOptions": { + "module": "esnext", + "moduleResolution": "bundler", + "moduleSuffixes": [".web", ""], + "allowImportingTsExtensions": true, + "strict": true, + "traceResolution": true + }, + "files": ["main.ts"], + "contentMappers": [ + { "package": "mapper", "extensions": [".box", ".long.box"] } + ] +} + +// @Filename: /node_modules/mapper/package.json +{ + "name": "mapper", + "version": "1.0.0", + "typescript": { "contentMapper": { "exec": ["compiler-test-mapper"] } } +} + +// @Filename: /card.box +export const platform = "default"; + +// @Filename: /card.web.box +export const platform = "web"; + +// @Filename: /card.long.box +export const platform = "default"; + +// @Filename: /card.web.long.box +export const platform = "web"; + +// @Filename: /card.long.web.box +export const platform = "wrong"; + +// @Filename: /fallback.box +export const platform = "default"; + +// @Filename: /target.ts +export const platform = "default"; + +// @Filename: /target.web.ts +export const platform = "web"; + +// @Filename: /main.ts +import { platform } from "./card.box"; +import { platform as longPlatform } from "./card.long.box"; +import { platform as fallbackPlatform } from "./fallback.box"; +import { platform as standardPlatform } from "./target.ts"; + +const check: "web" = platform; +const longCheck: "web" = longPlatform; +const fallbackCheck: "default" = fallbackPlatform; +const standardCheck: "web" = standardPlatform; + +export { platform as extensionlessPlatform } from "./card";