Skip to content

Commit d40ff59

Browse files
committed
follow-up to #2075
1 parent 8602fd1 commit d40ff59

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686

8787
With this release, esbuild now removes simplified statement-level expressions if the simplified result is a literal expression even when minification is disabled. Previously this was only done when minification is enabled. This change was only made because some people are bothered by seeing top-level literal expressions. This change has no effect on code behavior.
8888

89+
* Ignore `.d.ts` rules in `paths` in `tsconfig.json` files ([#2074](https://github.com/evanw/esbuild/issues/2074), [#2075](https://github.com/evanw/esbuild/pull/2075))
90+
91+
TypeScript's `tsconfig.json` configuration file has a `paths` field that lets you remap import paths to alternative files on the file system. This field is interpreted by esbuild during bundling so that esbuild's behavior matches that of the TypeScript type checker. However, people sometimes override import paths to JavaScript files to instead point to a `.d.ts` TypeScript type declaration file for that JavaScript file. The intent of this is to just use the remapping for type information and not to actually import the `.d.ts` file during the build.
92+
93+
With this release, esbuild will now ignore rules in `paths` that result in a `.d.ts` file during path resolution. This means code that does this should now be able to be bundled without modifying its `tsconfig.json` file to remove the `.d.ts` rule. This change was contributed by [@magic-akari](https://github.com/magic-akari).
94+
8995
## 0.14.23
9096

9197
* Update feature database to indicate that node 16.14+ supports import assertions ([#2030](https://github.com/evanw/esbuild/issues/2030))

internal/resolver/resolver.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -1459,11 +1459,17 @@ func (r resolverQuery) loadAsMainField(dirInfo *dirInfo, path string, extensionO
14591459
return PathPair{}, false, nil
14601460
}
14611461

1462+
func hasCaseInsensitiveSuffix(s string, suffix string) bool {
1463+
return len(s) >= len(suffix) && strings.EqualFold(s[len(s)-len(suffix):], suffix)
1464+
}
1465+
14621466
// This closely follows the behavior of "tryLoadModuleUsingPaths()" in the
14631467
// official TypeScript compiler
14641468
func (r resolverQuery) matchTSConfigPaths(tsConfigJSON *TSConfigJSON, path string) (PathPair, bool, *fs.DifferentCase) {
14651469
if r.debugLogs != nil {
14661470
r.debugLogs.addNote(fmt.Sprintf("Matching %q against \"paths\" in %q", path, tsConfigJSON.AbsPath))
1471+
r.debugLogs.increaseIndent()
1472+
defer r.debugLogs.decreaseIndent()
14671473
}
14681474

14691475
absBaseURL := tsConfigJSON.BaseURLForPaths
@@ -1486,7 +1492,11 @@ func (r resolverQuery) matchTSConfigPaths(tsConfigJSON *TSConfigJSON, path strin
14861492
r.debugLogs.addNote(fmt.Sprintf("Found an exact match for %q in \"paths\"", key))
14871493
}
14881494
for _, originalPath := range originalPaths {
1489-
if strings.HasSuffix(originalPath, ".d.ts") || strings.HasSuffix(strings.ToLower(originalPath), ".d.ts") {
1495+
// Ignore ".d.ts" files because this rule is obviously only here for type checking
1496+
if hasCaseInsensitiveSuffix(originalPath, ".d.ts") {
1497+
if r.debugLogs != nil {
1498+
r.debugLogs.addNote(fmt.Sprintf("Ignoring substitution %q because it ends in \".d.ts\"", originalPath))
1499+
}
14901500
continue
14911501
}
14921502

@@ -1547,7 +1557,11 @@ func (r resolverQuery) matchTSConfigPaths(tsConfigJSON *TSConfigJSON, path strin
15471557
matchedText := path[len(longestMatch.prefix) : len(path)-len(longestMatch.suffix)]
15481558
originalPath = strings.Replace(originalPath, "*", matchedText, 1)
15491559

1550-
if strings.HasSuffix(originalPath, ".d.ts") || strings.HasSuffix(strings.ToLower(originalPath), ".d.ts") {
1560+
// Ignore ".d.ts" files because this rule is obviously only here for type checking
1561+
if hasCaseInsensitiveSuffix(originalPath, ".d.ts") {
1562+
if r.debugLogs != nil {
1563+
r.debugLogs.addNote(fmt.Sprintf("Ignoring substitution %q because it ends in \".d.ts\"", originalPath))
1564+
}
15511565
continue
15521566
}
15531567

0 commit comments

Comments
 (0)