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

allow bundledDependencies: true in bun pm pack #16382

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 35 additions & 10 deletions src/cli/pack_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -835,22 +835,47 @@ pub const PackCommand = struct {
const bundled_deps = json.get(field) orelse return null;

invalid_field: {
var iter = bundled_deps.asArray() orelse switch (bundled_deps.data) {
.e_array => return .{},
switch (bundled_deps.data) {
.e_array => {
var iter = bundled_deps.asArray() orelse return .{};

while (iter.next()) |bundled_dep_item| {
const bundled_dep = try bundled_dep_item.asStringCloned(allocator) orelse break :invalid_field;
try deps.append(allocator, .{
.name = bundled_dep,
.from_root_package_json = true,
});
}
},
.e_boolean => {
const b = bundled_deps.asBool() orelse return .{};
if (!b == true) return .{};

if (json.get("dependencies")) |dependencies_expr| {
RiskyMH marked this conversation as resolved.
Show resolved Hide resolved
switch (dependencies_expr.data) {
.e_object => |dependencies| {
for (dependencies.properties.slice()) |*dependency| {
if (dependency.key == null) continue;
if (dependency.value == null) continue;

const bundled_dep = try dependency.key.?.asStringCloned(allocator) orelse break :invalid_field;
try deps.append(allocator, .{
.name = bundled_dep,
.from_root_package_json = true,
});
}
},
else => {},
}
}
},
else => break :invalid_field,
};
while (iter.next()) |bundled_dep_item| {
const bundled_dep = try bundled_dep_item.asStringCloned(allocator) orelse break :invalid_field;
try deps.append(allocator, .{
.name = bundled_dep,
.from_root_package_json = true,
});
}

return deps;
}

Output.errGeneric("expected `{s}` to be an array of strings", .{field});
Output.errGeneric("expected `{s}` to be an boolean or array of strings", .{field});
RiskyMH marked this conversation as resolved.
Show resolved Hide resolved
Global.crash();
}

Expand Down
41 changes: 41 additions & 0 deletions test/cli/install/bun-pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,47 @@ describe("bundledDependnecies", () => {
});
}

test(`basic (bundledDependencies: true)`, async () => {
await Promise.all([
write(
join(packageDir, "package.json"),
JSON.stringify({
name: "pack-bundled",
version: "4.4.4",
dependencies: {
"dep1": "1.1.1",
},
devDependencies: {
"dep2": "1.1.1",
},
bundledDependencies: true,
}),
),
write(
join(packageDir, "node_modules", "dep1", "package.json"),
JSON.stringify({
name: "dep1",
version: "1.1.1",
}),
),
write(
join(packageDir, "node_modules", "dep2", "package.json"),
JSON.stringify({
name: "dep2",
version: "1.1.1",
}),
),
]);

await pack(packageDir, bunEnv);

const tarball = readTarball(join(packageDir, "pack-bundled-4.4.4.tgz"));
expect(tarball.entries).toMatchObject([
{ "pathname": "package/package.json" },
{ "pathname": "package/node_modules/dep1/package.json" },
]);
});

test("resolve dep of bundled dep", async () => {
// Test that a bundled dep can have it's dependencies resolved without
// needing to add them to `bundledDependencies`. Also test that only
Expand Down
Loading