diff options
author | 2021-08-13 02:01:41 -0700 | |
---|---|---|
committer | 2021-08-13 02:01:41 -0700 | |
commit | f59892f647ceef1c05e40c9cdef4f79d0a530c2f (patch) | |
tree | 6fed07dc02e722de634618a6f2d246f52510d141 /src | |
parent | 86642cbdd528bbf37708dbb30a815c9a68b6aea1 (diff) | |
download | bun-f59892f647ceef1c05e40c9cdef4f79d0a530c2f.tar.gz bun-f59892f647ceef1c05e40c9cdef4f79d0a530c2f.tar.zst bun-f59892f647ceef1c05e40c9cdef4f79d0a530c2f.zip |
late
Former-commit-id: 1d598bb05a3bac62d86063125e1fe2962f0b5cc6
Diffstat (limited to 'src')
-rw-r--r-- | src/bundler.zig | 10 | ||||
-rw-r--r-- | src/css_scanner.zig | 23 | ||||
-rw-r--r-- | src/http.zig | 4 | ||||
-rw-r--r-- | src/http/url_path.zig | 72 | ||||
-rw-r--r-- | src/javascript/jsc/api/router.zig | 46 | ||||
-rw-r--r-- | src/javascript/jsc/base.zig | 4 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 40 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 3 | ||||
-rw-r--r-- | src/js_parser/js_parser.zig | 57 | ||||
-rw-r--r-- | src/js_printer.zig | 34 | ||||
-rw-r--r-- | src/options.zig | 34 | ||||
-rw-r--r-- | src/runtime.js | 4 | ||||
-rw-r--r-- | src/runtime.version | 2 |
13 files changed, 272 insertions, 61 deletions
diff --git a/src/bundler.zig b/src/bundler.zig index 49b9f496b..377079355 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -101,13 +101,13 @@ pub const ClientEntryPoint = struct { \\ globalThis.onerror.loaded = loaded; \\}} \\ - \\import * as boot from '{s}'; + \\import boot from '{s}'; \\loaded.boot = true; \\if ('setLoaded' in boot) boot.setLoaded(loaded); \\import * as EntryPoint from '{s}{s}'; \\loaded.entry = true; \\ - \\if (!('default' in boot) ) {{ + \\if (!boot) {{ \\ const now = Date.now(); \\ debugger; \\ const elapsed = Date.now() - now; @@ -116,7 +116,7 @@ pub const ClientEntryPoint = struct { \\ }} \\}} \\ - \\boot.default(EntryPoint, loaded); + \\boot(EntryPoint, loaded); , .{ client, @@ -2126,15 +2126,15 @@ pub const Transformer = struct { ) !options.TransformResult { js_ast.Expr.Data.Store.create(allocator); js_ast.Stmt.Data.Store.create(allocator); + const platform = options.Platform.from(opts.platform); - var define = try options.definesFromTransformOptions(allocator, log, opts.define, false); + var define = try options.definesFromTransformOptions(allocator, log, opts.define, false, platform); const cwd = if (opts.absolute_working_dir) |workdir| try std.fs.realpathAlloc(allocator, workdir) else try std.process.getCwdAlloc(allocator); const output_dir_parts = [_]string{ try std.process.getCwdAlloc(allocator), opts.output_dir orelse "out" }; const output_dir = try std.fs.path.join(allocator, &output_dir_parts); var output_files = try std.ArrayList(options.OutputFile).initCapacity(allocator, opts.entry_points.len); - const platform = options.Platform.from(opts.platform); const out_extensions = platform.outExtensions(allocator); var loader_map = try options.loadersFromTransformOptions(allocator, opts.loaders); diff --git a/src/css_scanner.zig b/src/css_scanner.zig index e39515292..298c5f81a 100644 --- a/src/css_scanner.zig +++ b/src/css_scanner.zig @@ -898,16 +898,21 @@ pub fn NewWriter( switch (chunk.content) { .t_url => |url| {}, .t_import => |import| { - try writer.buildCtx.addCSSImport( - try writer.linker.resolveCSS( - writer.source.path, - import.text.utf8, - chunk.range, - import_record.ImportKind.at, - Options.BundleOptions.ImportPathFormat.absolute_path, - true, - ), + const resolved = try writer.linker.resolveCSS( + writer.source.path, + import.text.utf8, + chunk.range, + import_record.ImportKind.at, + Options.BundleOptions.ImportPathFormat.absolute_path, + true, ); + + // TODO: just check is_external instead + if (strings.startsWith(import.text.utf8, "https://") or strings.startsWith(import.text.utf8, "http://")) { + return; + } + + try writer.buildCtx.addCSSImport(resolved); }, .t_verbatim => |verbatim| {}, } diff --git a/src/http.zig b/src/http.zig index 06eaf7a18..ffb7a3d72 100644 --- a/src/http.zig +++ b/src/http.zig @@ -1573,7 +1573,7 @@ pub const RequestContext = struct { break :brk try ctx.bundler.buildFile( &ctx.log, ctx.allocator, - ctx.url.path, + ctx.url.pathWithoutOrigin(ctx.bundler.options.origin), ctx.url.extname, true, ); @@ -1581,7 +1581,7 @@ pub const RequestContext = struct { break :brk try ctx.bundler.buildFile( &ctx.log, ctx.allocator, - ctx.url.path, + ctx.url.pathWithoutOrigin(ctx.bundler.options.origin), ctx.url.extname, false, ); diff --git a/src/http/url_path.zig b/src/http/url_path.zig index a2e55555d..e82d5601a 100644 --- a/src/http/url_path.zig +++ b/src/http/url_path.zig @@ -1,6 +1,8 @@ usingnamespace @import("../global.zig"); -const std = @import("std"); +const PercentEncoding = @import("../query_string_map.zig").PercentEncoding; +const std = @import("std"); +const allocators = @import("../allocators.zig"); const URLPath = @This(); extname: string = "", @@ -8,17 +10,63 @@ path: string = "", pathname: string = "", first_segment: string = "", query_string: string = "", +const toMutable = allocators.constStrToU8; + +// TODO: use a real URL parser +// this treats a URL like /_next/ identically to / +pub fn pathWithoutOrigin(this: *const URLPath, _origin: string) string { + if (_origin.len == 0) return this.path; + const leading_slash_offset: usize = if (_origin[0] == '/') 1 else 0; + const base = this.path; + const origin = _origin[leading_slash_offset..]; + + return if (base.len >= origin.len and strings.eql(base[0..origin.len], origin)) base[origin.len..] else base; +} + +// optimization: very few long strings will be URL-encoded +// we're allocating virtual memory here, so if we never use it, it won't be allocated +// and even when they're, they're probably rarely going to be > 1024 chars long +// so we can have a big and little one and almost always use the little one +threadlocal var temp_path_buf: [1024]u8 = undefined; +threadlocal var big_temp_path_buf: [16384]u8 = undefined; + +pub fn parse(possibly_encoded_pathname_: string) URLPath { + var decoded_pathname = possibly_encoded_pathname_; + + if (strings.indexOfChar(decoded_pathname, '%') != null) { + var possibly_encoded_pathname = switch (decoded_pathname.len) { + 0...1024 => &temp_path_buf, + else => &big_temp_path_buf, + }; + possibly_encoded_pathname = possibly_encoded_pathname[0..std.math.min( + possibly_encoded_pathname_.len, + possibly_encoded_pathname.len, + )]; + + std.mem.copy(u8, possibly_encoded_pathname, possibly_encoded_pathname_[0..possibly_encoded_pathname.len]); + var clone = possibly_encoded_pathname[0..possibly_encoded_pathname.len]; + + var fbs = std.io.fixedBufferStream( + // This is safe because: + // - this comes from a non-const buffer + // - percent *decoding* will always be <= length of the original string (no buffer overflow) + toMutable( + possibly_encoded_pathname, + ), + ); + var writer = fbs.writer(); + decoded_pathname = possibly_encoded_pathname[0 .. PercentEncoding.decode(@TypeOf(writer), writer, clone) catch unreachable]; + } -// This does one pass over the URL path instead of like 4 -pub fn parse(raw_path: string) URLPath { var question_mark_i: i16 = -1; var period_i: i16 = -1; var first_segment_end: i16 = std.math.maxInt(i16); var last_slash: i16 = -1; - var i: i16 = @intCast(i16, raw_path.len) - 1; + var i: i16 = @intCast(i16, decoded_pathname.len) - 1; + while (i >= 0) : (i -= 1) { - const c = raw_path[@intCast(usize, i)]; + const c = decoded_pathname[@intCast(usize, i)]; switch (c) { '?' => { @@ -52,24 +100,24 @@ pub fn parse(raw_path: string) URLPath { const extname = brk: { if (question_mark_i > -1 and period_i > -1) { period_i += 1; - break :brk raw_path[@intCast(usize, period_i)..@intCast(usize, question_mark_i)]; + break :brk decoded_pathname[@intCast(usize, period_i)..@intCast(usize, question_mark_i)]; } else if (period_i > -1) { period_i += 1; - break :brk raw_path[@intCast(usize, period_i)..]; + break :brk decoded_pathname[@intCast(usize, period_i)..]; } else { break :brk &([_]u8{}); } }; - const path = if (question_mark_i < 0) raw_path[1..] else raw_path[1..@intCast(usize, question_mark_i)]; + const path = if (question_mark_i < 0) decoded_pathname[1..] else decoded_pathname[1..@intCast(usize, question_mark_i)]; - const first_segment = raw_path[1..std.math.min(@intCast(usize, first_segment_end), raw_path.len)]; + const first_segment = decoded_pathname[1..std.math.min(@intCast(usize, first_segment_end), decoded_pathname.len)]; return URLPath{ .extname = extname, - .pathname = raw_path, + .pathname = decoded_pathname, .first_segment = first_segment, - .path = if (raw_path.len == 1) "." else path, - .query_string = if (question_mark_i > -1) raw_path[@intCast(usize, question_mark_i)..@intCast(usize, raw_path.len)] else "", + .path = if (decoded_pathname.len == 1) "." else path, + .query_string = if (question_mark_i > -1) decoded_pathname[@intCast(usize, question_mark_i)..@intCast(usize, decoded_pathname.len)] else "", }; } diff --git a/src/javascript/jsc/api/router.zig b/src/javascript/jsc/api/router.zig index e2283e9c5..84562fee3 100644 --- a/src/javascript/jsc/api/router.zig +++ b/src/javascript/jsc/api/router.zig @@ -16,6 +16,7 @@ const Fs = @import("../../../fs.zig"); route: *const FilesystemRouter.Match, query_string_map: ?QueryStringMap = null, +param_map: ?QueryStringMap = null, script_src: ?string = null, script_src_buf: [1024]u8 = undefined, @@ -223,6 +224,23 @@ pub const Instance = NewClass( .ts = d.ts{ .@"return" = "Record<string, string | string[]>", .tsdoc = + \\Route parameters & parsed query string values as a key-value object + \\ + \\@example + \\```js + \\console.assert(router.query.id === "123"); + \\console.assert(router.pathname === "/blog/posts/123"); + \\console.assert(router.route === "blog/posts/[id]"); + \\``` + , + }, + }, + .params = .{ + .@"get" = getParams, + .ro = true, + .ts = d.ts{ + .@"return" = "Record<string, string | string[]>", + .tsdoc = \\Route parameters as a key-value object \\ \\@example @@ -372,6 +390,34 @@ pub fn getScriptSrc( return js.JSValueMakeString(ctx, ZigString.init(src).toJSStringRef()); } +pub fn getParams( + this: *Router, + ctx: js.JSContextRef, + thisObject: js.JSObjectRef, + prop: js.JSStringRef, + exception: js.ExceptionRef, +) js.JSValueRef { + if (this.param_map == null) { + if (this.route.params.len > 0) { + if (QueryStringMap.initWithScanner(getAllocator(ctx), CombinedScanner.init( + "", + this.route.pathnameWithoutLeadingSlash(), + this.route.name, + this.route.params, + ))) |map| { + this.param_map = map; + } else |err| {} + } + } + + // If it's still null, there are no params + if (this.param_map) |*map| { + return createQueryObject(ctx, map, exception); + } else { + return JSValue.createEmptyObject(VirtualMachine.vm.global, 0).asRef(); + } +} + pub fn getQuery( this: *Router, ctx: js.JSContextRef, diff --git a/src/javascript/jsc/base.zig b/src/javascript/jsc/base.zig index 4902c731a..e17d4c7ab 100644 --- a/src/javascript/jsc/base.zig +++ b/src/javascript/jsc/base.zig @@ -205,6 +205,8 @@ pub const Properties = struct { pub const default: string = "default"; pub const include: string = "include"; + pub const env: string = "env"; + pub const GET = "GET"; pub const PUT = "PUT"; pub const POST = "POST"; @@ -278,6 +280,8 @@ pub const Properties = struct { pub var navigate: js.JSStringRef = undefined; pub var follow: js.JSStringRef = undefined; + + pub const env: js.JSStringRef = undefined; }; pub fn init() void { diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index fbe3c22ae..45a68954e 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -121,9 +121,40 @@ void GlobalObject::setConsole(void *console) { this->setConsoleClient(makeWeakPtr(m_console)); } +// This is not a publicly exposed API currently. +// This is used by the bundler to make Response, Request, FetchEvent, +// and any other objects available globally. void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) { WTF::Vector<GlobalPropertyInfo> extraStaticGlobals; - extraStaticGlobals.reserveCapacity((size_t)count); + extraStaticGlobals.reserveCapacity((size_t)count + 1); + + // This is not nearly a complete implementation. It's just enough to make some npm packages that + // were compiled with Webpack to run without crashing in this environment. + JSC::JSObject *process = JSC::constructEmptyObject(this, this->objectPrototype(), 4); + + // The transpiler inlines all defined process.env vars & dead code eliminates as relevant + // so this is just to return undefined for any missing ones and not crash if something tries to + // modify it or it wasn't statically analyzable + JSC::JSObject *processDotEnv = JSC::constructEmptyObject(this, this->objectPrototype(), 0); + + process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "env"), processDotEnv); + + // this should be transpiled out, but just incase + process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "browser"), + JSC::JSValue(false)); + + // this gives some way of identifying at runtime whether the SSR is happening in node or not. + // this should probably be renamed to what the name of the bundler is, instead of "notNodeJS" + // but it must be something that won't evaluate to truthy in Node.js + process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "notNodeJS"), + JSC::JSValue(true)); +#if defined(__APPLE__) + process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "platform"), + JSC::jsString(this->vm(), WTF::String("darwin"))); +#else + process->putDirect(this->vm(), JSC::Identifier::fromString(this->vm(), "platform"), + JSC::jsString(this->vm(), WTF::String("linux"))); +#endif for (int i = 0; i < count; i++) { auto jsClass = globals[i]; @@ -137,7 +168,12 @@ void GlobalObject::installAPIGlobals(JSClassRef *globals, int count) { GlobalPropertyInfo{JSC::Identifier::fromString(vm(), jsClass->className()), JSC::JSValue(object), JSC::PropertyAttribute::DontDelete | 0}); } - this->addStaticGlobals(extraStaticGlobals.data(), count); + + extraStaticGlobals.uncheckedAppend( + GlobalPropertyInfo{JSC::Identifier::fromString(vm(), "process"), JSC::JSValue(process), + JSC::PropertyAttribute::DontDelete | 0}); + + this->addStaticGlobals(extraStaticGlobals.data(), extraStaticGlobals.size()); extraStaticGlobals.releaseBuffer(); } diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index a2d9220ad..0b96ca3ed 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -117,7 +117,7 @@ pub const Wundle = struct { prop: js.JSStringRef, exception: js.ExceptionRef, ) js.JSValueRef { - if (!VirtualMachine.vm.bundler.options.routes.routes_enabled or VirtualMachine.vm.bundler.options.routes.dir.len > 0) { + if (!VirtualMachine.vm.bundler.options.routes.routes_enabled or VirtualMachine.vm.bundler.options.routes.dir.len == 0) { return js.JSValueMakeUndefined(ctx); } @@ -238,6 +238,7 @@ pub const VirtualMachine = struct { log: *logger.Log, event_listeners: EventListenerMixin.Map, main: string = "", + process: js.JSObjectRef = null, pub var vm_loaded = false; pub var vm: *VirtualMachine = undefined; diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 8b9649aa2..f6b59141c 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -70,7 +70,7 @@ pub const ImportScanner = struct { var stmt: Stmt = _stmt; switch (stmt.data) { .s_import => |st| { - var record: ImportRecord = p.import_records.items[st.import_record_index]; + var record: *ImportRecord = &p.import_records.items[st.import_record_index]; // The official TypeScript compiler always removes unused imported // symbols. However, we deliberately deviate from the official @@ -285,7 +285,7 @@ pub const ImportScanner = struct { // it's really stupid to import all 1,000 components from that design system // when you just want <Button /> const namespace_ref = st.namespace_ref; - const convert_star_to_clause = p.symbols.items[namespace_ref.inner_index].use_count_estimate == 0; + const convert_star_to_clause = p.symbols.items[namespace_ref.inner_index].use_count_estimate == 0 and st.default_name == null; if (convert_star_to_clause and !keep_unused_imports) { st.star_name_loc = null; @@ -1879,7 +1879,7 @@ pub const Parser = struct { var decls = try p.allocator.alloc(G.Decl, decls_count); var jsx_part_stmts = try p.allocator.alloc(Stmt, stmts_count); // Use the same array for storing the require call target of potentially both JSX runtimes - var require_call_args_base = p.allocator.alloc(Expr, imports_count) catch unreachable; + var require_call_args_base = p.allocator.alloc(Expr, if (p.options.can_import_from_bundle) 0 else imports_count) catch unreachable; var import_records = try p.allocator.alloc(u32, imports_count); var decl_i: usize = 0; @@ -1889,16 +1889,21 @@ pub const Parser = struct { var stmt_i: usize = 0; if (jsx_symbol.use_count_estimate > 0) { - require_call_args_base[require_call_args_i] = p.e(E.Identifier{ .ref = automatic_namespace_ref }, loc); - require_call_args_i += 1; - - var require_call_args = require_call_args_base[0..require_call_args_i]; - declared_symbols[declared_symbols_i] = .{ .ref = p.jsx_runtime_ref, .is_top_level = true }; declared_symbols_i += 1; declared_symbols[declared_symbols_i] = .{ .ref = automatic_namespace_ref, .is_top_level = true }; declared_symbols_i += 1; - var require_call = p.callRequireOrBundledRequire(require_call_args); + + const automatic_identifier = p.e(E.Identifier{ .ref = automatic_namespace_ref }, loc); + const dot_call_target = brk: { + if (p.options.can_import_from_bundle) { + break :brk automatic_identifier; + } else { + require_call_args_base[require_call_args_i] = automatic_identifier; + require_call_args_i += 1; + break :brk p.callUnbundledRequire(require_call_args_base[0..require_call_args_i]); + } + }; decls[decl_i] = G.Decl{ .binding = p.b( @@ -1909,7 +1914,7 @@ pub const Parser = struct { ), .value = p.e( E.Dot{ - .target = require_call, + .target = dot_call_target, .name = p.options.jsx.jsx, .name_loc = loc, .can_be_removed_if_unused = true, @@ -1938,6 +1943,7 @@ pub const Parser = struct { // When everything is CommonJS // We import JSX like this: // var {jsxDev} = require("react/jsx-dev") + jsx_part_stmts[stmt_i] = p.s(S.Import{ .namespace_ref = automatic_namespace_ref, .star_name_loc = loc, @@ -1962,9 +1968,20 @@ pub const Parser = struct { } if (jsx_classic_symbol.use_count_estimate > 0) { - require_call_args_base[require_call_args_i] = p.e(E.Identifier{ .ref = classic_namespace_ref }, loc); - var require_call_args = require_call_args_base[require_call_args_i..]; - var require_call = p.callRequireOrBundledRequire(require_call_args); + const classic_identifier = p.e(E.Identifier{ .ref = classic_namespace_ref }, loc); + + const dot_call_target = brk: { + // var react = $aopaSD123(); + + if (p.options.can_import_from_bundle) { + break :brk classic_identifier; + } else { + require_call_args_base[require_call_args_i] = classic_identifier; + require_call_args_i += 1; + break :brk p.callUnbundledRequire(require_call_args_base[0..require_call_args_i]); + } + }; + if (jsx_factory_symbol.use_count_estimate > 0) { declared_symbols[declared_symbols_i] = .{ .ref = p.jsx_factory_ref, .is_top_level = true }; declared_symbols_i += 1; @@ -1977,7 +1994,7 @@ pub const Parser = struct { ), .value = p.e( E.Dot{ - .target = require_call, + .target = dot_call_target, .name = p.options.jsx.factory[p.options.jsx.factory.len - 1], .name_loc = loc, .can_be_removed_if_unused = true, @@ -2000,7 +2017,7 @@ pub const Parser = struct { ), .value = p.e( E.Dot{ - .target = require_call, + .target = dot_call_target, .name = p.options.jsx.fragment[p.options.jsx.fragment.len - 1], .name_loc = loc, .can_be_removed_if_unused = true, @@ -2676,7 +2693,7 @@ pub fn NewParser( cjs_import_name, base_identifier_name, ); - std.mem.copy(u8, cjs_import_name[base_identifier_name.len..], suffix); + std.mem.copy(u8, cjs_import_name[base_identifier_name.len - 1 ..], suffix); const namespace_ref = p.declareSymbol(.hoisted, arg.loc, cjs_import_name) catch unreachable; @@ -2895,12 +2912,8 @@ pub fn NewParser( // If we're auto-importing JSX and it's bundled, we use the bundled version // This means we need to transform from require(react) to react() // unless we're building inside of speedy, then it's just normal commonjs - pub fn callRequireOrBundledRequire(p: *P, require_args: []Expr) Expr { - if (p.options.can_import_from_bundle) { - return p.e(E.Call{ .target = require_args[0] }, require_args[0].loc); - } else { - return p.callRuntime(require_args[0].loc, "__require", require_args); - } + pub fn callUnbundledRequire(p: *P, require_args: []Expr) Expr { + return p.callRuntime(require_args[0].loc, "__require", require_args); } pub fn recordExport(p: *P, loc: logger.Loc, alias: string, ref: Ref) !void { diff --git a/src/js_printer.zig b/src/js_printer.zig index 1f43479a6..8a5c81c8c 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -2835,9 +2835,15 @@ pub fn NewPrinter( } } else if (record.is_bundled) { p.print("import {"); + p.printLoadFromBundleWithoutCall(s.import_record_index); - p.print(" as "); - p.printSymbol(s.namespace_ref); + + if (!record.contains_import_star) { + p.print(" as "); + + p.printSymbol(s.namespace_ref); + } + p.print("} from "); p.printQuotedUTF8(record.path.text, false); p.printSemicolonAfterStatement(); @@ -2846,6 +2852,7 @@ pub fn NewPrinter( p.printIndent(); p.printSpaceBeforeIdentifier(); p.print("var {"); + for (s.items) |item, i| { p.print(item.alias); const name = p.renamer.nameForSymbol(item.name.ref.?); @@ -2859,22 +2866,43 @@ pub fn NewPrinter( } } p.print("} = "); - p.printSymbol(s.namespace_ref); + + if (!record.contains_import_star) { + p.printSymbol(s.namespace_ref); + } else { + p.printLoadFromBundleWithoutCall(s.import_record_index); + } p.print("()"); p.printSemicolonAfterStatement(); } else if (s.default_name) |default_name| { p.printIndent(); p.printSpaceBeforeIdentifier(); + p.print("var {default: "); + p.printSymbol(default_name.ref.?); p.print("} = "); + + if (!record.contains_import_star) { + p.printSymbol(s.namespace_ref); + } else { + p.printLoadFromBundleWithoutCall(s.import_record_index); + } + + p.print("()"); + p.printSemicolonAfterStatement(); + } else if (record.contains_import_star) { + p.print("var "); p.printSymbol(s.namespace_ref); + p.print(" = "); + p.printLoadFromBundleWithoutCall(s.import_record_index); p.print("()"); p.printSemicolonAfterStatement(); } return; } + p.print("import"); p.printSpace(); diff --git a/src/options.zig b/src/options.zig index f93937b24..a4302a703 100644 --- a/src/options.zig +++ b/src/options.zig @@ -250,6 +250,17 @@ pub const Platform = enum { speedy, node, + const browser_define_value_true = "true"; + const browser_define_value_false = "false"; + + pub fn processBrowserDefineValue(this: Platform) ?string { + return switch (this) { + .browser => browser_define_value_true, + .speedy, .node => browser_define_value_false, + else => null, + }; + } + pub fn isWebLike(platform: Platform) bool { return switch (platform) { .neutral, .browser => true, @@ -545,9 +556,20 @@ pub const DefaultUserDefines = struct { pub const Key = "process.env.NODE_ENV"; pub const Value = "\"development\""; }; + + pub const PlatformDefine = struct { + pub const Key = "process.browser"; + pub const Value = []string{ "false", "true" }; + }; }; -pub fn definesFromTransformOptions(allocator: *std.mem.Allocator, log: *logger.Log, _input_define: ?Api.StringMap, hmr: bool) !*defines.Define { +pub fn definesFromTransformOptions( + allocator: *std.mem.Allocator, + log: *logger.Log, + _input_define: ?Api.StringMap, + hmr: bool, + platform: Platform, +) !*defines.Define { var input_user_define = _input_define orelse std.mem.zeroes(Api.StringMap); var user_defines = try stringHashMapFromArrays( @@ -564,6 +586,12 @@ pub fn definesFromTransformOptions(allocator: *std.mem.Allocator, log: *logger.L try user_defines.put(DefaultUserDefines.HotModuleReloading.Key, DefaultUserDefines.HotModuleReloading.Value); } + // Automatically set `process.browser` to `true` for browsers and false for node+js + // This enables some extra dead code elimination + if (platform.processBrowserDefineValue()) |value| { + _ = try user_defines.getOrPutValue(DefaultUserDefines.PlatformDefine.Key, value); + } + var resolved_defines = try defines.DefineData.from_input(user_defines, log, allocator); return try defines.Define.init( allocator, @@ -672,7 +700,7 @@ pub const BundleOptions = struct { var opts: BundleOptions = BundleOptions{ .log = log, .resolve_mode = transform.resolve orelse .dev, - .define = try definesFromTransformOptions(allocator, log, transform.define, transform.serve orelse false), + .define = undefined, .loaders = try loadersFromTransformOptions(allocator, transform.loaders), .output_dir = try fs.absAlloc(allocator, &output_dir_parts), .platform = Platform.from(transform.platform), @@ -712,6 +740,8 @@ pub const BundleOptions = struct { else => {}, } + opts.define = try definesFromTransformOptions(allocator, log, transform.define, transform.serve orelse false, opts.platform); + if (!(transform.generate_node_module_bundle orelse false)) { if (node_modules_bundle_existing) |node_mods| { opts.node_modules_bundle = node_mods; diff --git a/src/runtime.js b/src/runtime.js index f065a1c4e..affd3bc4d 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -63,7 +63,7 @@ export var __commonJS = (cb, name) => { get() { return mod.exports; }, - enumerable: false, + enumerable: true, }); // If it's a namespace export without .default, pretend .default is the same as mod.exports } else if ( @@ -74,7 +74,7 @@ export var __commonJS = (cb, name) => { get() { return mod.exports; }, - enumerable: false, + enumerable: true, }); } diff --git a/src/runtime.version b/src/runtime.version index 1a0bb1fa9..4cda1e7bd 100644 --- a/src/runtime.version +++ b/src/runtime.version @@ -1 +1 @@ -ca3ca9f44c907d4a
\ No newline at end of file +7d529e0a92952d7a
\ No newline at end of file |