diff options
author | 2021-08-26 21:43:42 -0700 | |
---|---|---|
committer | 2021-08-26 21:43:42 -0700 | |
commit | 92229ac0f69b61ba008f222d9ac9728bbf03ada9 (patch) | |
tree | ba9727d39a6d7d2c472526f708166a9cd9c64751 | |
parent | 3a34f2e952869e7c05f2d129cd3c24101a46d814 (diff) | |
download | bun-92229ac0f69b61ba008f222d9ac9728bbf03ada9.tar.gz bun-92229ac0f69b61ba008f222d9ac9728bbf03ada9.tar.zst bun-92229ac0f69b61ba008f222d9ac9728bbf03ada9.zip |
Fix unbundled imports
Former-commit-id: f221da115c1afcd136648c9683d8e9907005a128
-rw-r--r-- | src/api/schema.d.ts | 4 | ||||
-rw-r--r-- | src/api/schema.js | 20 | ||||
-rw-r--r-- | src/api/schema.peechy | 7 | ||||
-rw-r--r-- | src/api/schema.zig | 20 | ||||
-rw-r--r-- | src/bundler.zig | 2 | ||||
-rw-r--r-- | src/cli.zig | 6 | ||||
-rw-r--r-- | src/fs.zig | 6 | ||||
-rw-r--r-- | src/js_parser/js_parser.zig | 5 | ||||
-rw-r--r-- | src/js_printer.zig | 14 | ||||
-rw-r--r-- | src/linker.zig | 2 | ||||
-rw-r--r-- | src/watcher.zig | 3 |
11 files changed, 49 insertions, 40 deletions
diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts index 304ff65d5..fc03c82b0 100644 --- a/src/api/schema.d.ts +++ b/src/api/schema.d.ts @@ -235,9 +235,9 @@ type uint32 = number; export interface JavascriptBundleContainer { bundle_format_version?: uint32; - bundle?: JavascriptBundle; - framework?: LoadedFramework; routes?: LoadedRouteConfig; + framework?: LoadedFramework; + bundle?: JavascriptBundle; code_length?: uint32; } diff --git a/src/api/schema.js b/src/api/schema.js index 6f7f60a73..0ae493e20 100644 --- a/src/api/schema.js +++ b/src/api/schema.js @@ -373,16 +373,16 @@ function decodeJavascriptBundleContainer(bb) { result["bundle_format_version"] = bb.readUint32(); break; - case 2: - result["bundle"] = decodeJavascriptBundle(bb); + case 3: + result["routes"] = decodeLoadedRouteConfig(bb); break; - case 3: + case 2: result["framework"] = decodeLoadedFramework(bb); break; case 4: - result["routes"] = decodeLoadedRouteConfig(bb); + result["bundle"] = decodeJavascriptBundle(bb); break; case 5: @@ -403,22 +403,22 @@ function encodeJavascriptBundleContainer(message, bb) { bb.writeUint32(value); } - var value = message["bundle"]; + var value = message["routes"]; if (value != null) { - bb.writeByte(2); - encodeJavascriptBundle(value, bb); + bb.writeByte(3); + encodeLoadedRouteConfig(value, bb); } var value = message["framework"]; if (value != null) { - bb.writeByte(3); + bb.writeByte(2); encodeLoadedFramework(value, bb); } - var value = message["routes"]; + var value = message["bundle"]; if (value != null) { bb.writeByte(4); - encodeLoadedRouteConfig(value, bb); + encodeJavascriptBundle(value, bb); } var value = message["code_length"]; diff --git a/src/api/schema.peechy b/src/api/schema.peechy index 552160be9..f296b6452 100644 --- a/src/api/schema.peechy +++ b/src/api/schema.peechy @@ -100,10 +100,11 @@ struct JavascriptBundle { message JavascriptBundleContainer { uint32 bundle_format_version = 1; - JavascriptBundle bundle = 2; + // These go first so if we change JavaScriptBundle we can still read these + LoadedRouteConfig routes = 3; + LoadedFramework framework = 2; - LoadedFramework framework = 3; - LoadedRouteConfig routes = 4; + JavascriptBundle bundle = 4; // Don't technically need to store this, but it may be helpful as a sanity check uint32 code_length = 5; diff --git a/src/api/schema.zig b/src/api/schema.zig index 6afbc02de..3e542b035 100644 --- a/src/api/schema.zig +++ b/src/api/schema.zig @@ -598,14 +598,14 @@ pub const JavascriptBundleContainer = struct { /// bundle_format_version bundle_format_version: ?u32 = null, -/// bundle -bundle: ?JavascriptBundle = null, +/// routes +routes: ?LoadedRouteConfig = null, /// framework framework: ?LoadedFramework = null, -/// routes -routes: ?LoadedRouteConfig = null, +/// bundle +bundle: ?JavascriptBundle = null, /// code_length code_length: ?u32 = null, @@ -622,13 +622,13 @@ pub fn decode(reader: anytype) anyerror!JavascriptBundleContainer { this.bundle_format_version = try reader.readValue(u32); }, 2 => { - this.bundle = try reader.readValue(JavascriptBundle); + this.routes = try reader.readValue(LoadedRouteConfig); }, 3 => { this.framework = try reader.readValue(LoadedFramework); }, 4 => { - this.routes = try reader.readValue(LoadedRouteConfig); + this.bundle = try reader.readValue(JavascriptBundle); }, 5 => { this.code_length = try reader.readValue(u32); @@ -646,17 +646,17 @@ if (this.bundle_format_version) |bundle_format_version| { try writer.writeFieldID(1); try writer.writeInt(bundle_format_version); } -if (this.bundle) |bundle| { +if (this.routes) |routes| { try writer.writeFieldID(2); - try writer.writeValue(bundle); + try writer.writeValue(routes); } if (this.framework) |framework| { try writer.writeFieldID(3); try writer.writeValue(framework); } -if (this.routes) |routes| { +if (this.bundle) |bundle| { try writer.writeFieldID(4); - try writer.writeValue(routes); + try writer.writeValue(bundle); } if (this.code_length) |code_length| { try writer.writeFieldID(5); diff --git a/src/bundler.zig b/src/bundler.zig index 1ffd25cef..4beef36c7 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -1714,7 +1714,7 @@ pub fn NewBundler(cache_files: bool) type { opts.enable_bundling = false; opts.transform_require_to_import = true; opts.can_import_from_bundle = bundler.options.node_modules_bundle != null; - opts.features.hot_module_reloading = bundler.options.hot_module_reloading and bundler.options.platform != .bun; // and client_entry_point_ == null; + opts.features.hot_module_reloading = bundler.options.hot_module_reloading and bundler.options.platform != .bun and (opts.can_import_from_bundle or !path.isNodeModule()); // and client_entry_point_ == null; opts.features.react_fast_refresh = opts.features.hot_module_reloading and jsx.parse and bundler.options.jsx.supports_fast_refresh; opts.filepath_hash_for_hmr = file_hash orelse 0; opts.warn_about_unbundled_modules = bundler.options.platform != .bun; diff --git a/src/cli.zig b/src/cli.zig index e77e76154..1937071d7 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -309,9 +309,11 @@ pub const Arguments = struct { }; switch (comptime cmd) { - .AutoCommand, .DevCommand, .BuildCommand => { + .AutoCommand, .DevCommand, .BuildCommand, .BunCommand => { if (args.option("--public-dir")) |public_dir| { - opts.router = Api.RouteConfig{ .extensions = &.{}, .dir = &.{}, .static_dir = public_dir }; + if (public_dir.len > 0) { + opts.router = Api.RouteConfig{ .extensions = &.{}, .dir = &.{}, .static_dir = public_dir }; + } } }, else => {}, diff --git a/src/fs.zig b/src/fs.zig index d5bec356e..3936ea933 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -953,7 +953,7 @@ pub const PathName = struct { pub fn nonUniqueNameString(self: *const PathName, allocator: *std.mem.Allocator) !string { if (strings.eqlComptime(self.base, "index")) { if (self.dir.len > 0) { - return MutableString.ensureValidIdentifier(PathName.init(self.dir).dir, allocator); + return MutableString.ensureValidIdentifier(PathName.init(self.dir).base, allocator); } } @@ -1059,6 +1059,10 @@ pub const Path = struct { (a.text == b.text and (a.flags < b.flags || (a.flags == b.flags))))); } + + pub fn isNodeModule(this: *const Path) bool { + return strings.lastIndexOf(this.name.dir, std.fs.path.sep_str ++ "node_modules" ++ std.fs.path.sep_str) != null; + } }; test "PathName.init" { diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 28e2e04b1..e1fbd2ca4 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -1973,7 +1973,8 @@ pub const Parser = struct { decl_i += 1; } - const import_record_id = p.addImportRecord(.require, loc, p.options.jsx.import_source); + // We do not mark this as .require becuase we are already wrapping it manually. + const import_record_id = p.addImportRecord(.internal, loc, p.options.jsx.import_source); // When everything is CommonJS // We import JSX like this: // var {jsxDev} = require("react/jsx-dev") @@ -2810,7 +2811,7 @@ pub fn NewParser( cjs_import_name, base_identifier_name, ); - std.mem.copy(u8, cjs_import_name[base_identifier_name.len - 1 ..], suffix); + std.mem.copy(u8, cjs_import_name[base_identifier_name.len..], suffix); const namespace_ref = p.declareSymbol(.hoisted, arg.loc, cjs_import_name) catch unreachable; diff --git a/src/js_printer.zig b/src/js_printer.zig index 4e8c8fa28..4cbf6759d 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -2944,10 +2944,10 @@ pub fn NewPrinter( if (p.options.runtime_imports.__require) |require_ref| { var module_name_buf: [256]u8 = undefined; var fixed_buf_allocator = std.heap.FixedBufferAllocator.init(&module_name_buf); - const module_name_segment = (fs.PathName.init(record.path.pretty).nonUniqueNameString(&fixed_buf_allocator.allocator) catch unreachable)[1..]; - p.print("import * as "); + const module_name_segment = (fs.PathName.init(record.path.pretty).nonUniqueNameString(&fixed_buf_allocator.allocator) catch unreachable); + p.print("import * as $$"); p.print(module_name_segment); - p.print("_module from \""); + p.print(" from \""); p.print(record.path.text); p.print("\";\n"); @@ -2956,9 +2956,9 @@ pub fn NewPrinter( p.printSymbol(s.namespace_ref); p.print(" = "); p.printSymbol(require_ref); - p.print("("); + p.print("($$"); p.print(module_name_segment); - p.print("_module);\n"); + p.print(");\n"); } if (s.default_name) |default_name| { @@ -2966,9 +2966,9 @@ pub fn NewPrinter( p.printSymbol(default_name.ref.?); p.print(" = "); p.printSymbol(require_ref); - p.print("("); + p.print("($$"); p.print(module_name_segment); - p.print("_module);\n"); + p.print(");\n"); } return; diff --git a/src/linker.zig b/src/linker.zig index c4c43a003..bebbe6a99 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -321,7 +321,7 @@ pub fn NewLinker(comptime BundlerType: type) type { // If it's a namespace import, assume it's safe. // We can do this in the printer instead of creating a bunch of AST nodes here. // But we need to at least tell the printer that this needs to happen. - if (import_record.kind == .stmt and resolved_import.shouldAssumeCommonJS(import_record)) { + if (result.ast.exports_kind != .cjs and (import_record.kind == .require or (import_record.kind == .stmt and resolved_import.shouldAssumeCommonJS(import_record)))) { import_record.wrap_with_to_module = true; result.ast.needs_runtime = true; } diff --git a/src/watcher.zig b/src/watcher.zig index b3cc4f27a..5b4a8431a 100644 --- a/src/watcher.zig +++ b/src/watcher.zig @@ -117,7 +117,8 @@ pub fn NewWatcher(comptime ContextType: type) type { pub fn start(this: *Watcher) !void { _ = try this.getQueue(); std.debug.assert(this.watchloop_handle == null); - _ = try std.Thread.spawn(.{}, Watcher.watchLoop, .{this}); + var thread = try std.Thread.spawn(.{}, Watcher.watchLoop, .{this}); + thread.setName("File Watcher") catch {}; } // This must only be called from the watcher thread |