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

fix(bundler) Typescript paths with packages external fix #16163

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions src/resolver/resolver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,6 @@ pub const Resolver = struct {
}

pub fn isExternalPattern(r: *ThisResolver, import_path: string) bool {
if (r.opts.packages == .external and isPackagePath(import_path)) {
return true;
}
for (r.opts.external.patterns) |pattern| {
if (import_path.len >= pattern.prefix.len + pattern.suffix.len and (strings.startsWith(
import_path,
Expand Down Expand Up @@ -902,6 +899,10 @@ pub const Resolver = struct {

r.flushDebugLogs(.success) catch {};
result.import_kind = kind;

if (r.opts.packages == .external and result.is_from_node_modules) {
result.is_external = true;
}
return .{ .success = result.* };
},
.failure => |e| {
Expand Down Expand Up @@ -1127,6 +1128,7 @@ pub const Resolver = struct {
.package_json = res.package_json,
.dirname_fd = res.dirname_fd,
.file_fd = res.file_fd,
.is_from_node_modules = res.is_node_module,
.jsx = tsconfig.mergeJSX(result.jsx),
},
};
Expand Down Expand Up @@ -1161,6 +1163,7 @@ pub const Resolver = struct {
.path_pair = entry.path_pair,
.diff_case = entry.diff_case,
.package_json = entry.package_json,
.is_from_node_modules = entry.is_node_module,
.file_fd = entry.file_fd,
.jsx = r.opts.jsx,
},
Expand Down Expand Up @@ -3598,6 +3601,7 @@ pub const Resolver = struct {
.dirname_fd = file.dirname_fd,
.package_json = package_json,
.file_fd = file.file_fd,
.is_node_module = true,
};
}
}
Expand Down Expand Up @@ -3631,6 +3635,8 @@ pub const Resolver = struct {
}) orelse return null;
var package_json: ?*PackageJSON = null;

const is_node_module_folder = dir_info.isInsideNodeModules();

// Try using the main field(s) from "package.json"
if (dir_info.package_json) |pkg_json| {
package_json = pkg_json;
Expand Down Expand Up @@ -3705,6 +3711,7 @@ pub const Resolver = struct {
.dirname_fd = _result.dirname_fd,
.package_json = package_json,
.file_fd = auto_main_result.file_fd,
.is_node_module = is_node_module_folder,
};
} else {
if (r.debug_logs) |*debug| {
Expand All @@ -3715,12 +3722,14 @@ pub const Resolver = struct {
});
}
var _auto_main_result = auto_main_result;
_auto_main_result.is_node_module = is_node_module_folder;
_auto_main_result.package_json = package_json;
return _auto_main_result;
}
}
}

_result.is_node_module = is_node_module_folder;
_result.package_json = _result.package_json orelse package_json;
return _result;
}
Expand All @@ -3731,6 +3740,7 @@ pub const Resolver = struct {
if (r.loadAsIndexWithBrowserRemapping(dir_info, path, extension_order)) |res| {
var res_copy = res;
res_copy.package_json = res.package_json orelse package_json;
res_copy.is_node_module = is_node_module_folder;
return res_copy;
}

Expand Down
43 changes: 36 additions & 7 deletions test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,10 +1344,6 @@ describe("bundler", () => {
import { a } from "foo";
console.log(a);
`,
},
packages: "external",
target: "bun",
runtimeFiles: {
"/node_modules/foo/index.js": `export const a = "Hello World";`,
"/node_modules/foo/package.json": /* json */ `
{
Expand All @@ -1357,10 +1353,43 @@ describe("bundler", () => {
}
`,
},
run: {
stdout: `
Hello World
packages: "external",
target: "bun",
onAfterBundle(api) {
api.expectFile("/out.js").not.toInclude(`Hello World`);
},
});
itBundled("edgecase/PackageExternalRespectTSPathAliases", {
files: {
"/src/entry.ts": /* ts */ `
import { value } from "alias/foo";
import { other } from "@scope/bar";
import { nested } from "deep/path";
import { absolute } from "abs/path";
console.log(value, other, nested, absolute);
`,
"/src/actual/foo.ts": `export const value = "foo_fake_value";`,
"/src/lib/bar.ts": `export const other = "bar_fake_value";`,
"/src/nested/deep/file.ts": `export const nested = "nested_fake_value";`,
"/src/absolute.ts": `export const absolute = "absolute_fake_value";`,
"/src/tsconfig.json": /* json */ `{
"compilerOptions": {
"baseUrl": "\${configDir}",
"paths": {
"alias/*": ["actual/*"],
"@scope/*": ["lib/*"],
"deep/path": ["nested/deep/file.ts"],
"abs/*": ["\${configDir}/absolute.ts"]
}
}
}`,
},
packages: "external",
onAfterBundle(api) {
api.expectFile("/out.js").toInclude(`foo_fake_value`);
api.expectFile("/out.js").toInclude(`bar_fake_value`);
api.expectFile("/out.js").toInclude(`nested_fake_value`);
api.expectFile("/out.js").toInclude(`absolute_fake_value`);
},
});
itBundled("edgecase/EntrypointWithoutPrefixSlashOrDotIsNotConsideredExternal#12734", {
Expand Down