From 8602fd1f39d838b7405377572ea36f6ab4652897 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Thu, 3 Mar 2022 10:57:26 +0800 Subject: [PATCH] Ignore type-only entries in paths (#2075) --- internal/bundler/bundler_tsconfig_test.go | 38 +++++++++++++++++++ .../bundler/snapshots/snapshots_tsconfig.txt | 14 +++++++ internal/resolver/resolver.go | 8 ++++ 3 files changed, 60 insertions(+) diff --git a/internal/bundler/bundler_tsconfig_test.go b/internal/bundler/bundler_tsconfig_test.go index d2a6e122d12..7ce2fdc5150 100644 --- a/internal/bundler/bundler_tsconfig_test.go +++ b/internal/bundler/bundler_tsconfig_test.go @@ -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{ diff --git a/internal/bundler/snapshots/snapshots_tsconfig.txt b/internal/bundler/snapshots/snapshots_tsconfig.txt index d4382766513..1a55e9e0034 100644 --- a/internal/bundler/snapshots/snapshots_tsconfig.txt +++ b/internal/bundler/snapshots/snapshots_tsconfig.txt @@ -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 ---------- diff --git a/internal/resolver/resolver.go b/internal/resolver/resolver.go index a931d15f812..54e1d14ff4f 100644 --- a/internal/resolver/resolver.go +++ b/internal/resolver/resolver.go @@ -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) { @@ -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) {