Skip to content
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

Ignore type-only entries in paths #2075

Merged
merged 3 commits into from
Mar 3, 2022
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
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