diff options
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/api/JSBundler.zig | 113 | ||||
-rw-r--r-- | src/bun.js/api/JSTranspiler.zig | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 3 | ||||
-rw-r--r-- | src/bun.js/builtins/js/BundlerPlugin.js | 4 |
4 files changed, 106 insertions, 16 deletions
diff --git a/src/bun.js/api/JSBundler.zig b/src/bun.js/api/JSBundler.zig index cccbca9db..55c224726 100644 --- a/src/bun.js/api/JSBundler.zig +++ b/src/bun.js/api/JSBundler.zig @@ -51,6 +51,7 @@ pub const JSBundler = struct { entry_points: bun.StringSet = bun.StringSet.init(bun.default_allocator), hot: bool = false, define: bun.StringMap = bun.StringMap.init(bun.default_allocator, true), + loaders: ?Api.LoaderMap = null, dir: OwnedString = OwnedString.initEmpty(bun.default_allocator), outdir: OwnedString = OwnedString.initEmpty(bun.default_allocator), serve: Serve = .{}, @@ -60,7 +61,6 @@ pub const JSBundler = struct { server_components: ServerComponents = ServerComponents{}, names: Names = .{}, - label: OwnedString = OwnedString.initEmpty(bun.default_allocator), external: bun.StringSet = bun.StringSet.init(bun.default_allocator), source_map: options.SourceMapOption = .none, public_path: OwnedString = OwnedString.initEmpty(bun.default_allocator), @@ -73,7 +73,6 @@ pub const JSBundler = struct { .external = bun.StringSet.init(allocator), .define = bun.StringMap.init(allocator, true), .dir = OwnedString.initEmpty(allocator), - .label = OwnedString.initEmpty(allocator), .outdir = OwnedString.initEmpty(allocator), .names = .{ .owned_entry_point = OwnedString.initEmpty(allocator), @@ -88,6 +87,20 @@ pub const JSBundler = struct { this.target = target; } + if (try config.getOptionalEnum(globalThis, "sourcemap", options.SourceMapOption)) |source_map| { + this.source_map = source_map; + } + + if (try config.getOptionalEnum(globalThis, "format", options.Format)) |format| { + switch (format) { + .esm => {}, + else => { + globalThis.throwInvalidArguments("Formats besides 'esm' are not implemented", .{}); + return error.JSException; + }, + } + } + // if (try config.getOptional(globalThis, "hot", bool)) |hot| { // this.hot = hot; // } @@ -150,17 +163,12 @@ pub const JSBundler = struct { } } - if (try config.getOptional(globalThis, "label", ZigString.Slice)) |slice| { - defer slice.deinit(); - this.label.appendSliceExact(slice.slice()) catch unreachable; - } - - if (try config.getOptional(globalThis, "dir", ZigString.Slice)) |slice| { - defer slice.deinit(); - this.dir.appendSliceExact(slice.slice()) catch unreachable; - } else { - this.dir.appendSliceExact(globalThis.bunVM().bundler.fs.top_level_dir) catch unreachable; - } + // if (try config.getOptional(globalThis, "dir", ZigString.Slice)) |slice| { + // defer slice.deinit(); + // this.dir.appendSliceExact(slice.slice()) catch unreachable; + // } else { + // this.dir.appendSliceExact(globalThis.bunVM().bundler.fs.top_level_dir) catch unreachable; + // } if (try config.getOptional(globalThis, "publicPath", ZigString.Slice)) |slice| { defer slice.deinit(); @@ -198,6 +206,84 @@ pub const JSBundler = struct { } } + if (try config.getObject(globalThis, "define")) |define| { + if (!define.isObject()) { + globalThis.throwInvalidArguments("define must be an object", .{}); + return error.JSException; + } + + var define_iter = JSC.JSPropertyIterator(.{ + .skip_empty_name = true, + .include_value = true, + }).init(globalThis, define.asObjectRef()); + defer define_iter.deinit(); + + while (define_iter.next()) |prop| { + const property_value = define_iter.value; + const value_type = property_value.jsType(); + + if (!value_type.isStringLike()) { + globalThis.throwInvalidArguments("define \"{s}\" must be a JSON string", .{prop}); + return error.JSException; + } + + var val = JSC.ZigString.init(""); + property_value.toZigString(&val, globalThis); + if (val.len == 0) { + val = JSC.ZigString.init("\"\""); + } + + try this.define.insert(prop.slice(), val.slice()); + } + } + + if (try config.getObject(globalThis, "loader")) |loaders| { + if (!loaders.isUndefinedOrNull()) { + if (!loaders.isObject()) { + globalThis.throwInvalidArguments("loader must be an object", .{}); + return error.JSException; + } + + var loader_iter = JSC.JSPropertyIterator(.{ + .skip_empty_name = true, + .include_value = true, + }).init(globalThis, loaders.asObjectRef()); + defer loader_iter.deinit(); + + var loader_names = try allocator.alloc(string, loader_iter.len); + var loader_values = try allocator.alloc(Api.Loader, loader_iter.len); + + while (loader_iter.next()) |prop| { + if (prop.len == 0 or prop.ptr[0] != '.') { + globalThis.throwInvalidArguments("loader property names must be file extensions, such as '.txt'", .{}); + return error.JSException; + } + + loader_names[loader_iter.i] = prop.slice(); + var property_value = loader_iter.value; + var value_type = property_value.jsType(); + if (!value_type.isStringLike()) { + globalThis.throwInvalidArguments("loader \"{s}\" must be a string", .{prop}); + return error.JSException; + } + + var val = JSC.ZigString.init(""); + property_value.toZigString(&val, globalThis); + if (options.Loader.fromString(val.slice())) |loader| { + loader_values[loader_iter.i] = loader.toAPI(); + } else { + globalThis.throwInvalidArguments("loader \"{s}\" is not a valid loader", .{val}); + return error.JSException; + } + } + + this.loaders = Api.LoaderMap{ + .extensions = loader_names, + .loaders = loader_values, + }; + } + } + if (try config.getArray(globalThis, "plugins")) |array| { var iter = array.arrayIterator(globalThis); while (iter.next()) |plugin| { @@ -355,7 +441,6 @@ pub const JSBundler = struct { self.serve.deinit(allocator); self.server_components.deinit(allocator); self.names.deinit(); - self.label.deinit(); self.outdir.deinit(); self.public_path.deinit(); } diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index f1b00f191..2ac6948d1 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -565,7 +565,7 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std } } else { var sourcemap = flag.toSlice(globalThis, allocator); - if (options.SourceMapOption.map.get(sourcemap.slice())) |source| { + if (options.SourceMapOption.Map.get(sourcemap.slice())) |source| { transpiler.transform.source_map = source.toAPI(); } else { JSC.throwInvalidArguments("sourcemap must be one of \"inline\", \"external\", or \"none\"", .{}, globalObject, exception); diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 12cc81118..71409da4f 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -4009,9 +4009,10 @@ pub const JSValue = enum(JSValueReprInt) { pub fn getOptionalEnum(this: JSValue, globalThis: *JSGlobalObject, comptime property_name: []const u8, comptime Enum: type) !?Enum { if (get(this, globalThis, property_name)) |prop| { + if (prop.isEmptyOrUndefinedOrNull()) + return null; return try toEnum(prop, globalThis, property_name, Enum); } - return null; } diff --git a/src/bun.js/builtins/js/BundlerPlugin.js b/src/bun.js/builtins/js/BundlerPlugin.js index 64c655bbe..ec8fee397 100644 --- a/src/bun.js/builtins/js/BundlerPlugin.js +++ b/src/bun.js/builtins/js/BundlerPlugin.js @@ -66,7 +66,9 @@ function runOnResolvePlugins( path: inputPath, importer, namespace: inputNamespace, + // resolveDir kind, + // pluginData }); while ( @@ -368,6 +370,8 @@ function runOnLoadPlugins(internalID, path, namespace, defaultLoaderId) { var result = callback({ path, namespace, + // suffix + // pluginData loader: defaultLoader, }); |