Skip to content

Commit

Permalink
Added returning of original path from Bun.build
Browse files Browse the repository at this point in the history
  • Loading branch information
BjornTheProgrammer committed Nov 21, 2024
1 parent 2283ed0 commit 81d0498
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1839,6 +1839,7 @@ declare module "bun" {
hash: string | null;
kind: "entry-point" | "chunk" | "asset" | "sourcemap" | "bytecode";
sourcemap: BuildArtifact | null;
sourcefile: string | null;
}

interface BuildOutput {
Expand Down
1 change: 1 addition & 0 deletions src/bun.js/api/JSBundler.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default [
loader: { getter: "getLoader", cache: true },
type: { getter: "getMimeType", cache: true },
kind: { getter: "getOutputKind", cache: true },
sourcefile: { getter: "getSourceFile", cache: true },
},
}),
];
39 changes: 39 additions & 0 deletions src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ pub const BuildArtifact = struct {
hash: u64 = std.math.maxInt(u64),
output_kind: OutputKind,
sourcemap: JSC.Strong = .{},
source_file: []const u8 = "",

pub const OutputKind = enum {
chunk,
Expand All @@ -1129,6 +1130,7 @@ pub const BuildArtifact = struct {
this.sourcemap.deinit();

bun.default_allocator.free(this.path);
bun.default_allocator.free(this.source_file);
}

pub fn getText(
Expand Down Expand Up @@ -1202,6 +1204,14 @@ pub const BuildArtifact = struct {
return ZigString.init(out).toJS(globalThis);
}

pub fn getSourceFile(
this: *BuildArtifact,
globalThis: *JSC.JSGlobalObject,
) JSValue {
if (this.source_file.len == 0) return JSC.JSValue.jsNull();
return ZigString.fromUTF8(this.source_file).toJS(globalThis);
}

pub fn getSize(this: *BuildArtifact, globalObject: *JSC.JSGlobalObject) JSValue {
return @call(bun.callmod_inline, Blob.getSize, .{ &this.blob, globalObject });
}
Expand Down Expand Up @@ -1324,6 +1334,35 @@ pub const BuildArtifact = struct {
);
}
}

if (this.output_kind == .@"entry-point") {
formatter.printComma(Writer, writer, enable_ansi_colors) catch unreachable;
try writer.writeAll("\n");
try formatter.writeIndent(Writer, writer);
try writer.writeAll(
comptime Output.prettyFmt(
"<r>sourcefile<r>: ",
enable_ansi_colors,
),
);

if (this.source_file.len != 0) {
try writer.print(
comptime Output.prettyFmt(
"<green>\"{s}\"<r>",
enable_ansi_colors,
),
.{this.source_file},
);
} else {
try writer.writeAll(
comptime Output.prettyFmt(
"<yellow>null<r>",
enable_ansi_colors,
),
);
}
}
}
try writer.writeAll("\n");
try formatter.writeIndent(Writer, writer);
Expand Down
15 changes: 6 additions & 9 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2190,14 +2190,8 @@ pub const OutputFile = struct {
) catch |err| {
Output.panic("error: Unable to create file blob: \"{s}\"", .{@errorName(err)});
};

var build_output = bun.new(JSC.API.BuildArtifact, .{
.blob = JSC.WebCore.Blob.initWithStore(file_blob, globalObject),
.hash = this.hash,
.loader = this.input_loader,
.output_kind = this.output_kind,
.path = bun.default_allocator.dupe(u8, copy.pathname) catch @panic("Failed to allocate path"),
});
const source_file = if (this.output_kind != .@"entry-point") "" else bun.default_allocator.dupe(u8, this.src_path.text) catch @panic("Failed to allocate source_path");
var build_output = bun.new(JSC.API.BuildArtifact, .{ .blob = JSC.WebCore.Blob.initWithStore(file_blob, globalObject), .hash = this.hash, .loader = this.input_loader, .output_kind = this.output_kind, .source_file = source_file, .path = bun.default_allocator.dupe(u8, copy.pathname) catch @panic("Failed to allocate path") });

break :brk build_output.toJS(globalObject);
},
Expand All @@ -2214,12 +2208,13 @@ pub const OutputFile = struct {
) catch |err| {
Output.panic("error: Unable to create file blob: \"{s}\"", .{@errorName(err)});
};

const source_file = if (this.output_kind != .@"entry-point") "" else bun.default_allocator.dupe(u8, this.src_path.text) catch @panic("Failed to allocate source_path");
build_output.* = JSC.API.BuildArtifact{
.blob = JSC.WebCore.Blob.initWithStore(file_blob, globalObject),
.hash = this.hash,
.loader = this.input_loader,
.output_kind = this.output_kind,
.source_file = source_file,
.path = bun.default_allocator.dupe(u8, path_to_use) catch @panic("Failed to allocate path"),
};

Expand All @@ -2237,11 +2232,13 @@ pub const OutputFile = struct {
blob.size = @as(JSC.WebCore.Blob.SizeType, @truncate(buffer.bytes.len));

var build_output = bun.default_allocator.create(JSC.API.BuildArtifact) catch @panic("Unable to allocate Artifact");
const source_file = if (this.output_kind != .@"entry-point") "" else bun.default_allocator.dupe(u8, this.src_path.text) catch @panic("Failed to allocate source_path");
build_output.* = JSC.API.BuildArtifact{
.blob = blob,
.hash = this.hash,
.loader = this.input_loader,
.output_kind = this.output_kind,
.source_file = source_file,
.path = owned_pathname orelse bun.default_allocator.dupe(u8, this.src_path.text) catch unreachable,
};
break :brk build_output.toJS(globalObject);
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
73 changes: 68 additions & 5 deletions test/bundler/bun-build-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ describe("Bun.build", () => {
`,
});

const entrypoint = join(dir, "a.css");
const build = await Bun.build({
entrypoints: [join(dir, "a.css")],
entrypoints: [entrypoint],
outdir: join(dir, "out"),
minify: true,
});

expect(build.outputs).toHaveLength(2);
expect(build.outputs[0].kind).toBe("entry-point");
expect(build.outputs[0].sourcefile).toEqual(entrypoint);
expect(await build.outputs[0].text()).not.toEqualIgnoringWhitespace(".hello{color:#00f}.hi{color:red}\n");
});

Expand All @@ -73,15 +75,17 @@ describe("Bun.build", () => {
},
});

const entrypoint = join(dir, "index.ts");
const build = await Bun.build({
entrypoints: [join(dir, "index.ts")],
entrypoints: [entrypoint],
outdir: join(dir, "out"),
target: "bun",
bytecode: true,
});

expect(build.outputs).toHaveLength(2);
expect(build.outputs[0].kind).toBe("entry-point");
expect(build.outputs[0].sourcefile).toEqual(entrypoint);
expect(build.outputs[1].kind).toBe("bytecode");
expect([build.outputs[0].path]).toRun("world\n");
});
Expand Down Expand Up @@ -235,8 +239,10 @@ describe("Bun.build", () => {
const outdir = tempDirWithFiles("build-artifact-properties", {
"package.json": `{}`,
});

const entrypoint = join(import.meta.dir, "./fixtures/trivial/index.js");
const x = await Bun.build({
entrypoints: [join(import.meta.dir, "./fixtures/trivial/index.js")],
entrypoints: [entrypoint],
outdir,
});
const [blob] = x.outputs;
Expand All @@ -249,6 +255,7 @@ describe("Bun.build", () => {
expect(blob.kind).toBe("entry-point");
expect(blob.loader).toBe("jsx");
expect(blob.sourcemap).toBe(null);
expect(blob.sourcefile).toEqual(entrypoint);
Bun.gc(true);
});

Expand All @@ -257,8 +264,9 @@ describe("Bun.build", () => {
const outdir = tempDirWithFiles("build-artifact-properties-entry-naming", {
"package.json": `{}`,
});
const entrypoint = join(import.meta.dir, "./fixtures/trivial/index.js");
const x = await Bun.build({
entrypoints: [join(import.meta.dir, "./fixtures/trivial/index.js")],
entrypoints: [entrypoint],
naming: {
entry: "hello",
},
Expand All @@ -272,6 +280,7 @@ describe("Bun.build", () => {
expect(blob.hash).toBeTruthy();
expect(blob.hash).toMatchSnapshot("hash");
expect(blob.kind).toBe("entry-point");
expect(blob.sourcefile).toEqual(entrypoint);
expect(blob.loader).toBe("jsx");
expect(blob.sourcemap).toBe(null);
Bun.gc(true);
Expand All @@ -282,8 +291,9 @@ describe("Bun.build", () => {
const outdir = tempDirWithFiles("build-artifact-properties-sourcemap", {
"package.json": `{}`,
});
const entrypoint = join(import.meta.dir, "./fixtures/trivial/index.js");
const x = await Bun.build({
entrypoints: [join(import.meta.dir, "./fixtures/trivial/index.js")],
entrypoints: [entrypoint],
sourcemap: "external",
outdir,
});
Expand All @@ -295,6 +305,7 @@ describe("Bun.build", () => {
expect(blob.hash).toMatchSnapshot("hash index.js");
expect(blob.kind).toBe("entry-point");
expect(blob.loader).toBe("jsx");
expect(blob.sourcefile).toEqual(entrypoint);
expect(blob.sourcemap).toBe(map);

expect(map.type).toBe("application/json;charset=utf-8");
Expand All @@ -303,6 +314,7 @@ describe("Bun.build", () => {
expect(map.hash).toBeTruthy();
expect(map.hash).toMatchSnapshot("hash index.js.map");
expect(map.kind).toBe("sourcemap");
expect(map.sourcefile).toBe(null);
expect(map.loader).toBe("file");
expect(map.sourcemap).toBe(null);
Bun.gc(true);
Expand Down Expand Up @@ -546,4 +558,55 @@ describe("Bun.build", () => {

expect(await bundle.outputs[0].text()).toBe("var o=/*@__PURE__*/console.log(1);export{o as OUT};\n");
});

test("multiple entries have sourcefiles", async () => {
Bun.gc(true);
const fixture = tempDirWithFiles("build-entries-have-sourcefiles", {
"entry1.ts": `
import { bar } from './bar'
export const entry1 = () => {
console.log('FOO')
bar()
}
`,
"entry2.ts": `
import { bar } from './bar'
export const entry1 = () => {
console.log('FOO')
bar()
}
`,
"bar.ts": `
export const bar = () => {
console.log('BAR')
}
`,
});

const entry1 = join(fixture, "entry1.ts");
const entry2 = join(fixture, "entry2.ts");
const build = await Bun.build({
entrypoints: [entry1, entry2],
splitting: true,
minify: false,
naming: "[dir]/[name]-[hash].[ext]",
sourcemap: "external",
});

const entry1Output = [...build.outputs].find(item => item.sourcefile == entry1);
const entry2Output = [...build.outputs].find(item => item.sourcefile == entry2);

expect(entry1Output).toBeObject();
expect(entry2Output).toBeObject();

expect(entry1Output?.kind).toBe("entry-point");
expect(entry2Output?.kind).toBe("entry-point");

for (const output of build.outputs) {
if (output.kind != "entry-point") {
expect(output?.sourcefile).toBe(null);
}
}
Bun.gc(true);
});
});

0 comments on commit 81d0498

Please sign in to comment.