mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 03:48:56 +00:00
fix defines
Former-commit-id: 12db22bc3f5875ee0c43d25f8247983967451c3f
This commit is contained in:
@@ -1,9 +1,3 @@
|
||||
globalThis.process = {
|
||||
platform: "posix",
|
||||
env: {},
|
||||
browser: true,
|
||||
};
|
||||
|
||||
import * as ReactDOM from "react-dom";
|
||||
import App from "next/app";
|
||||
import mitt, { MittEmitter } from "next/dist/shared/lib/mitt";
|
||||
@@ -36,7 +30,6 @@ import {
|
||||
createRouter,
|
||||
makePublicRouterInstance,
|
||||
} from "next/dist/client/router";
|
||||
import "./renderDocument";
|
||||
export const emitter: MittEmitter<string> = mitt();
|
||||
|
||||
export default function boot(EntryPointNamespace, loader) {
|
||||
|
||||
@@ -20,16 +20,31 @@
|
||||
"client": {
|
||||
".env": "NEXT_PUBLIC_",
|
||||
"defaults": {
|
||||
"process.env.__NEXT_TRAILING_SLASH": "false"
|
||||
"process.env.__NEXT_TRAILING_SLASH": "false",
|
||||
"process.env.NODE_ENV": "\"development\"",
|
||||
"process.env.__NEXT_ROUTER_BASEPATH": "''",
|
||||
"process.env.__NEXT_SCROLL_RESTORATION": "false",
|
||||
"process.env.__NEXT_I18N_SUPPORT": "false",
|
||||
"process.env.__NEXT_HAS_REWRITES": "false",
|
||||
"process.env.__NEXT_ANALYTICS_ID": "null",
|
||||
"process.env.__NEXT_OPTIMIZE_CSS": "false",
|
||||
"process.env.__NEXT_CROSS_ORIGIN": "''"
|
||||
}
|
||||
},
|
||||
"server": {
|
||||
".env": "*",
|
||||
".env": "NEXT_",
|
||||
"defaults": {
|
||||
"process.env.__NEXT_TRAILING_SLASH": "false",
|
||||
"process.env.__NEXT_OPTIMIZE_FONTS": "false",
|
||||
"process.env.NODE_ENV": "\"development\"",
|
||||
"process.env.__NEXT_OPTIMIZE_IMAGES": "false",
|
||||
"process.env.__NEXT_OPTIMIZE_CSS": "false"
|
||||
"process.env.__NEXT_OPTIMIZE_CSS": "false",
|
||||
"process.env.__NEXT_ROUTER_BASEPATH": "''",
|
||||
"process.env.__NEXT_SCROLL_RESTORATION": "false",
|
||||
"process.env.__NEXT_I18N_SUPPORT": "false",
|
||||
"process.env.__NEXT_HAS_REWRITES": "false",
|
||||
"process.env.__NEXT_ANALYTICS_ID": "null",
|
||||
"process.env.__NEXT_CROSS_ORIGIN": "''"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import "../src/font.css";
|
||||
import "../src/index.css";
|
||||
|
||||
import App from "next/app";
|
||||
|
||||
@@ -345,6 +345,11 @@ pub fn NewBundler(cache_files: bool) type {
|
||||
|
||||
try this.runEnvLoader();
|
||||
|
||||
js_ast.Expr.Data.Store.create(this.allocator);
|
||||
js_ast.Stmt.Data.Store.create(this.allocator);
|
||||
defer js_ast.Expr.Data.Store.reset();
|
||||
defer js_ast.Stmt.Data.Store.reset();
|
||||
|
||||
if (this.options.framework) |framework| {
|
||||
if (this.options.platform.isClient()) {
|
||||
try this.options.loadDefines(this.allocator, this.env, &framework.client_env);
|
||||
@@ -578,6 +583,7 @@ pub fn NewBundler(cache_files: bool) type {
|
||||
var tmpdir: std.fs.Dir = try bundler.fs.fs.openTmpDir();
|
||||
var tmpname_buf: [64]u8 = undefined;
|
||||
bundler.resetStore();
|
||||
try bundler.configureDefines();
|
||||
|
||||
const tmpname = try bundler.fs.tmpname(
|
||||
".jsb",
|
||||
|
||||
@@ -120,7 +120,7 @@ pub const DefineData = struct {
|
||||
var data: js_ast.Expr.Data = undefined;
|
||||
switch (expr.data) {
|
||||
.e_missing => {
|
||||
continue;
|
||||
data = .{ .e_missing = js_ast.E.Missing{} };
|
||||
},
|
||||
// We must copy so we don't recycle
|
||||
.e_string => {
|
||||
|
||||
@@ -34,6 +34,9 @@ pub const css_supports_fence = true;
|
||||
pub const disable_entry_cache = false;
|
||||
pub const enable_bytecode_caching = false;
|
||||
|
||||
// This feature flag exists so when you have defines inside package.json, you can use single quotes in nested strings.
|
||||
pub const allow_json_single_quotes = true;
|
||||
|
||||
pub const react_specific_warnings = true;
|
||||
// Disabled due to concurrency bug I don't have time to fix right now.
|
||||
// I suspect it's like 3 undefined memory issues.
|
||||
|
||||
26
src/http.zig
26
src/http.zig
@@ -741,8 +741,7 @@ pub const RequestContext = struct {
|
||||
Output.flush();
|
||||
return;
|
||||
};
|
||||
vm.bundler.configureRouter(false) catch {};
|
||||
try vm.bundler.configureDefines();
|
||||
|
||||
std.debug.assert(JavaScript.VirtualMachine.vm_loaded);
|
||||
javascript_vm = vm;
|
||||
|
||||
@@ -752,6 +751,29 @@ pub const RequestContext = struct {
|
||||
vm.watcher = handler.watcher;
|
||||
{
|
||||
defer vm.flush();
|
||||
vm.bundler.configureRouter(false) catch {};
|
||||
vm.bundler.configureDefines() catch |err| {
|
||||
if (vm.log.msgs.items.len > 0) {
|
||||
for (vm.log.msgs.items) |msg| {
|
||||
msg.writeFormat(Output.errorWriter()) catch continue;
|
||||
}
|
||||
}
|
||||
|
||||
Output.prettyErrorln(
|
||||
"<r>JavaScript VM failed to start due to <red>{s}<r>.",
|
||||
.{
|
||||
@errorName(err),
|
||||
},
|
||||
);
|
||||
|
||||
Output.flush();
|
||||
|
||||
if (channel.tryReadItem() catch null) |item| {
|
||||
item.ctx.sendInternalError(error.JSFailedToStart) catch {};
|
||||
item.ctx.arena.deinit();
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
var entry_point = boot;
|
||||
if (!std.fs.path.isAbsolute(entry_point)) {
|
||||
|
||||
@@ -37,89 +37,5 @@ pub fn configureTransformOptionsForSpeedyVM(allocator: *std.mem.Allocator, _args
|
||||
pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
|
||||
var args = _args;
|
||||
args.platform = Api.Platform.speedy;
|
||||
// We inline process.env.* at bundle time but process.env is a proxy object which will otherwise return undefined.
|
||||
|
||||
var env_map = try getNodeEnvMap(allocator);
|
||||
var env_count = env_map.count();
|
||||
|
||||
if (args.define) |def| {
|
||||
for (def.keys) |key| {
|
||||
env_count += @boolToInt((env_map.get(key) == null));
|
||||
}
|
||||
}
|
||||
var needs_node_env = env_map.get("NODE_ENV") == null;
|
||||
var needs_window_undefined = true;
|
||||
|
||||
var needs_regenerate = args.define == null and env_count > 0;
|
||||
if (args.define) |def| {
|
||||
if (def.keys.len != env_count) {
|
||||
needs_regenerate = true;
|
||||
}
|
||||
for (def.keys) |key| {
|
||||
if (strings.eql(key, "process.env.NODE_ENV")) {
|
||||
needs_node_env = false;
|
||||
} else if (strings.eql(key, "window")) {
|
||||
needs_window_undefined = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var extras_count = @intCast(usize, @boolToInt(needs_node_env)) + @intCast(usize, @boolToInt(needs_window_undefined));
|
||||
|
||||
if (needs_regenerate) {
|
||||
var new_list = try allocator.alloc([]const u8, env_count * 2 + extras_count * 2);
|
||||
var keys = new_list[0 .. new_list.len / 2];
|
||||
var values = new_list[keys.len..];
|
||||
var new_map = Api.StringMap{
|
||||
.keys = keys,
|
||||
.values = values,
|
||||
};
|
||||
var iter = env_map.iterator();
|
||||
|
||||
var last: usize = 0;
|
||||
while (iter.next()) |entry| {
|
||||
keys[last] = entry.key_ptr.*;
|
||||
var value = entry.value_ptr.*;
|
||||
|
||||
if (value.len == 0 or value[0] != '"' or value[value.len - 1] != '"') {
|
||||
value = try std.fmt.allocPrint(allocator, "\"{s}\"", .{value});
|
||||
}
|
||||
values[last] = value;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
if (args.define) |def| {
|
||||
var from_env = keys[0..last];
|
||||
|
||||
for (def.keys) |pre, i| {
|
||||
if (env_map.get(pre) != null) {
|
||||
for (from_env) |key, j| {
|
||||
if (strings.eql(key, pre)) {
|
||||
values[j] = def.values[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
keys[last] = pre;
|
||||
values[last] = def.values[i];
|
||||
last += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (needs_node_env) {
|
||||
keys[last] = options.DefaultUserDefines.NodeEnv.Key;
|
||||
values[last] = options.DefaultUserDefines.NodeEnv.Value;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
if (needs_window_undefined) {
|
||||
keys[last] = DefaultSpeedyDefines.Keys.window;
|
||||
values[last] = DefaultSpeedyDefines.Values.window;
|
||||
last += 1;
|
||||
}
|
||||
|
||||
args.define = new_map;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -604,8 +604,10 @@ pub const Lexer = struct {
|
||||
lexer.string_literal = lexer.string_literal_buffer.items;
|
||||
}
|
||||
|
||||
if (quote == '\'' and lexer.json_options != null) {
|
||||
try lexer.addRangeError(lexer.range(), "JSON strings must use double quotes", .{}, true);
|
||||
if (comptime !FeatureFlags.allow_json_single_quotes) {
|
||||
if (quote == '\'' and lexer.json_options != null) {
|
||||
try lexer.addRangeError(lexer.range(), "JSON strings must use double quotes", .{}, true);
|
||||
}
|
||||
}
|
||||
|
||||
// for (text)
|
||||
|
||||
@@ -120,6 +120,61 @@ const ExprFlag = packed struct {
|
||||
}
|
||||
};
|
||||
|
||||
const ImportVariant = enum {
|
||||
path_only,
|
||||
import_star,
|
||||
import_default,
|
||||
import_star_and_import_default,
|
||||
import_items,
|
||||
import_items_and_default,
|
||||
import_items_and_star,
|
||||
import_items_and_default_and_star,
|
||||
|
||||
pub inline fn hasItems(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.import_default => .import_items_and_default,
|
||||
.import_star => .import_items_and_star,
|
||||
.import_star_and_import_default => .import_items_and_default_and_star,
|
||||
else => .import_items,
|
||||
};
|
||||
}
|
||||
|
||||
// We always check star first so don't need to be exhaustive here
|
||||
pub inline fn hasStar(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.path_only => .import_star,
|
||||
else => import_variant,
|
||||
};
|
||||
}
|
||||
|
||||
// We check default after star
|
||||
pub inline fn hasDefault(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.path_only => .import_default,
|
||||
.import_star => .import_star_and_import_default,
|
||||
else => import_variant,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn determine(record: *const importRecord.ImportRecord, namespace: Symbol, s_import: *const S.Import) ImportVariant {
|
||||
var variant = ImportVariant.path_only;
|
||||
|
||||
if (record.contains_import_star) {
|
||||
variant = variant.hasStar();
|
||||
}
|
||||
|
||||
if (record.contains_default_alias or s_import.default_name != null) {
|
||||
variant = variant.hasDefault();
|
||||
}
|
||||
|
||||
if (s_import.items.len > 0) {
|
||||
variant = variant.hasItems();
|
||||
}
|
||||
|
||||
return variant;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn NewPrinter(
|
||||
comptime ascii_only: bool,
|
||||
comptime Writer: type,
|
||||
@@ -2847,18 +2902,34 @@ pub fn NewPrinter(
|
||||
p.printSpaceBeforeIdentifier();
|
||||
p.print("var {");
|
||||
|
||||
for (s.items) |item, i| {
|
||||
p.print(item.alias);
|
||||
const name = p.renamer.nameForSymbol(item.name.ref.?);
|
||||
if (!strings.eql(name, item.alias)) {
|
||||
p.print(":");
|
||||
p.printSymbol(item.name.ref.?);
|
||||
if (s.default_name) |default_name| {
|
||||
p.print("default: ");
|
||||
p.printSymbol(default_name.ref.?);
|
||||
p.print(", ");
|
||||
for (s.items) |item, i| {
|
||||
p.print(item.alias);
|
||||
const name = p.renamer.nameForSymbol(item.name.ref.?);
|
||||
if (!strings.eql(name, item.alias)) {
|
||||
p.print(": ");
|
||||
p.printSymbol(item.name.ref.?);
|
||||
}
|
||||
p.print(" , ");
|
||||
}
|
||||
} else {
|
||||
for (s.items) |item, i| {
|
||||
p.print(item.alias);
|
||||
const name = p.renamer.nameForSymbol(item.name.ref.?);
|
||||
if (!strings.eql(name, item.alias)) {
|
||||
p.print(":");
|
||||
p.printSymbol(item.name.ref.?);
|
||||
}
|
||||
|
||||
if (i < s.items.len - 1) {
|
||||
p.print(", ");
|
||||
if (i < s.items.len - 1) {
|
||||
p.print(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p.print("} = ");
|
||||
|
||||
p.printLoadFromBundleWithoutCall(s.import_record_index);
|
||||
@@ -3048,60 +3119,7 @@ pub fn NewPrinter(
|
||||
return;
|
||||
}
|
||||
|
||||
const ImportVariant = enum {
|
||||
path_only,
|
||||
import_star,
|
||||
import_default,
|
||||
import_star_and_import_default,
|
||||
import_items,
|
||||
import_items_and_default,
|
||||
import_items_and_star,
|
||||
import_items_and_default_and_star,
|
||||
|
||||
pub fn hasItems(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.import_default => .import_items_and_default,
|
||||
.import_star => .import_items_and_star,
|
||||
.import_star_and_import_default => .import_items_and_default_and_star,
|
||||
else => .import_items,
|
||||
};
|
||||
}
|
||||
|
||||
// We always check star first so don't need to be exhaustive here
|
||||
pub fn hasStar(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.path_only => .import_star,
|
||||
else => import_variant,
|
||||
};
|
||||
}
|
||||
|
||||
// We check default after star
|
||||
pub fn hasDefault(import_variant: @This()) @This() {
|
||||
return switch (import_variant) {
|
||||
.path_only => .import_default,
|
||||
.import_star => .import_star_and_import_default,
|
||||
else => import_variant,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var variant = ImportVariant.path_only;
|
||||
|
||||
var namespace = p.symbols.get(s.namespace_ref).?;
|
||||
|
||||
if (record.contains_import_star) {
|
||||
variant = variant.hasStar();
|
||||
}
|
||||
|
||||
if (record.contains_default_alias or s.default_name != null) {
|
||||
variant = variant.hasDefault();
|
||||
}
|
||||
|
||||
if (s.items.len > 0) {
|
||||
variant = variant.hasItems();
|
||||
}
|
||||
|
||||
switch (variant) {
|
||||
switch (ImportVariant.determine(&record, p.symbols.get(s.namespace_ref).?, s)) {
|
||||
// we treat path_only the same as import_star because we may have property accesses using it.
|
||||
.path_only, .import_star => {
|
||||
p.print("var ");
|
||||
|
||||
Reference in New Issue
Block a user