Skip to content

Commit

Permalink
allow bundledDependencies: true in bun pm pack (#16382)
Browse files Browse the repository at this point in the history
Co-authored-by: Dylan Conway <[email protected]>
  • Loading branch information
RiskyMH and dylan-conway authored Jan 14, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 6b197d8 commit 5c57930
Showing 2 changed files with 108 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/cli/pack_command.zig
Original file line number Diff line number Diff line change
@@ -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| {
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 a boolean or an array of strings", .{field});
Global.crash();
}

73 changes: 73 additions & 0 deletions test/cli/install/bun-pack.test.ts
Original file line number Diff line number Diff line change
@@ -602,6 +602,79 @@ 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(`invalid bundledDependencies value should throw`, async () => {
await Promise.all([
write(
join(packageDir, "package.json"),
JSON.stringify({
name: "pack-bundled",
version: "4.4.4",
bundledDependencies: "a",
}),
),
]);

const { stdout, stderr, exited } = Bun.spawn({
cmd: [bunExe(), "pm", "pack"],
cwd: packageDir,
stdout: "pipe",
stderr: "pipe",
stdin: "ignore",
env: bunEnv,
});

const err = await Bun.readableStreamToText(stderr);
expect(err).toContain("error:");
expect(err).toContain("to be a boolean or an array of strings");
expect(err).not.toContain("warning:");
expect(err).not.toContain("failed");
expect(err).not.toContain("panic:");

const exitCode = await exited;
expect(exitCode).toBe(1);
});

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

0 comments on commit 5c57930

Please sign in to comment.