aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-08 23:16:13 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-04-08 23:16:13 -0700
commit20cdb197e2ef81443850bc649008da956f3c53be (patch)
tree15a0dc15a246295701fd541964ba88cfb95458d6 /src
parent1f68b8ada07bb05ecc96e5bfe7fb00345c9e55ad (diff)
downloadbun-20cdb197e2ef81443850bc649008da956f3c53be.tar.gz
bun-20cdb197e2ef81443850bc649008da956f3c53be.tar.zst
bun-20cdb197e2ef81443850bc649008da956f3c53be.zip
Fix Next.js stylesheet bug
Diffstat (limited to 'src')
-rw-r--r--src/bundler.zig10
-rw-r--r--src/http.zig12
-rw-r--r--src/js_printer.zig12
-rw-r--r--src/linker.zig188
-rw-r--r--src/runtime/hmr.ts10
5 files changed, 139 insertions, 93 deletions
diff --git a/src/bundler.zig b/src/bundler.zig
index 63ebdcce5..bb6d73db9 100644
--- a/src/bundler.zig
+++ b/src/bundler.zig
@@ -1763,7 +1763,15 @@ pub const Bundler = struct {
const resolved_import: *const _resolver.Result = _resolved_import;
- const _module_data = BundledModuleData.getForceBundle(this, resolved_import) orelse unreachable;
+ const _module_data = BundledModuleData.getForceBundle(this, resolved_import) orelse {
+ // if a macro imports code that cannot be bundled
+ // we just silently disable it
+ // because...we need some kind of hook to say "don't bundle this"
+ import_record.path.is_disabled = true;
+ import_record.is_bundled = false;
+
+ continue;
+ };
import_record.module_id = _module_data.module_id;
std.debug.assert(import_record.module_id != 0);
import_record.is_bundled = true;
diff --git a/src/http.zig b/src/http.zig
index 22c7da30f..4a93da2b3 100644
--- a/src/http.zig
+++ b/src/http.zig
@@ -344,7 +344,15 @@ pub const RequestContext = struct {
bundler_parse_options,
@as(?*bundler.FallbackEntryPoint, &fallback_entry_point),
)) |*result| {
- try bundler_.linker.link(fallback_entry_point.source.path, result, this.origin, .absolute_url, false);
+ try bundler_.linker.linkAllowImportingFromBundle(
+ fallback_entry_point.source.path,
+ result,
+ this.origin,
+ .absolute_url,
+ false,
+ false,
+ );
+
var buffer_writer = try JSPrinter.BufferWriter.init(default_allocator);
var writer = JSPrinter.BufferPrinter.init(buffer_writer);
_ = try bundler_.print(
@@ -3316,7 +3324,7 @@ pub const Server = struct {
const kinds = slice.items(.kind);
const hashes = slice.items(.hash);
var file_descriptors = slice.items(.fd);
- var header = fbs.getWritten();
+ const header = fbs.getWritten();
defer ctx.watcher.flushEvictions();
defer Output.flush();
diff --git a/src/js_printer.zig b/src/js_printer.zig
index 2e03c9092..e4a641389 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -484,7 +484,7 @@ const ImportVariant = enum {
};
}
- pub fn determine(record: *const importRecord.ImportRecord, _: *const Symbol, s_import: *const S.Import) ImportVariant {
+ pub fn determine(record: *const importRecord.ImportRecord, s_import: *const S.Import) ImportVariant {
var variant = ImportVariant.path_only;
if (record.contains_import_star) {
@@ -3883,7 +3883,15 @@ pub fn NewPrinter(
const is_disabled = import_record.path.is_disabled;
const module_id = import_record.module_id;
- switch (ImportVariant.determine(&record, p.symbols.get(s.namespace_ref).?, s)) {
+ // If the bundled import was disabled and only imported for side effects
+ // we can skip it
+
+ if (record.path.is_disabled) {
+ if (p.symbols.get(s.namespace_ref) == null)
+ return;
+ }
+
+ switch (ImportVariant.determine(&record, s)) {
.path_only => {
if (!is_disabled) {
p.printCallModuleID(module_id);
diff --git a/src/linker.zig b/src/linker.zig
index 7d0a3ac41..7bc682a17 100644
--- a/src/linker.zig
+++ b/src/linker.zig
@@ -195,6 +195,18 @@ pub const Linker = struct {
comptime import_path_format: Options.BundleOptions.ImportPathFormat,
comptime ignore_runtime: bool,
) !void {
+ return linkAllowImportingFromBundle(linker, file_path, result, origin, import_path_format, ignore_runtime, true);
+ }
+
+ pub fn linkAllowImportingFromBundle(
+ linker: *ThisLinker,
+ file_path: Fs.Path,
+ result: *_bundler.ParseResult,
+ origin: URL,
+ comptime import_path_format: Options.BundleOptions.ImportPathFormat,
+ comptime ignore_runtime: bool,
+ comptime allow_import_from_bundle: bool,
+ ) !void {
const source_dir = file_path.sourceDir();
var externals = std.ArrayList(u32).init(linker.allocator);
var needs_bundle = false;
@@ -293,34 +305,36 @@ pub const Linker = struct {
}
}
- if (linker.options.node_modules_bundle) |node_modules_bundle| {
- if (Resolver.isPackagePath(import_record.path.text)) {
- const text = import_record.path.text;
+ if (comptime allow_import_from_bundle) {
+ if (linker.options.node_modules_bundle) |node_modules_bundle| {
+ if (Resolver.isPackagePath(import_record.path.text)) {
+ const text = import_record.path.text;
- var package_name = text;
- if (text[0] == '@') {
- if (std.mem.indexOfScalar(u8, text, '/')) |i| {
- if (std.mem.indexOfScalar(u8, text[i + 1 ..], '/')) |j| {
- package_name = text[0 .. i + 1 + j];
+ var package_name = text;
+ if (text[0] == '@') {
+ if (std.mem.indexOfScalar(u8, text, '/')) |i| {
+ if (std.mem.indexOfScalar(u8, text[i + 1 ..], '/')) |j| {
+ package_name = text[0 .. i + 1 + j];
+ }
+ }
+ } else {
+ if (std.mem.indexOfScalar(u8, text, '/')) |i| {
+ package_name = text[0..i];
}
}
- } else {
- if (std.mem.indexOfScalar(u8, text, '/')) |i| {
- package_name = text[0..i];
- }
- }
- if (package_name.len != text.len) {
- if (node_modules_bundle.getPackage(package_name)) |pkg| {
- const import_path = text[@minimum(text.len, package_name.len + 1)..];
- if (node_modules_bundle.findModuleIDInPackageIgnoringExtension(pkg, import_path)) |found_module| {
- import_record.is_bundled = true;
- node_module_bundle_import_path = node_module_bundle_import_path orelse
- linker.nodeModuleBundleImportPath(origin);
-
- import_record.path.text = node_module_bundle_import_path.?;
- import_record.module_id = node_modules_bundle.bundle.modules[found_module].id;
- needs_bundle = true;
- continue :outer;
+ if (package_name.len != text.len) {
+ if (node_modules_bundle.getPackage(package_name)) |pkg| {
+ const import_path = text[@minimum(text.len, package_name.len + 1)..];
+ if (node_modules_bundle.findModuleIDInPackageIgnoringExtension(pkg, import_path)) |found_module| {
+ import_record.is_bundled = true;
+ node_module_bundle_import_path = node_module_bundle_import_path orelse
+ linker.nodeModuleBundleImportPath(origin);
+
+ import_record.path.text = node_module_bundle_import_path.?;
+ import_record.module_id = node_modules_bundle.bundle.modules[found_module].id;
+ needs_bundle = true;
+ continue :outer;
+ }
}
}
}
@@ -332,21 +346,23 @@ pub const Linker = struct {
else => {},
// for fast refresh, attempt to read the version directly from the bundle instead of resolving it
.react_refresh => {
- if (linker.options.node_modules_bundle) |node_modules_bundle| {
- const runtime = linker.options.jsx.refresh_runtime;
- const package_name = runtime[0 .. strings.indexOfChar(runtime, '/') orelse runtime.len];
-
- if (node_modules_bundle.getPackage(package_name)) |pkg| {
- const import_path = runtime[@minimum(runtime.len, package_name.len + 1)..];
- if (node_modules_bundle.findModuleInPackage(pkg, import_path)) |found_module| {
- import_record.is_bundled = true;
- node_module_bundle_import_path = node_module_bundle_import_path orelse
- linker.nodeModuleBundleImportPath(origin);
-
- import_record.path.text = node_module_bundle_import_path.?;
- import_record.module_id = found_module.id;
- needs_bundle = true;
- continue :outer;
+ if (comptime allow_import_from_bundle) {
+ if (linker.options.node_modules_bundle) |node_modules_bundle| {
+ const runtime = linker.options.jsx.refresh_runtime;
+ const package_name = runtime[0 .. strings.indexOfChar(runtime, '/') orelse runtime.len];
+
+ if (node_modules_bundle.getPackage(package_name)) |pkg| {
+ const import_path = runtime[@minimum(runtime.len, package_name.len + 1)..];
+ if (node_modules_bundle.findModuleInPackage(pkg, import_path)) |found_module| {
+ import_record.is_bundled = true;
+ node_module_bundle_import_path = node_module_bundle_import_path orelse
+ linker.nodeModuleBundleImportPath(origin);
+
+ import_record.path.text = node_module_bundle_import_path.?;
+ import_record.module_id = found_module.id;
+ needs_bundle = true;
+ continue :outer;
+ }
}
}
}
@@ -390,54 +406,56 @@ pub const Linker = struct {
const loader = linker.options.loader(path.name.ext);
if (loader.isJavaScriptLikeOrJSON()) {
- bundled: {
- if (linker.options.node_modules_bundle) |node_modules_bundle| {
- const package_json = resolved_import.package_json orelse break :bundled;
- const package_base_dir = package_json.source.path.sourceDir();
- if (node_modules_bundle.getPackageIDByHash(package_json.hash)) |pkg_id| {
- const package = node_modules_bundle.bundle.packages[pkg_id];
-
- if (comptime Environment.isDebug) {
- std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name));
- std.debug.assert(strings.eql(node_modules_bundle.str(package.version), package_json.version));
- }
+ if (comptime allow_import_from_bundle) {
+ bundled: {
+ if (linker.options.node_modules_bundle) |node_modules_bundle| {
+ const package_json = resolved_import.package_json orelse break :bundled;
+ const package_base_dir = package_json.source.path.sourceDir();
+ if (node_modules_bundle.getPackageIDByHash(package_json.hash)) |pkg_id| {
+ const package = node_modules_bundle.bundle.packages[pkg_id];
+
+ if (comptime Environment.isDebug) {
+ std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name));
+ std.debug.assert(strings.eql(node_modules_bundle.str(package.version), package_json.version));
+ }
+
+ const package_relative_path = linker.fs.relative(
+ package_base_dir,
+ if (!strings.eqlComptime(path.namespace, "node")) path.pretty else path.text,
+ );
- const package_relative_path = linker.fs.relative(
- package_base_dir,
- if (!strings.eqlComptime(path.namespace, "node")) path.pretty else path.text,
- );
+ const found_module = node_modules_bundle.findModuleInPackage(&package, package_relative_path) orelse {
+ // linker.log.addErrorFmt(
+ // null,
+ // logger.Loc.Empty,
+ // linker.allocator,
+ // "New dependency import: \"{s}/{s}\"\nPlease run `bun bun` to update the .bun.",
+ // .{
+ // package_json.name,
+ // package_relative_path,
+ // },
+ // ) catch {};
+ break :bundled;
+ };
+
+ if (comptime Environment.isDebug) {
+ const module_path = node_modules_bundle.str(found_module.path);
+ std.debug.assert(
+ strings.eql(
+ module_path,
+ package_relative_path,
+ ),
+ );
+ }
- const found_module = node_modules_bundle.findModuleInPackage(&package, package_relative_path) orelse {
- // linker.log.addErrorFmt(
- // null,
- // logger.Loc.Empty,
- // linker.allocator,
- // "New dependency import: \"{s}/{s}\"\nPlease run `bun bun` to update the .bun.",
- // .{
- // package_json.name,
- // package_relative_path,
- // },
- // ) catch {};
- break :bundled;
- };
-
- if (comptime Environment.isDebug) {
- const module_path = node_modules_bundle.str(found_module.path);
- std.debug.assert(
- strings.eql(
- module_path,
- package_relative_path,
- ),
- );
+ import_record.is_bundled = true;
+ node_module_bundle_import_path = node_module_bundle_import_path orelse
+ linker.nodeModuleBundleImportPath(origin);
+ import_record.path.text = node_module_bundle_import_path.?;
+ import_record.module_id = found_module.id;
+ needs_bundle = true;
+ continue;
}
-
- import_record.is_bundled = true;
- node_module_bundle_import_path = node_module_bundle_import_path orelse
- linker.nodeModuleBundleImportPath(origin);
- import_record.path.text = node_module_bundle_import_path.?;
- import_record.module_id = found_module.id;
- needs_bundle = true;
- continue;
}
}
}
diff --git a/src/runtime/hmr.ts b/src/runtime/hmr.ts
index 14350146e..afffc4847 100644
--- a/src/runtime/hmr.ts
+++ b/src/runtime/hmr.ts
@@ -1338,9 +1338,11 @@ if (typeof window !== "undefined") {
// we cannot export new modules. we can only mutate existing ones.
const oldGraphUsed = HMRModule.dependencies.graph_used;
- var oldModule = HMRModule.dependencies.modules[this.module_index];
+ var oldModule =
+ HMRModule.dependencies.modules.length > this.module_index &&
+ HMRModule.dependencies.modules[this.module_index];
HMRModule.dependencies = orig_deps.fork(this.module_index);
- var blobURL = null;
+ var blobURL = "";
// We inject the source map URL into the end of the file.
// We do that here for a few reasons:
@@ -1366,6 +1368,8 @@ if (typeof window !== "undefined") {
blobURL = URL.createObjectURL(blob);
HMRModule.dependencies.blobToID.set(blobURL, this.module_id);
await import(blobURL);
+ this.bytes = null;
+ URL.revokeObjectURL(blobURL);
this.timings.import = performance.now() - importStart;
} catch (exception) {
HMRModule.dependencies = orig_deps;
@@ -1392,7 +1396,7 @@ if (typeof window !== "undefined") {
// If we do import a new module, we have to do a full page reload for now
}
- URL.revokeObjectURL(blobURL);
+ blobURL = "";
// Ensure we don't keep the bytes around longer than necessary
this.bytes = null;