Skip to content

Commit

Permalink
[bunfig] Implement config file format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jan 27, 2022
1 parent 9f5a170 commit 3273321
Show file tree
Hide file tree
Showing 20 changed files with 667 additions and 156 deletions.
2 changes: 2 additions & 0 deletions src/analytics/analytics_thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub const Features = struct {
pub var origin = false;
pub var external = false;
pub var fetch = false;
pub var bunfig = false;

pub fn formatter() Formatter {
return Formatter{};
Expand All @@ -77,6 +78,7 @@ pub const Features = struct {
"origin",
"external",
"fetch",
"bunfig",
};
inline for (fields) |field| {
if (@field(Features, field)) {
Expand Down
8 changes: 6 additions & 2 deletions src/api/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions src/api/schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/api/schema.peechy
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ message TransformOptions {
bool disable_hmr = 24;

uint16 port = 25;
MessageLevel logLevel = 26;

}

struct FileHandle {
Expand Down Expand Up @@ -379,7 +381,8 @@ enum MessageLevel {
err = 1;
warn =2;
note = 3;
debug = 4;
info = 4;
debug = 5;
}

struct Location {
Expand Down
13 changes: 13 additions & 0 deletions src/api/schema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,9 @@ pub const Api = struct {
/// port
port: ?u16 = null,

/// logLevel
log_level: ?MessageLevel = null,

pub fn decode(reader: anytype) anyerror!TransformOptions {
var this = std.mem.zeroes(TransformOptions);

Expand Down Expand Up @@ -1819,6 +1822,9 @@ pub const Api = struct {
25 => {
this.port = try reader.readValue(u16);
},
26 => {
this.log_level = try reader.readValue(MessageLevel);
},
else => {
return error.InvalidMessage;
},
Expand Down Expand Up @@ -1928,6 +1934,10 @@ pub const Api = struct {
try writer.writeFieldID(25);
try writer.writeInt(port);
}
if (this.log_level) |log_level| {
try writer.writeFieldID(26);
try writer.writeEnum(log_level);
}
try writer.endMessage();
}
};
Expand Down Expand Up @@ -2104,6 +2114,9 @@ pub const Api = struct {
/// note
note,

/// info
info,

/// debug
debug,

Expand Down
5 changes: 5 additions & 0 deletions src/bun_js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub const Run = struct {
ctx: Command.Context,
vm: *VirtualMachine,
entry_path: string,

pub fn boot(ctx: Command.Context, file: std.fs.File, entry_path: string) !void {
@import("javascript/jsc/javascript_core_c_api.zig").JSCInitialize();

Expand All @@ -53,6 +54,10 @@ pub const Run = struct {

run.vm.argv = ctx.positionals;

if (ctx.debug.macros) |macros| {
run.vm.bundler.options.macro_remap = macros;
}

run.vm.bundler.configureRouter(false) catch {
if (Output.enable_ansi_colors_stderr) {
run.vm.log.printForLogLevelWithEnableAnsiColors(Output.errorWriter(), true) catch {};
Expand Down
55 changes: 39 additions & 16 deletions src/bundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const js_ast = @import("js_ast.zig");
const linker = @import("linker.zig");
const Ref = @import("ast/base.zig").Ref;
const Define = @import("defines.zig").Define;
const DebugOptions = @import("./cli.zig").Command.DebugOptions;

const panicky = @import("panic_handler.zig");
const Fs = @import("fs.zig");
Expand Down Expand Up @@ -692,6 +693,7 @@ pub const Bundler = struct {

always_bundled_package_hashes: []u32 = &[_]u32{},
always_bundled_package_jsons: []*const PackageJSON = &.{},
package_bundle_map: options.BundlePackage.Map = options.BundlePackage.Map{},

const U32Map = std.AutoHashMap(u32, u32);
pub const current_version: u32 = 1;
Expand Down Expand Up @@ -777,6 +779,7 @@ pub const Bundler = struct {
route_config: ?Api.LoadedRouteConfig,
destination: [*:0]const u8,
estimated_input_lines_of_code: *usize,
package_bundle_map: options.BundlePackage.Map,
) !?Api.JavascriptBundleContainer {
_ = try bundler.fs.fs.openTmpDir();
var tmpname_buf: [64]u8 = undefined;
Expand Down Expand Up @@ -814,6 +817,7 @@ pub const Bundler = struct {
.package_list_map = std.AutoHashMap(u64, u32).init(allocator),
.pool = undefined,
.write_lock = Lock.init(),
.package_bundle_map = package_bundle_map,
};
// dist/index.js appears more common than /index.js
// but this means we can store both "dist/index.js" and "index.js" in one.
Expand Down Expand Up @@ -850,15 +854,26 @@ pub const Bundler = struct {
break :brk read_dir.package_json.?;
};
Analytics.setProjectID(std.fs.path.dirname(root_package_json.source.path.text) orelse "/", root_package_json.name);
Analytics.Features.macros = Analytics.Features.macros or root_package_json.macros.count() > 0;
if (bundler.macro_context) |macro_ctx| {
Analytics.Features.macros = macro_ctx.remap.count() > 0;
}

const bundle_keys = package_bundle_map.keys();
const do_always_bundle = package_bundle_map.values();
var always_bundle_count: u32 = 0;
for (do_always_bundle) |always| {
always_bundle_count += @as(u32, @boolToInt(always == .always));
}

if (root_package_json.always_bundle.len > 0) {
if (always_bundle_count > 0) {
Analytics.Features.always_bundle = true;
var always_bundled_package_jsons = bundler.allocator.alloc(*PackageJSON, root_package_json.always_bundle.len) catch unreachable;
var always_bundled_package_hashes = bundler.allocator.alloc(u32, root_package_json.always_bundle.len) catch unreachable;
var always_bundled_package_jsons = bundler.allocator.alloc(*PackageJSON, always_bundle_count) catch unreachable;
var always_bundled_package_hashes = bundler.allocator.alloc(u32, always_bundle_count) catch unreachable;
var i: u16 = 0;

inner: for (root_package_json.always_bundle) |name| {
inner: for (bundle_keys) |name, k| {
if (do_always_bundle[k] != .always) continue;

std.mem.copy(u8, &tmp_buildfile_buf, name);
std.mem.copy(u8, tmp_buildfile_buf[name.len..], "/package.json");
const package_json_import = tmp_buildfile_buf[0 .. name.len + "/package.json".len];
Expand Down Expand Up @@ -1294,6 +1309,10 @@ pub const Bundler = struct {

if (resolve_result.package_json) |pkg_| {
var pkg: *const PackageJSON = pkg_;
if (this.package_bundle_map.get(pkg.name)) |result| {
if (result == .never) return null;
}

if (std.mem.indexOfScalar(u32, this.always_bundled_package_hashes, pkg.hash)) |pkg_i| {
pkg = this.always_bundled_package_jsons[pkg_i];
const key_path_source_dir = pkg.source.key_path.sourceDir();
Expand Down Expand Up @@ -1507,14 +1526,18 @@ pub const Bundler = struct {

var shared_buffer = &worker.data.shared_buffer;
var scan_pass_result = &worker.data.scan_pass_result;
var file_path = (resolve.pathConst() orelse unreachable).*;

const is_from_node_modules = resolve.isLikelyNodeModule() or brk: {
if (resolve.package_json) |pkg| {
break :brk std.mem.indexOfScalar(u32, this.always_bundled_package_hashes, pkg.hash) != null;
const add_to_bundle = brk: {
if (resolve.package_json) |package_json| {
if (this.package_bundle_map.get(package_json.name)) |result| {
break :brk result == .always;
}
}
break :brk false;

break :brk resolve.isLikelyNodeModule();
};
var file_path = (resolve.pathConst() orelse unreachable).*;

const source_dir = file_path.sourceDir();
const loader = bundler.options.loader(file_path.name.ext);
const platform = bundler.options.platform;
Expand All @@ -1525,7 +1548,7 @@ pub const Bundler = struct {
var log = worker.data.log;

// If we're in a node_module, build that almost normally
if (is_from_node_modules) {
if (add_to_bundle) {
var code_offset: u32 = 0;

const module_data = BundledModuleData.getForceBundleForMain(this, &resolve) orelse {
Expand Down Expand Up @@ -1625,7 +1648,7 @@ pub const Bundler = struct {
opts.enable_bundling = true;
opts.warn_about_unbundled_modules = false;
opts.macro_context = &worker.data.macro_context;
opts.macro_context.remap = package.macros;

ast = (bundler.resolver.caches.js.parse(
bundler.allocator,
opts,
Expand Down Expand Up @@ -2024,7 +2047,6 @@ pub const Bundler = struct {
jsx.parse = loader.isJSX();
var opts = js_parser.Parser.Options.init(jsx, loader);
opts.macro_context = &worker.data.macro_context;
opts.macro_context.remap = resolve.getMacroRemappings();

try bundler.resolver.caches.js.scan(
bundler.allocator,
Expand Down Expand Up @@ -2303,7 +2325,7 @@ pub const Bundler = struct {
.dirname_fd = resolve_result.dirname_fd,
.file_descriptor = file_descriptor,
.file_hash = filepath_hash,
.macro_remappings = resolve_result.getMacroRemappings(),
.macro_remappings = bundler.options.macro_remap,
.jsx = resolve_result.jsx,
},
client_entry_point,
Expand Down Expand Up @@ -2416,7 +2438,7 @@ pub const Bundler = struct {
.dirname_fd = resolve_result.dirname_fd,
.file_descriptor = null,
.file_hash = null,
.macro_remappings = resolve_result.getMacroRemappings(),
.macro_remappings = bundler.options.macro_remap,
.jsx = resolve_result.jsx,
},
client_entry_point_,
Expand Down Expand Up @@ -2720,7 +2742,7 @@ pub const Bundler = struct {
opts.features.top_level_await = true;

opts.macro_context = &bundler.macro_context.?;
opts.macro_context.remap = this_parse.macro_remappings;

opts.features.is_macro_runtime = bundler.options.platform == .bun_macro;

const value = (bundler.resolver.caches.js.parse(
Expand Down Expand Up @@ -2942,6 +2964,7 @@ pub const Bundler = struct {
bundler.configureLinker();
try bundler.configureRouter(false);
try bundler.configureDefines();
bundler.macro_context = js_ast.Macro.MacroContext.init(&bundler);

var skip_normalize = false;
var load_from_routes = false;
Expand Down
Loading

0 comments on commit 3273321

Please sign in to comment.