aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-08-14 23:04:29 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-08-14 23:04:29 -0700
commitbfac22d951fb3ca2e1cf849a2af0cc60c372b1ed (patch)
tree3ca7f1213edc4ef290edfeae8a367a92904bd4dc
parentd95ffe63023c09ea792d4f0379374f7c6c7975e6 (diff)
downloadbun-bfac22d951fb3ca2e1cf849a2af0cc60c372b1ed.tar.gz
bun-bfac22d951fb3ca2e1cf849a2af0cc60c372b1ed.tar.zst
bun-bfac22d951fb3ca2e1cf849a2af0cc60c372b1ed.zip
fix defines
Former-commit-id: 12db22bc3f5875ee0c43d25f8247983967451c3f
-rw-r--r--demos/css-stress-test/nexty/client.development.tsx7
-rw-r--r--demos/css-stress-test/nexty/package.json21
-rw-r--r--demos/css-stress-test/pages/_app.tsx1
-rw-r--r--src/bundler.zig6
-rw-r--r--src/defines.zig2
-rw-r--r--src/feature_flags.zig3
-rw-r--r--src/http.zig26
-rw-r--r--src/javascript/jsc/config.zig84
-rw-r--r--src/js_lexer.zig6
-rw-r--r--src/js_printer.zig142
10 files changed, 136 insertions, 162 deletions
diff --git a/demos/css-stress-test/nexty/client.development.tsx b/demos/css-stress-test/nexty/client.development.tsx
index 778df6f12..82bce03a8 100644
--- a/demos/css-stress-test/nexty/client.development.tsx
+++ b/demos/css-stress-test/nexty/client.development.tsx
@@ -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) {
diff --git a/demos/css-stress-test/nexty/package.json b/demos/css-stress-test/nexty/package.json
index d18211113..ea9879069 100644
--- a/demos/css-stress-test/nexty/package.json
+++ b/demos/css-stress-test/nexty/package.json
@@ -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": "''"
}
}
}
diff --git a/demos/css-stress-test/pages/_app.tsx b/demos/css-stress-test/pages/_app.tsx
index 04a911072..43d6a776a 100644
--- a/demos/css-stress-test/pages/_app.tsx
+++ b/demos/css-stress-test/pages/_app.tsx
@@ -1,4 +1,3 @@
-import "../src/font.css";
import "../src/index.css";
import App from "next/app";
diff --git a/src/bundler.zig b/src/bundler.zig
index 4fba107c0..97e9f692c 100644
--- a/src/bundler.zig
+++ b/src/bundler.zig
@@ -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",
diff --git a/src/defines.zig b/src/defines.zig
index c3869c166..7c970a71d 100644
--- a/src/defines.zig
+++ b/src/defines.zig
@@ -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 => {
diff --git a/src/feature_flags.zig b/src/feature_flags.zig
index 3cbd5787a..be216e69c 100644
--- a/src/feature_flags.zig
+++ b/src/feature_flags.zig
@@ -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.
diff --git a/src/http.zig b/src/http.zig
index 5ea455dcc..c47c1dbec 100644
--- a/src/http.zig
+++ b/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)) {
diff --git a/src/javascript/jsc/config.zig b/src/javascript/jsc/config.zig
index edc6f342d..affb1df68 100644
--- a/src/javascript/jsc/config.zig
+++ b/src/javascript/jsc/config.zig
@@ -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;
}
diff --git a/src/js_lexer.zig b/src/js_lexer.zig
index ee80c6fb6..cf1470cab 100644
--- a/src/js_lexer.zig
+++ b/src/js_lexer.zig
@@ -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)
diff --git a/src/js_printer.zig b/src/js_printer.zig
index fa51171d8..606751dea 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -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 ");