Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2ea3972
css: emit @layer statements that precede @import before the imported …
robobun Sep 6, 2026
f00b578
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Sep 13, 2026
d6fd4ac
css: scan the rule prefix once and shorten the comments
robobun Sep 13, 2026
b535d7d
ci: retrigger
robobun Sep 13, 2026
7190a15
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Oct 18, 2026
7f376f8
css: keep a layer list that prints as a block behind the hoisted exte…
robobun Oct 18, 2026
cd727fc
test: keep the layer tests clear of the external-import cases
robobun Oct 18, 2026
b7db12f
css: only a file with an @import gets the early layer entry, and its …
robobun Oct 19, 2026
e8af3c6
css: port esbuild's layer merging test, and mark where the hoist depa…
robobun Oct 19, 2026
1290f3d
css: do not strip inside an anonymous layer, and shorten comments
robobun Oct 19, 2026
2dc63d9
css: keep a layer list that another layer wraps, declare the layer of…
robobun Oct 20, 2026
e38e9fe
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Oct 21, 2026
c636745
test: a differential check of the cascade layers in bundled CSS
robobun Oct 21, 2026
b8d4a24
css: keep empty named @layer blocks as @layer statements
robobun Oct 21, 2026
ecbb1b5
test: pin random import graphs for the layer order, and the empty blo…
robobun Oct 21, 2026
5f26697
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Oct 21, 2026
d94ae00
css: shorten three comments
robobun Oct 21, 2026
da54e71
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Oct 23, 2026
21b1a6b
Revert "css: keep empty named @layer blocks as @layer statements"
robobun Oct 23, 2026
773b826
css: change nothing for a bundle with no @layer statement ahead of an…
robobun Oct 23, 2026
db16556
test: expectations for the layer check that is on only with a pre-imp…
robobun Oct 23, 2026
b3bb517
css: the hoist predicate keeps only what this change needs
robobun Oct 23, 2026
f6d6172
[autofix.ci] apply automated fixes
autofix-ci[bot] Oct 23, 2026
477ec6e
css: with a pre-import entry, declare the layer of a cyclic import an…
robobun Oct 23, 2026
d344bcd
css: a file keeps every copy only when a layer is first declared unde…
robobun Oct 23, 2026
93c82f7
css: narrow the changes that depend on a pre-import entry
robobun Oct 24, 2026
f9da5ec
css: shorten two comments and restore one comment of main
robobun Oct 24, 2026
1bae41a
css test: add the two-entry input that waits for #46357 as a todo case
robobun Oct 24, 2026
438aa2d
Merge remote-tracking branch 'origin/main' into robobun/b6517831/css-…
robobun Oct 27, 2026
e2a9317
css: keep the copy of a CSS module with composes under a layer, and f…
robobun Oct 27, 2026
8cf330a
css: name Layers::PreImport in the SAFETY comment of Chunk
robobun Oct 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions scripts/css-layer-fuzz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// bun scripts/css-layer-fuzz.ts <bun> <graphs> [first seed] [baseline bun]
// [--cycles] [--anonymous] [--no-leading-statements] [--empty-blocks]
// Runs test/bundler/css-layer-order.ts over many seeds against <bun> and prints the seeds where the bundle does not
// have the cascade layers of its sources. `<graphs>` = 1 prints the files of that seed too. With a baseline binary it
// also prints the seeds the baseline gets right and <bun> gets wrong, and how many are the other way round. It does
// that twice: for the whole check, and for the rule that `.t` gets alone, which is what a page shows.
import { resolve } from "node:path";
import {
checkGraph,
generateGraph,
isWinnerProblem,
type GraphOptions,
type GraphResult,
} from "../test/bundler/css-layer-order";

const known = ["--cycles", "--anonymous", "--no-leading-statements", "--empty-blocks"];
const flags = process.argv.slice(2).filter(arg => arg.startsWith("--"));
const [bun, count = "100", first = "1", baseline] = process.argv.slice(2).filter(arg => !arg.startsWith("--"));
if (!bun || flags.some(flag => !known.includes(flag))) {
console.error(
`usage: bun scripts/css-layer-fuzz.ts <bun> <graphs> [first seed] [baseline bun] [${known.join("] [")}]`,
);
process.exit(1);
}
const options: GraphOptions = {
cycles: flags.includes("--cycles"),
anonymous: flags.includes("--anonymous"),
leadingStatements: !flags.includes("--no-leading-statements"),
emptyBlocks: flags.includes("--empty-blocks"),
};
const locate = (binary: string) => Bun.which(binary) ?? resolve(binary);
const [target, before] = [locate(bun), baseline && locate(baseline)];
if (Number(count) === 1) {
for (const [name, css] of Object.entries(generateGraph(Number(first), options).files))
console.log(`/* ${name} */\n${css}`);
}
const tally = { ok: 0, wrong: 0, skipped: 0, wrongWinner: 0 };
const wrongWinner = (result: GraphResult) => result.status === "wrong" && result.problems.some(isWinnerProblem);
const regressed = { whole: [] as number[], winner: [] as number[] };
const repaired = { whole: 0, winner: 0 };
const parallel = 8;
const end = Number(first) + Number(count);
for (let seed = Number(first); seed < end; seed += parallel) {
const batch: Promise<[GraphResult, GraphResult | undefined]>[] = [];
for (let s = seed; s < Math.min(end, seed + parallel); s++) {
batch.push(Promise.all([checkGraph(target, s, options), before ? checkGraph(before, s, options) : undefined]));
}
for (const [result, old] of await Promise.all(batch)) {
tally[result.status]++;
if (wrongWinner(result)) tally.wrongWinner++;
if (result.status !== "ok")
console.log(`seed ${result.seed} ${result.status}:\n ` + result.problems.slice(0, 6).join("\n "));
if (!old || old.status === "skipped" || result.status === "skipped") continue;
if (old.status === "ok" && result.status === "wrong") regressed.whole.push(result.seed);
if (old.status === "wrong" && result.status === "ok") repaired.whole++;
if (!wrongWinner(old) && wrongWinner(result)) regressed.winner.push(result.seed);
if (wrongWinner(old) && !wrongWinner(result)) repaired.winner++;
}
}
if (before) {
const seeds = (list: number[]) => `${list.length} graphs` + (list.length ? ` (seeds ${list.join(", ")})` : "");
console.log(`right with the baseline, wrong with <bun>: ${seeds(regressed.whole)}`);
console.log(`wrong with the baseline, right with <bun>: ${repaired.whole} graphs`);
console.log(`.t gets the right rule with the baseline and another with <bun>: ${seeds(regressed.winner)}`);
console.log(`.t gets another rule with the baseline and the right one with <bun>: ${repaired.winner} graphs`);
}
console.log(JSON.stringify({ graphs: Number(count), firstSeed: Number(first), ...tally }));
34 changes: 30 additions & 4 deletions src/bundler/Chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Default for Content<'_> {

// SAFETY: `Chunk` is processed across the bundler thread pool (see
// `computeCrossChunkDependencies`, `generateChunksInParallel`). Raw-pointer
// fields (`Layers::Borrowed`, `ChunkRenamer` arena)
// fields (`Layers::Borrowed`, `Layers::PreImport`, `ChunkRenamer` arena)
// point into bundler-arena storage that outlives the
// pool join and is only mutated by the owning task; this mirrors
// `InputFile`'s blanket impls (bundle_v2.rs).
Expand Down Expand Up @@ -1397,6 +1397,7 @@ pub enum CssImportOrderKind<'a> {
/// We don't want to redundantly print the rules of these redundant imports
/// BUT, the imports may include layers.
/// We'll just print layer name declarations so that the original ordering is preserved.
/// `Layers::PreImport` is the other producer: the statements a file has ahead of its first "@import".
#[strum(serialize = "layers")]
Layers(Layers),
#[strum(serialize = "external_path")]
Expand All @@ -1414,13 +1415,15 @@ pub enum Layers {
/// Borrowed from another `CssImportOrder`'s `Layers` or the parsed stylesheet.
Borrowed(bun_ptr::BackRef<Vec<bun_css::LayerName>>),
Owned(Vec<bun_css::LayerName>),
/// A stylesheet's `layers_pre_import`, borrowed. The order passes keep or remove this entry, and never change it.
PreImport(bun_ptr::BackRef<Vec<bun_css::LayerName>>),
Comment thread
robobun marked this conversation as resolved.
Resolved
}

impl Layers {
#[inline]
pub(crate) fn inner(&self) -> &Vec<bun_css::LayerName> {
match self {
Layers::Borrowed(p) => p.get(),
Layers::Borrowed(p) | Layers::PreImport(p) => p.get(),
Layers::Owned(b) => b,
}
}
Expand All @@ -1439,6 +1442,12 @@ impl Layers {
Layers::Borrowed(bun_ptr::BackRef::from(p))
}

/// `borrow()` for a stylesheet's `layers_pre_import`.
#[inline]
pub(crate) fn pre_import(p: core::ptr::NonNull<Vec<bun_css::LayerName>>) -> Self {
Layers::PreImport(bun_ptr::BackRef::from(p))
}

/// Drop owned (arena-backed, so no-op) and
/// install a fresh owned value.
#[inline]
Expand All @@ -1449,17 +1458,34 @@ impl Layers {
/// If borrowed, deep-clone into an owned
/// list and return `&mut` to it; if already owned, return as-is.
pub(crate) fn to_owned(&mut self) -> &mut Vec<bun_css::LayerName> {
if let Layers::Borrowed(p) = *self {
if let Layers::Borrowed(p) | Layers::PreImport(p) = *self {
*self = Layers::Owned(p.deep_clone_with(|l| l.clone()));
}
match self {
Layers::Owned(b) => b,
Layers::Borrowed(_) => unreachable!(),
Layers::Borrowed(_) | Layers::PreImport(_) => unreachable!(),
}
}
}

impl CssImportOrder<'_> {
/// Whether this entry prints only "@layer" statements, which may precede an "@import".
pub(crate) fn prints_as_layer_statements(&self) -> bool {
let CssImportOrderKind::Layers(layers) = &self.kind else {
return false;
};
match self.conditions.slice() {
[] => true,
[only] => {
layers.inner().is_empty()
&& only.layer.as_ref().is_some_and(|layer| layer.v.is_some())
&& only.supports.is_none()
&& only.media.media_queries.is_empty()
}
_ => false,
}
}

pub(crate) fn hash<H: bun_core::Hasher + ?Sized>(&self, hasher: &mut H) {
// TODO: conditions, condition_import_records

Expand Down
Loading
Loading