diff options
author | 2023-05-12 07:33:07 -0700 | |
---|---|---|
committer | 2023-05-12 07:33:07 -0700 | |
commit | 4d5d0d075a597400175d410db8b8dcfcc8016623 (patch) | |
tree | 7d71234b4438bd83641d52e33d61b6040b205e62 /src/bun.js/api/JSBundler.zig | |
parent | c815716899d8ace52b9c6ebd494735a7f8de9bf8 (diff) | |
download | bun-4d5d0d075a597400175d410db8b8dcfcc8016623.tar.gz bun-4d5d0d075a597400175d410db8b8dcfcc8016623.tar.zst bun-4d5d0d075a597400175d410db8b8dcfcc8016623.zip |
`root` bundle option (#2859)
* handle multiple output files and `[dir]`
* get the realpath of `root_dir`
* duplicate output paths
* add `rootdir` to `JSBundler`
* use realpath of input file
* add tests for naming and root
---------
Co-authored-by: Dave Caruso <me@paperdave.net>
Diffstat (limited to 'src/bun.js/api/JSBundler.zig')
-rw-r--r-- | src/bun.js/api/JSBundler.zig | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index f17fe99d1..c95c359f1 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -11,6 +11,7 @@ const js = JSC.C; const WebCore = @import("../webcore/response.zig"); const Bundler = bun.bundler; const options = @import("../../options.zig"); +const resolve_path = @import("../../resolver/resolve_path.zig"); const VirtualMachine = JavaScript.VirtualMachine; const ScriptSrcStream = std.io.FixedBufferStream([]u8); const ZigString = JSC.ZigString; @@ -54,6 +55,7 @@ pub const JSBundler = struct { loaders: ?Api.LoaderMap = null, dir: OwnedString = OwnedString.initEmpty(bun.default_allocator), outdir: OwnedString = OwnedString.initEmpty(bun.default_allocator), + rootdir: OwnedString = OwnedString.initEmpty(bun.default_allocator), serve: Serve = .{}, jsx: options.JSX.Pragma = .{}, code_splitting: bool = false, @@ -74,6 +76,7 @@ pub const JSBundler = struct { .define = bun.StringMap.init(allocator, true), .dir = OwnedString.initEmpty(allocator), .outdir = OwnedString.initEmpty(allocator), + .rootdir = OwnedString.initEmpty(allocator), .names = .{ .owned_entry_point = OwnedString.initEmpty(allocator), .owned_chunk = OwnedString.initEmpty(allocator), @@ -253,6 +256,33 @@ pub const JSBundler = struct { return error.JSException; } + { + const path: ZigString.Slice = brk: { + if (try config.getOptional(globalThis, "root", ZigString.Slice)) |slice| { + break :brk slice; + } + + const entry_points = this.entry_points.keys(); + + if (entry_points.len == 1) { + break :brk ZigString.Slice.fromUTF8NeverFree(std.fs.path.dirname(entry_points[0]) orelse "."); + } + + break :brk ZigString.Slice.fromUTF8NeverFree(resolve_path.getIfExistsLongestCommonPath(entry_points) orelse "."); + }; + + defer path.deinit(); + + var dir = std.fs.cwd().openDir(path.slice(), .{}) catch |err| { + globalThis.throwPretty("{s}: failed to open root directory: {s}", .{ @errorName(err), path.slice() }); + return error.JSException; + }; + defer dir.close(); + + var rootdir_buf: [bun.MAX_PATH_BYTES]u8 = undefined; + this.rootdir.appendSliceExact(try bun.getFdPath(dir.fd, &rootdir_buf)) catch unreachable; + } + if (try config.getArray(globalThis, "external")) |externals| { var iter = externals.arrayIterator(globalThis); while (iter.next()) |entry_point| { @@ -455,6 +485,7 @@ pub const JSBundler = struct { } self.names.deinit(); self.outdir.deinit(); + self.rootdir.deinit(); self.public_path.deinit(); } }; |