Skip to content

Commit

Permalink
Ignore type-only entries in paths (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Mar 3, 2022
1 parent 3c399c1 commit 8602fd1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/bundler/bundler_tsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,44 @@ NOTE: You can mark the path "#/test" as external to exclude it from the bundle,
})
}

func TestTsConfigPathsTypeOnly(t *testing.T) {
tsconfig_suite.expectBundled(t, bundled{
files: map[string]string{
"/Users/user/project/entry.ts": `
import { fib } from "fib";
console.log(fib(10));
`,
"/Users/user/project/node_modules/fib/index.js": `
export function fib(input) {
if (input < 2) {
return input;
}
return fib(input - 1) + fib(input - 2);
}
`,
"/Users/user/project/fib-local.d.ts": `
export function fib(input: number): number;
`,
"/Users/user/project/tsconfig.json": `
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"fib": ["fib-local.d.ts"]
}
}
}
`,
},
entryPaths: []string{"/Users/user/project/entry.ts"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/Users/user/project/out.js",
},
})
}

func TestTsConfigJSX(t *testing.T) {
tsconfig_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
14 changes: 14 additions & 0 deletions internal/bundler/snapshots/snapshots_tsconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ var test_default = 123;
// Users/user/project/src/entry.ts
console.log(test_default);

================================================================================
TestTsConfigPathsTypeOnly
---------- /Users/user/project/out.js ----------
// Users/user/project/node_modules/fib/index.js
function fib(input) {
if (input < 2) {
return input;
}
return fib(input - 1) + fib(input - 2);
}

// Users/user/project/entry.ts
console.log(fib(10));

================================================================================
TestTsconfigImportsNotUsedAsValuesPreserve
---------- /Users/user/project/out.js ----------
Expand Down
8 changes: 8 additions & 0 deletions internal/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,10 @@ func (r resolverQuery) matchTSConfigPaths(tsConfigJSON *TSConfigJSON, path strin
r.debugLogs.addNote(fmt.Sprintf("Found an exact match for %q in \"paths\"", key))
}
for _, originalPath := range originalPaths {
if strings.HasSuffix(originalPath, ".d.ts") || strings.HasSuffix(strings.ToLower(originalPath), ".d.ts") {
continue
}

// Load the original path relative to the "baseUrl" from tsconfig.json
absoluteOriginalPath := originalPath
if !r.fs.IsAbs(originalPath) {
Expand Down Expand Up @@ -1543,6 +1547,10 @@ func (r resolverQuery) matchTSConfigPaths(tsConfigJSON *TSConfigJSON, path strin
matchedText := path[len(longestMatch.prefix) : len(path)-len(longestMatch.suffix)]
originalPath = strings.Replace(originalPath, "*", matchedText, 1)

if strings.HasSuffix(originalPath, ".d.ts") || strings.HasSuffix(strings.ToLower(originalPath), ".d.ts") {
continue
}

// Load the original path relative to the "baseUrl" from tsconfig.json
absoluteOriginalPath := originalPath
if !r.fs.IsAbs(originalPath) {
Expand Down

0 comments on commit 8602fd1

Please sign in to comment.