Skip to content
1 change: 1 addition & 0 deletions docs/pm/cli/pm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ On a terminal, code is compared by meaning and shown as written. Both sides of e
- A comment edit, a changed type annotation, or any change in control flow is a change, shown in the author's words.
- Unchanged text whose meaning moved — code that became unreachable, or dead code that came alive because a condition elsewhere flipped — is marked `~`.
- Minified bundles have no readable original, so an un-minified re-print stands in for it (one declaration, call and property per line, each numbered by its `line:col` in the original) and the same folded comparison decides what changed — with the short names two builds' minifiers handed out differently renamed in lockstep; `dist/*.min.js` diffs down to the lines that actually changed rather than one enormous `-`/`+` pair. The re-print is used only when it parses back to the same program as the file; otherwise the file's own lines are shown. `.json` and `.css` files are compared and shown as their canonical print (`normalized`), so a reformat around one edit is one edit; identical prints read `formatting only`.
- In a `.css` file, a custom property, a value that holds `var()` and an unknown at-rule keep the tokens they were written with. The amount of whitespace, comments, and the blank next to a comma or next to `*` and `/` in `calc()` are formatting. A blank that appears or disappears anywhere else is a change: `--sel: .a .b` and `--sel: .a.b` are two values.
- The summary at the end calls out what a reviewer looks for first: new `install`/`postinstall` scripts, dependency and entry-point changes, a file that newly imports `child_process`/`fs`/`net`/`http`/`vm`/…, new package imports, growth in `eval()` / `new Function()` / `process.env` / `fetch()` use, newly executable files, a text file that became binary, and terminal escape sequences or bidirectional-text controls appearing in a release. For a published version, `bun install` takes dependencies, `bin`, `os` and `cpu` from the registry's entry for it: where that entry and the `package.json` in the tarball disagree, the summary shows both and goes by the registry. `*.map` files are summarised as regenerated rather than shown. Files over 64 MB are diffed as text (`too large to normalize`).
- Line tints follow the terminal: light or dark is taken from `COLORFGBG` or by asking the terminal for its background colour (OSC 11), and exact colours are used when `COLORTERM=truecolor`.
- Nothing in a package can drive your terminal: control characters, escape sequences and bidirectional-text overrides in file contents and names are drawn as `␛`, `^M`, `‹U+202E›`, with `NO_COLOR` too. Permission changes show as `mode 644 → 755` / `new file mode 100755`.
Expand Down
6 changes: 6 additions & 0 deletions src/css/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct PrinterOptions<'a> {
/// A mapping of pseudo classes to replace with class names that can be applied
/// from JavaScript. Useful for polyfills, for example.
pub pseudo_classes: Option<PseudoClasses<'a>>,
/// Print each raw token list in one form, for a diff. `bun pm diff` sets it.
pub canonical_token_lists: bool,
}

impl<'a> PrinterOptions<'a> {
Expand All @@ -50,6 +52,7 @@ impl<'a> PrinterOptions<'a> {
},
analyze_dependencies: None,
pseudo_classes: None,
canonical_token_lists: false,
}
}
}
Expand Down Expand Up @@ -120,6 +123,8 @@ pub struct Printer<'a> {
pub(crate) line: u32,
pub(crate) col: u32,
pub(crate) minify: bool,
/// Set by `minify` and by `canonical_token_lists`. The second alone is the diff print.
pub(crate) collapse_token_whitespace: bool,
pub(crate) targets: Targets,
pub(crate) vendor_prefix: css::VendorPrefix,
/// True while nested rules are being re-serialized for a non-final vendor
Expand Down Expand Up @@ -279,6 +284,7 @@ impl<'a> Printer<'a> {
sources: None,
dest,
minify: options.minify,
collapse_token_whitespace: options.minify || options.canonical_token_lists,
targets: options.targets,
dependencies: if options.analyze_dependencies.is_some() {
Some(BumpVec::new_in(arena))
Expand Down
381 changes: 213 additions & 168 deletions src/css/properties/custom.rs

Large diffs are not rendered by default.

30 changes: 18 additions & 12 deletions src/css_jsc/css_internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub(crate) enum TestKind {
Normal,
Minify,
Prefix,
/// The print that `bun pm diff` compares: no minify pass, canonical token lists.
Canonical,
}

#[derive(PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -58,6 +60,10 @@ pub fn _test(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
testing_impl(global, frame, TestKind::Normal, TestCategory::Normal)
}

pub fn canonical_test(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> {
testing_impl(global, frame, TestKind::Canonical, TestCategory::Normal)
}

/// Shared arg-validation for the test-only CSS internals: pulls the next arg,
/// throws "{fn_name}: expected {expected_n} arguments, got {got_n}" if absent,
/// throws "{fn_name}: expected {arg_label} to be a string" if not a string,
Expand Down Expand Up @@ -168,13 +174,16 @@ fn testing_impl(
) {
Ok(ret) => {
let (mut stylesheet, extra) = ret;
let mut minify_options = MinifyOptions::default();
minify_options.targets.browsers = browsers;
match stylesheet.minify(alloc, &minify_options, &extra) {
Ok(_) => {}
Err(err) => {
return Err(global
.throw_value(global.create_error_instance(format_args!("{}", err.kind))));
if test_kind != TestKind::Canonical {
let mut minify_options = MinifyOptions::default();
minify_options.targets.browsers = browsers;
match stylesheet.minify(alloc, &minify_options, &extra) {
Ok(_) => {}
Err(err) => {
return Err(global.throw_value(
global.create_error_instance(format_args!("{}", err.kind)),
));
}
}
}

Expand All @@ -183,11 +192,8 @@ fn testing_impl(
let result = match stylesheet.to_css(
alloc,
&PrinterOptions {
minify: match test_kind {
TestKind::Minify => true,
TestKind::Normal => false,
TestKind::Prefix => false,
},
minify: test_kind == TestKind::Minify,
canonical_token_lists: test_kind == TestKind::Canonical,
targets: Targets {
browsers,
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const cssInternals = {
minifyTest: $newRustFunction("css_internals.rs", "minifyTest", 3),
prefixTest: $newRustFunction("css_internals.rs", "prefixTest", 3),
_test: $newRustFunction("css_internals.rs", "_test", 3),
canonicalTest: $newRustFunction("css_internals.rs", "canonicalTest", 3),
attrTest: $newRustFunction("css_internals.rs", "attrTest", 3),
};

Expand Down
1 change: 1 addition & 0 deletions src/runtime/cli/pm_diff_normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ fn normalize_css(path: &[u8], bytes: &[u8]) -> Option<Normalized> {
alloc,
&bun_css::PrinterOptions {
minify: false,
canonical_token_lists: true,
..bun_css::PrinterOptions::default()
},
Some(bun_css::ImportInfo::init_outside_of_bundler(
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/dispatch_js2native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ pub(crate) fn bun_get_use_system_ca(

mod css {
pub(crate) use bun_css_jsc::css_internals::{
_test, attr_test, minify_error_test_with_options, minify_test, minify_test_with_options,
prefix_test, prefix_test_with_options, test_with_options,
_test, attr_test, canonical_test, minify_error_test_with_options, minify_test,
minify_test_with_options, prefix_test, prefix_test_with_options, test_with_options,
};
}
pub(crate) use css::_test as css_jsc_css_internals__test;
pub(crate) use css::attr_test as css_jsc_css_internals_attr_test;
pub(crate) use css::canonical_test as css_jsc_css_internals_canonical_test;
pub(crate) use css::minify_error_test_with_options as css_jsc_css_internals_minify_error_test_with_options;
pub(crate) use css::minify_test as css_jsc_css_internals_minify_test;
pub(crate) use css::minify_test_with_options as css_jsc_css_internals_minify_test_with_options;
Expand Down
16 changes: 15 additions & 1 deletion test/cli/install/bun-pm-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,17 @@ describe.concurrent("bun pm diff (canonical re-print)", () => {
"b/index.js": `"use strict"\n\nmodule.exports = function add(a, b) {\n\treturn a + b\n}\n`,
"a/style.css": `.a{color:red;margin:0 auto}`,
"b/style.css": `.a {\n color: red;\n margin: 0 auto;\n}\n`,
// Values that the CSS parser keeps as tokens, as a minifier prints them and as `bun fmt` prints them, with
// one value wrapped and commented by hand. The blank next to a comma and next to `*` and `/` in calc(),
// the amount of whitespace, and a comment are formatting.
"a/tokens.css": `.a{--shadow:0 0 1px red,0 0 2px blue;font-family:var(--f),sans-serif;color:rgba(1,2,3,var(--o));width:calc(var(--a)*2/3);margin:var(--x) auto}@apply a b;`,
"b/tokens.css": `.a {\n --shadow: 0 0 1px red, /* second layer */\n 0 0 2px blue;\n font-family: var(--f), sans-serif;\n color: rgba(1, 2, 3, var(--o));\n width: calc(var(--a) * 2 / 3);\n margin: var(--x) auto /* centered */;\n}\n@apply a b;\n`,
});
expect(text).toMatch(/\nindex\.js ─+ formatting only\n/);
expect(text).toMatch(/\npackage\.json ─+ formatting only\n/);
expect(text).toMatch(/\nstyle\.css ─+ formatting only\n/);
expect(text).toContain("3 formatting only");
expect(text).toMatch(/\ntokens\.css ─+ formatting only\n/);
expect(text).toContain("4 formatting only");
expect(changedLines(text)).toEqual([]);
expect(exitCode).toBe(0);
});
Expand Down Expand Up @@ -2156,6 +2162,9 @@ describe.concurrent("bun pm diff (engine invariants)", () => {
"b/s.css": ".a { color: blue }\n.b { margin: 0 }\n",
"a/bad.css": ".a{color:red}\n",
"b/bad.css": ".a{color:red\n.b{{{\n",
// A blank that is not next to a comma can be a descendant combinator, so one that goes or comes is a change.
"a/sel.css": ".a{--sel:.x .y;--w:var(--m)var(--n)}\n",
"b/sel.css": ".a{--sel:.x.y;--w:var(--m) var(--n)}\n",
"a/d.json": '{"a":1,"b":[1,2]}\n',
"b/d.json": '{\n "a": 2,\n "b": [1, 2]\n}\n',
"a/bad.json": '{"a":1}\n',
Expand All @@ -2166,6 +2175,11 @@ describe.concurrent("bun pm diff (engine invariants)", () => {
expect(text).toContain("│- color: red;");
expect(text).toContain("│+ color: #00f;");
expect(text).not.toContain("margin");
expect(text).toMatch(/\nsel\.css ─+ normalized \+2 -2\n/);
expect(text).toContain("│- --sel: .x .y;");
expect(text).toContain("│+ --sel: .x.y;");
expect(text).toContain("│- --w: var(--m)var(--n);");
expect(text).toContain("│+ --w: var(--m) var(--n);");
expect(text).toMatch(/\nd\.json ─+ normalized \+1 -1\n/);
expect(text).toContain('│+ "a": 2,');
expect(text).not.toMatch(/│[-+].*"b"/);
Expand Down
Loading
Loading