Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions tsc/internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that recognition of built-in file extensions is also case-sensitive in most places throughout the compiler.

}
fileNameNoExtension := fileName.RemoveExtension(ext)
for _, suffix := range r.compilerOptions.ModuleSuffixes {
path := fileNameNoExtension.AppendSuffix(suffix + ext)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ===

<no content>
Original file line number Diff line number Diff line change
@@ -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";

Original file line number Diff line number Diff line change
@@ -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. ========
63 changes: 63 additions & 0 deletions tsc/testdata/tests/cases/compiler/contentMapperModuleSuffixes.ts
Original file line number Diff line number Diff line change
@@ -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";
Loading