Skip to content

Commit

Permalink
a few batched changes
Browse files Browse the repository at this point in the history
- print "DEV" when running index.html
- tla check error message fixes
- tidy up internal docs
  • Loading branch information
paperclover committed Feb 3, 2025
1 parent 795ae51 commit 523bf96
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/bun.js/api/JSBundler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub const JSBundler = struct {
rootdir: OwnedString = OwnedString.initEmpty(bun.default_allocator),
serve: Serve = .{},
jsx: options.JSX.Pragma = .{},
force_development: bool = false,
force_node_env: options.BundleOptions.ForceNodeEnv = .unspecified,
code_splitting: bool = false,
minify: Minify = .{},
no_macros: bool = false,
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/api/server/HTMLBundle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub const HTMLBundleRoute = struct {
config.define.put("process.env.NODE_ENV", "\"production\"") catch bun.outOfMemory();
config.jsx.development = false;
} else {
config.force_development = true;
config.force_node_env = .development;
config.jsx.development = true;
}

Expand Down
32 changes: 20 additions & 12 deletions src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,11 @@ pub const BundleV2 = struct {
task.known_target = target;
{
const bundler = this.bundlerForTarget(target);
task.jsx.development = bundler.options.force_development orelse bundler.options.jsx.development;
task.jsx.development = switch (bundler.options.force_node_env) {
.development => true,
.production => false,
.unspecified => bundler.options.jsx.development,
};
}

// Handle onLoad plugins as entry points
Expand Down Expand Up @@ -1756,8 +1760,8 @@ pub const BundleV2 = struct {
);
transpiler.options.env.behavior = config.env_behavior;
transpiler.options.env.prefix = config.env_prefix.slice();
if (config.force_development) {
transpiler.options.force_development = true;
if (config.force_node_env != .unspecified) {
transpiler.options.force_node_env = config.force_node_env;
}

transpiler.options.entry_points = config.entry_points.keys();
Expand Down Expand Up @@ -2881,11 +2885,11 @@ pub const BundleV2 = struct {
resolve_task.secondary_path_for_commonjs_interop = secondary_path_to_copy;
resolve_task.known_target = target;
resolve_task.jsx = resolve_result.jsx;

{
const bundler = this.bundlerForTarget(target);
resolve_task.jsx.development = bundler.options.force_development orelse bundler.options.jsx.development;
}
resolve_task.jsx.development = switch (transpiler.options.force_node_env) {
.development => true,
.production => false,
.unspecified => transpiler.options.jsx.development,
};

// Figure out the loader.
{
Expand Down Expand Up @@ -3552,7 +3556,7 @@ pub const ParseTask = struct {
};
};

const debug = Output.scoped(.ParseTask, false);
const debug = Output.scoped(.ParseTask, true);

pub fn init(resolve_result: *const _resolver.Result, source_index: Index, ctx: *BundleV2) ParseTask {
return .{
Expand Down Expand Up @@ -7555,7 +7559,7 @@ pub const LinkerContext = struct {
continue;
}

_ = this.validateTLA(id, tla_keywords, tla_checks, input_files, import_records, flags);
_ = this.validateTLA(id, tla_keywords, tla_checks, input_files, import_records, flags, import_records_list);

for (import_records) |record| {
if (!record.source_index.isValid()) {
Expand Down Expand Up @@ -11037,6 +11041,7 @@ pub const LinkerContext = struct {
input_files: []Logger.Source,
import_records: []ImportRecord,
meta_flags: []JSMeta.Flags,
ast_import_records: []bun.BabyList(ImportRecord),
) js_ast.TlaCheck {
var result_tla_check: *js_ast.TlaCheck = &tla_checks[source_index];

Expand All @@ -11048,7 +11053,7 @@ pub const LinkerContext = struct {

for (import_records, 0..) |record, import_record_index| {
if (Index.isValid(record.source_index) and (record.kind == .require or record.kind == .stmt)) {
const parent = c.validateTLA(record.source_index.get(), tla_keywords, tla_checks, input_files, import_records, meta_flags);
const parent = c.validateTLA(record.source_index.get(), tla_keywords, tla_checks, input_files, import_records, meta_flags, ast_import_records);
if (Index.isInvalid(Index.init(parent.parent))) {
continue;
}
Expand All @@ -11075,9 +11080,11 @@ pub const LinkerContext = struct {
const parent_source_index = other_source_index;

if (parent_result_tla_keyword.len > 0) {
tla_pretty_path = input_files[other_source_index].path.pretty;
const source = input_files[other_source_index];
tla_pretty_path = source.path.pretty;
notes.append(Logger.Data{
.text = std.fmt.allocPrint(c.allocator, "The top-level await in {s} is here:", .{tla_pretty_path}) catch bun.outOfMemory(),
.location = .initOrNull(&source, parent_result_tla_keyword),
}) catch bun.outOfMemory();
break;
}
Expand All @@ -11096,6 +11103,7 @@ pub const LinkerContext = struct {
input_files[parent_source_index].path.pretty,
input_files[other_source_index].path.pretty,
}) catch bun.outOfMemory(),
.location = .initOrNull(&input_files[parent_source_index], ast_import_records[parent_source_index].slice()[tla_checks[parent_source_index].import_record_index].range),
}) catch bun.outOfMemory();
}

Expand Down
8 changes: 7 additions & 1 deletion src/crash_handler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ pub fn crashHandler(
var trace_buf: std.builtin.StackTrace = undefined;

// If a trace was not provided, compute one now
const trace = error_return_trace orelse get_backtrace: {
const trace = @as(?*std.builtin.StackTrace, if (error_return_trace) |ert|
if (ert.index > 0)
ert
else
null
else
null) orelse get_backtrace: {
trace_buf = std.builtin.StackTrace{
.index = 0,
.instruction_addresses = &addr_buf,
Expand Down
10 changes: 7 additions & 3 deletions src/js/internal/html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This is the file that loads when you pass a, .html entry point to Bun.
// This is the file that loads when you pass a '.html' entry point to Bun.
// It imports the entry points and initializes a server.
import type { HTMLBundle, Server } from "bun";
const initital = performance.now();
const argv = process.argv;
Expand Down Expand Up @@ -247,15 +248,18 @@ yourself with Bun.serve().
const enableANSIColors = Bun.enableANSIColors;
function printInitialMessage(isFirst: boolean) {
if (enableANSIColors) {
let topLine = `\n\x1b[1;34m\x1b[5mBun\x1b[0m \x1b[1;34mv${Bun.version}\x1b[0m`;
let topLine = `${server.development ? "\x1b[34;7m DEV \x1b[0m " : ""}\x1b[1;34m\x1b[5mBun\x1b[0m \x1b[1;34mv${Bun.version}\x1b[0m`;
if (isFirst) {
topLine += ` \x1b[2mready in\x1b[0m \x1b[1m${elapsed}\x1b[0m ms`;
}
console.log(topLine + "\n");
console.log(`\x1b[1;34m➜\x1b[0m \x1b[36m${server!.url.href}\x1b[0m`);
} else {
let topLine = `\n Bun v${Bun.version}`;
let topLine = `Bun v${Bun.version}`;
if (isFirst) {
if (server.development) {
topLine += " dev server";
}
topLine += ` ready in ${elapsed} ms`;
}
console.log(topLine + "\n");
Expand Down
18 changes: 16 additions & 2 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,10 @@ pub const JSX = struct {
classic_import_source: string = "react",
package_name: []const u8 = "react",

/// Configuration Priority:
/// - `--define=process.env.NODE_ENV=...`
/// - `NODE_ENV=...`
/// - tsconfig.json's `compilerOptions.jsx` (`react-jsx` or `react-jsxdev`)
development: bool = true,
parse: bool = true,

Expand Down Expand Up @@ -1580,14 +1584,24 @@ pub const BundleOptions = struct {

supports_multiple_outputs: bool = true,

force_development: ?bool = null,
/// This is set by the process environment, which is used to override the
/// JSX configuration. When this is unspecified, the tsconfig.json is used
/// to determine if a development jsx-runtime is used (by going between
/// "react-jsx" or "react-jsx-dev-runtime")
force_node_env: ForceNodeEnv = .unspecified,

pub const ForceNodeEnv = enum {
unspecified,
development,
production,
};

pub fn isTest(this: *const BundleOptions) bool {
return this.rewrite_jest_for_tests;
}

pub fn setProduction(this: *BundleOptions, value: bool) void {
if (this.force_development == null) {
if (this.force_node_env == .unspecified) {
this.production = value;
this.jsx.development = !value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/transpiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ pub const Transpiler = struct {
if (is_development) {
this.options.setProduction(false);
this.resolver.opts.setProduction(false);
this.options.force_development = true;
this.resolver.opts.force_development = true;
this.options.force_node_env = .development;
this.resolver.opts.force_node_env = .development;
} else if (is_production) {
this.options.setProduction(true);
this.resolver.opts.setProduction(true);
Expand Down

0 comments on commit 523bf96

Please sign in to comment.