aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-09-01 14:00:46 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-09-01 14:00:46 -0700
commit4b7917ba8f5ee8e62b0e268d121b7fd30193b967 (patch)
tree2305a9665a83a5c25e168414a1fa56ff12e26a43 /src
parente2b9f232315fde2f47e0d55fb01f9f214ac2b3d3 (diff)
downloadbun-4b7917ba8f5ee8e62b0e268d121b7fd30193b967.tar.gz
bun-4b7917ba8f5ee8e62b0e268d121b7fd30193b967.tar.zst
bun-4b7917ba8f5ee8e62b0e268d121b7fd30193b967.zip
latest
Former-commit-id: 20b96180ffc41610dfb21a041d1258f2ff8d4196
Diffstat (limited to 'src')
-rw-r--r--src/http.zig2
-rw-r--r--src/js_ast.zig6
-rw-r--r--src/js_parser/js_parser.zig49
-rw-r--r--src/runtime.js9
-rw-r--r--src/runtime.version2
-rw-r--r--src/test/fixtures/disabled-import-bun.ts3
-rw-r--r--src/test/fixtures/maybe-rewrite-index-accessor.js1
7 files changed, 62 insertions, 10 deletions
diff --git a/src/http.zig b/src/http.zig
index a967b466d..fd17f2021 100644
--- a/src/http.zig
+++ b/src/http.zig
@@ -1488,7 +1488,7 @@ pub const RequestContext = struct {
);
},
.success => {
- if (build_result.timestamp < cmd.timestamp) {
+ if (build_result.timestamp > cmd.timestamp) {
Output.prettyln(
"<r><b><green>{d}ms<r> <d>built<r> <b>{s}<r><b> <r><d>({d}+ LOC)",
.{
diff --git a/src/js_ast.zig b/src/js_ast.zig
index 42d3816e9..8899df8c5 100644
--- a/src/js_ast.zig
+++ b/src/js_ast.zig
@@ -1066,15 +1066,15 @@ pub const E = struct {
}
}
- pub fn isUTF8(s: *const String) bool {
+ pub inline fn isUTF8(s: *const String) bool {
return s.utf8.len > 0;
}
- pub fn isBlank(s: *const String) bool {
+ pub inline fn isBlank(s: *const String) bool {
return std.math.max(s.utf8.len, s.value.len) == 0;
}
- pub fn isPresent(s: *const String) bool {
+ pub inline fn isPresent(s: *const String) bool {
return std.math.max(s.utf8.len, s.value.len) > 0;
}
diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig
index a1d4f04a4..590f8b168 100644
--- a/src/js_parser/js_parser.zig
+++ b/src/js_parser/js_parser.zig
@@ -11139,21 +11139,45 @@ pub fn NewParser(
.has_chain_parent = (e_.optional_chain orelse js_ast.OptionalChain.start) == js_ast.OptionalChain.ccontinue,
});
e_.target = target;
+ const index = p.visitExpr(e_.index);
+ e_.index = index;
- if (e_.optional_chain == null and @as(Expr.Tag, e_.index.data) == .e_string) {
+ if (e_.optional_chain == null and e_.index.data == .e_string and e_.index.data.e_string.isUTF8()) {
+ const literal = e_.index.data.e_string.utf8;
if (p.maybeRewritePropertyAccess(
expr.loc,
in.assign_target,
is_delete_target,
e_.target,
- e_.index.data.e_string.string(p.allocator) catch unreachable,
+ literal,
e_.index.loc,
is_call_target,
)) |val| {
return val;
}
- } else {
- e_.index = p.visitExpr(e_.index);
+
+ // delete process.env["NODE_ENV"]
+ // shouldn't be transformed into
+ // delete undefined
+ if (!is_delete_target and !is_call_target) {
+ // We check for defines here as well
+ // esbuild doesn't do this
+ // In a lot of codebases, people will sometimes do:
+ // process.env["NODE_ENV"]
+ // Often not intentionally
+ // So we want to be able to detect this and still Do The Right Thing
+ if (p.define.dots.get(literal)) |parts| {
+ for (parts) |define| {
+ if (p.isDotDefineMatch(expr, define.parts)) {
+ if (!define.data.isUndefined()) {
+ return p.valueForDefine(expr.loc, in.assign_target, is_delete_target, &define.data);
+ }
+
+ return p.e(E.Undefined{}, expr.loc);
+ }
+ }
+ }
+ }
}
// Create an error for assigning to an import namespace when bundling. Even
@@ -13140,6 +13164,8 @@ pub fn NewParser(
};
}
+ // This function is recursive
+ // But it shouldn't be that long
pub fn isDotDefineMatch(p: *P, expr: Expr, parts: []const string) bool {
switch (expr.data) {
.e_dot => |ex| {
@@ -13157,6 +13183,21 @@ pub fn NewParser(
.e_import_meta => {
return parts.len == 2 and strings.eqlComptime(parts[0], "import") and strings.eqlComptime(parts[1], "meta");
},
+ // Note: this behavior differs from esbuild
+ // esbuild does not try to match index accessors
+ // we do, but only if it's a UTF8 string
+ // the intent is to handle people using this form instead of E.Dot. So we really only want to do this if the accessor can also be an identifier
+ .e_index => |index| {
+ if (parts.len > 1 and index.index.data == .e_string and index.index.data.e_string.isUTF8()) {
+ if (index.optional_chain != null) {
+ return false;
+ }
+
+ const last = parts.len - 1;
+ const is_tail_match = strings.eql(parts[last], index.index.data.e_string.utf8);
+ return is_tail_match and p.isDotDefineMatch(index.target, parts[0..last]);
+ }
+ },
.e_identifier => |ex| {
// The last expression must be an identifier
diff --git a/src/runtime.js b/src/runtime.js
index 96c1c2d6f..a76b3f077 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -69,7 +69,8 @@ export var __commonJS = (cb, name) => {
});
// If it's a namespace export without .default, pretend .default is the same as mod.exports
} else if (
- typeof mod.exports === "object" &&
+ (typeof mod.exports === "object" ||
+ typeof mod.exports === "function") &&
!("default" in mod.exports)
) {
var defaultValue = mod.exports;
@@ -184,3 +185,9 @@ export var __reExport = (target, module, desc) => {
});
return target;
};
+
+if (typeof globalThis.process === "undefined") {
+ globalThis.process = {
+ env: {},
+ };
+}
diff --git a/src/runtime.version b/src/runtime.version
index 866b687de..ffb6d2620 100644
--- a/src/runtime.version
+++ b/src/runtime.version
@@ -1 +1 @@
-f763299933041bd3 \ No newline at end of file
+cab4fe7bfaf335f6 \ No newline at end of file
diff --git a/src/test/fixtures/disabled-import-bun.ts b/src/test/fixtures/disabled-import-bun.ts
new file mode 100644
index 000000000..632c80364
--- /dev/null
+++ b/src/test/fixtures/disabled-import-bun.ts
@@ -0,0 +1,3 @@
+import * as Hello from "object-inspect";
+
+Hello.default();
diff --git a/src/test/fixtures/maybe-rewrite-index-accessor.js b/src/test/fixtures/maybe-rewrite-index-accessor.js
new file mode 100644
index 000000000..6e7411a01
--- /dev/null
+++ b/src/test/fixtures/maybe-rewrite-index-accessor.js
@@ -0,0 +1 @@
+const isProd = process.env["NODE_ENV"] === "production";