Fix unbundled imports

Former-commit-id: f221da115c1afcd136648c9683d8e9907005a128
This commit is contained in:
Jarred Sumner
2021-08-26 21:43:42 -07:00
parent 3a34f2e952
commit 92229ac0f6
11 changed files with 49 additions and 40 deletions

4
src/api/schema.d.ts vendored
View File

@@ -235,9 +235,9 @@ type uint32 = number;
export interface JavascriptBundleContainer { export interface JavascriptBundleContainer {
bundle_format_version?: uint32; bundle_format_version?: uint32;
bundle?: JavascriptBundle;
framework?: LoadedFramework;
routes?: LoadedRouteConfig; routes?: LoadedRouteConfig;
framework?: LoadedFramework;
bundle?: JavascriptBundle;
code_length?: uint32; code_length?: uint32;
} }

View File

@@ -373,16 +373,16 @@ function decodeJavascriptBundleContainer(bb) {
result["bundle_format_version"] = bb.readUint32(); result["bundle_format_version"] = bb.readUint32();
break; break;
case 2: case 3:
result["bundle"] = decodeJavascriptBundle(bb); result["routes"] = decodeLoadedRouteConfig(bb);
break; break;
case 3: case 2:
result["framework"] = decodeLoadedFramework(bb); result["framework"] = decodeLoadedFramework(bb);
break; break;
case 4: case 4:
result["routes"] = decodeLoadedRouteConfig(bb); result["bundle"] = decodeJavascriptBundle(bb);
break; break;
case 5: case 5:
@@ -403,22 +403,22 @@ function encodeJavascriptBundleContainer(message, bb) {
bb.writeUint32(value); bb.writeUint32(value);
} }
var value = message["bundle"]; var value = message["routes"];
if (value != null) { if (value != null) {
bb.writeByte(2); bb.writeByte(3);
encodeJavascriptBundle(value, bb); encodeLoadedRouteConfig(value, bb);
} }
var value = message["framework"]; var value = message["framework"];
if (value != null) { if (value != null) {
bb.writeByte(3); bb.writeByte(2);
encodeLoadedFramework(value, bb); encodeLoadedFramework(value, bb);
} }
var value = message["routes"]; var value = message["bundle"];
if (value != null) { if (value != null) {
bb.writeByte(4); bb.writeByte(4);
encodeLoadedRouteConfig(value, bb); encodeJavascriptBundle(value, bb);
} }
var value = message["code_length"]; var value = message["code_length"];

View File

@@ -100,10 +100,11 @@ struct JavascriptBundle {
message JavascriptBundleContainer { message JavascriptBundleContainer {
uint32 bundle_format_version = 1; 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; JavascriptBundle bundle = 4;
LoadedRouteConfig routes = 4;
// Don't technically need to store this, but it may be helpful as a sanity check // Don't technically need to store this, but it may be helpful as a sanity check
uint32 code_length = 5; uint32 code_length = 5;

View File

@@ -598,14 +598,14 @@ pub const JavascriptBundleContainer = struct {
/// bundle_format_version /// bundle_format_version
bundle_format_version: ?u32 = null, bundle_format_version: ?u32 = null,
/// bundle /// routes
bundle: ?JavascriptBundle = null, routes: ?LoadedRouteConfig = null,
/// framework /// framework
framework: ?LoadedFramework = null, framework: ?LoadedFramework = null,
/// routes /// bundle
routes: ?LoadedRouteConfig = null, bundle: ?JavascriptBundle = null,
/// code_length /// code_length
code_length: ?u32 = null, code_length: ?u32 = null,
@@ -622,13 +622,13 @@ pub fn decode(reader: anytype) anyerror!JavascriptBundleContainer {
this.bundle_format_version = try reader.readValue(u32); this.bundle_format_version = try reader.readValue(u32);
}, },
2 => { 2 => {
this.bundle = try reader.readValue(JavascriptBundle); this.routes = try reader.readValue(LoadedRouteConfig);
}, },
3 => { 3 => {
this.framework = try reader.readValue(LoadedFramework); this.framework = try reader.readValue(LoadedFramework);
}, },
4 => { 4 => {
this.routes = try reader.readValue(LoadedRouteConfig); this.bundle = try reader.readValue(JavascriptBundle);
}, },
5 => { 5 => {
this.code_length = try reader.readValue(u32); 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.writeFieldID(1);
try writer.writeInt(bundle_format_version); try writer.writeInt(bundle_format_version);
} }
if (this.bundle) |bundle| { if (this.routes) |routes| {
try writer.writeFieldID(2); try writer.writeFieldID(2);
try writer.writeValue(bundle); try writer.writeValue(routes);
} }
if (this.framework) |framework| { if (this.framework) |framework| {
try writer.writeFieldID(3); try writer.writeFieldID(3);
try writer.writeValue(framework); try writer.writeValue(framework);
} }
if (this.routes) |routes| { if (this.bundle) |bundle| {
try writer.writeFieldID(4); try writer.writeFieldID(4);
try writer.writeValue(routes); try writer.writeValue(bundle);
} }
if (this.code_length) |code_length| { if (this.code_length) |code_length| {
try writer.writeFieldID(5); try writer.writeFieldID(5);

View File

@@ -1714,7 +1714,7 @@ pub fn NewBundler(cache_files: bool) type {
opts.enable_bundling = false; opts.enable_bundling = false;
opts.transform_require_to_import = true; opts.transform_require_to_import = true;
opts.can_import_from_bundle = bundler.options.node_modules_bundle != null; 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.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.filepath_hash_for_hmr = file_hash orelse 0;
opts.warn_about_unbundled_modules = bundler.options.platform != .bun; opts.warn_about_unbundled_modules = bundler.options.platform != .bun;

View File

@@ -309,9 +309,11 @@ pub const Arguments = struct {
}; };
switch (comptime cmd) { switch (comptime cmd) {
.AutoCommand, .DevCommand, .BuildCommand => { .AutoCommand, .DevCommand, .BuildCommand, .BunCommand => {
if (args.option("--public-dir")) |public_dir| { 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 => {}, else => {},

View File

@@ -953,7 +953,7 @@ pub const PathName = struct {
pub fn nonUniqueNameString(self: *const PathName, allocator: *std.mem.Allocator) !string { pub fn nonUniqueNameString(self: *const PathName, allocator: *std.mem.Allocator) !string {
if (strings.eqlComptime(self.base, "index")) { if (strings.eqlComptime(self.base, "index")) {
if (self.dir.len > 0) { 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.text == b.text and (a.flags < b.flags ||
(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" { test "PathName.init" {

View File

@@ -1973,7 +1973,8 @@ pub const Parser = struct {
decl_i += 1; 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 // When everything is CommonJS
// We import JSX like this: // We import JSX like this:
// var {jsxDev} = require("react/jsx-dev") // var {jsxDev} = require("react/jsx-dev")
@@ -2810,7 +2811,7 @@ pub fn NewParser(
cjs_import_name, cjs_import_name,
base_identifier_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; const namespace_ref = p.declareSymbol(.hoisted, arg.loc, cjs_import_name) catch unreachable;

View File

@@ -2944,10 +2944,10 @@ pub fn NewPrinter(
if (p.options.runtime_imports.__require) |require_ref| { if (p.options.runtime_imports.__require) |require_ref| {
var module_name_buf: [256]u8 = undefined; var module_name_buf: [256]u8 = undefined;
var fixed_buf_allocator = std.heap.FixedBufferAllocator.init(&module_name_buf); 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..]; const module_name_segment = (fs.PathName.init(record.path.pretty).nonUniqueNameString(&fixed_buf_allocator.allocator) catch unreachable);
p.print("import * as "); p.print("import * as $$");
p.print(module_name_segment); p.print(module_name_segment);
p.print("_module from \""); p.print(" from \"");
p.print(record.path.text); p.print(record.path.text);
p.print("\";\n"); p.print("\";\n");
@@ -2956,9 +2956,9 @@ pub fn NewPrinter(
p.printSymbol(s.namespace_ref); p.printSymbol(s.namespace_ref);
p.print(" = "); p.print(" = ");
p.printSymbol(require_ref); p.printSymbol(require_ref);
p.print("("); p.print("($$");
p.print(module_name_segment); p.print(module_name_segment);
p.print("_module);\n"); p.print(");\n");
} }
if (s.default_name) |default_name| { if (s.default_name) |default_name| {
@@ -2966,9 +2966,9 @@ pub fn NewPrinter(
p.printSymbol(default_name.ref.?); p.printSymbol(default_name.ref.?);
p.print(" = "); p.print(" = ");
p.printSymbol(require_ref); p.printSymbol(require_ref);
p.print("("); p.print("($$");
p.print(module_name_segment); p.print(module_name_segment);
p.print("_module);\n"); p.print(");\n");
} }
return; return;

View File

@@ -321,7 +321,7 @@ pub fn NewLinker(comptime BundlerType: type) type {
// If it's a namespace import, assume it's safe. // 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. // 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. // 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; import_record.wrap_with_to_module = true;
result.ast.needs_runtime = true; result.ast.needs_runtime = true;
} }

View File

@@ -117,7 +117,8 @@ pub fn NewWatcher(comptime ContextType: type) type {
pub fn start(this: *Watcher) !void { pub fn start(this: *Watcher) !void {
_ = try this.getQueue(); _ = try this.getQueue();
std.debug.assert(this.watchloop_handle == null); 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 // This must only be called from the watcher thread