diff --git a/docs/bundler/css.md b/docs/bundler/css.md new file mode 100644 index 00000000000000..b5b2c00c4e464d --- /dev/null +++ b/docs/bundler/css.md @@ -0,0 +1,1030 @@ +# CSS + +Bun's bundler has built-in support for CSS with the following features: + +- Transpiling modern/feature features to work on all browsers (including vendor prefixing) +- Minification +- CSS Modules +- Tailwind (via a native bundler plugin) + +## Transpiling + +Bun's CSS bundler lets you use future/modern CSS features without having to worry about browser compatibility — all thanks to its transpiling and vendor prefixing features which are enabled by default. + +Bun's CSS parser and bundler is a direct Rust → Zig port of [LightningCSS](https://lightningcss.dev/), with a bundling approach inspired by esbuild. The transpiler converts modern CSS syntax into backwards-compatible equivalents that work across browsers. + +A huge thanks goes to the amazing work from the authors of [LightningCSS](https://lightningcss.dev/) and [esbuild](https://esbuild.github.io/). + +### Browser Compatibility + +By default, Bun's CSS bundler targets the following browsers: + +- ES2020 +- Edge 88+ +- Firefox 78+ +- Chrome 87+ +- Safari 14+ + +### Syntax Lowering + +#### Nesting + +The CSS Nesting specification allows you to write more concise and intuitive stylesheets by nesting selectors inside one another. Instead of repeating parent selectors across your CSS file, you can write child styles directly within their parent blocks. + +```css +/* With nesting */ +.card { + background: white; + border-radius: 4px; + + .title { + font-size: 1.2rem; + font-weight: bold; + } + + .content { + padding: 1rem; + } +} +``` + +Bun's CSS bundler automatically converts this nested syntax into traditional flat CSS that works in all browsers: + +```css +/* Compiled output */ +.card { + background: white; + border-radius: 4px; +} + +.card .title { + font-size: 1.2rem; + font-weight: bold; +} + +.card .content { + padding: 1rem; +} +``` + +You can also nest media queries and other at-rules inside selectors, eliminating the need to repeat selector patterns: + +```css +.responsive-element { + display: block; + + @media (min-width: 768px) { + display: flex; + } +} +``` + +This compiles to: + +```css +.responsive-element { + display: block; +} + +@media (min-width: 768px) { + .responsive-element { + display: flex; + } +} +``` + +#### Color mix + +The `color-mix()` function gives you an easy way to blend two colors together according to a specified ratio in a chosen color space. This powerful feature lets you create color variations without manually calculating the resulting values. + +```css +.button { + /* Mix blue and red in the RGB color space with a 30/70 proportion */ + background-color: color-mix(in srgb, blue 30%, red); + + /* Create a lighter variant for hover state */ + &:hover { + background-color: color-mix(in srgb, blue 30%, red, white 20%); + } +} +``` + +Bun's CSS bundler evaluates these color mixes at build time when all color values are known (not CSS variables), generating static color values that work in all browsers: + +```css +.button { + /* Computed to the exact resulting color */ + background-color: #b31a1a; +} + +.button:hover { + background-color: #c54747; +} +``` + +This feature is particularly useful for creating color systems with programmatically derived shades, tints, and accents without needing preprocessors or custom tooling. + +#### Relative colors + +CSS now allows you to modify individual components of a color using relative color syntax. This powerful feature lets you create color variations by adjusting specific attributes like lightness, saturation, or individual channels without having to recalculate the entire color. + +```css +.theme-color { + /* Start with a base color and increase lightness by 15% */ + --accent: lch(from purple calc(l + 15%) c h); + + /* Take our brand blue and make a desaturated version */ + --subtle-blue: oklch(from var(--brand-blue) l calc(c * 0.8) h); +} +``` + +Bun's CSS bundler computes these relative color modifications at build time (when not using CSS variables) and generates static color values for browser compatibility: + +```css +.theme-color { + --accent: lch(69.32% 58.34 328.37); + --subtle-blue: oklch(60.92% 0.112 240.01); +} +``` + +This approach is extremely useful for theme generation, creating accessible color variants, or building color scales based on mathematical relationships instead of hard-coding each value. + +#### LAB colors + +Modern CSS supports perceptually uniform color spaces like LAB, LCH, OKLAB, and OKLCH that offer significant advantages over traditional RGB. These color spaces can represent colors outside the standard RGB gamut, resulting in more vibrant and visually consistent designs. + +```css +.vibrant-element { + /* A vibrant red that exceeds sRGB gamut boundaries */ + color: lab(55% 78 35); + + /* A smooth gradient using perceptual color space */ + background: linear-gradient( + to right, + oklch(65% 0.25 10deg), + oklch(65% 0.25 250deg) + ); +} +``` + +Bun's CSS bundler automatically converts these advanced color formats to backwards-compatible alternatives for browsers that don't yet support them: + +```css +.vibrant-element { + /* Fallback to closest RGB approximation */ + color: #ff0f52; + /* P3 fallback for browsers with wider gamut support */ + color: color(display-p3 1 0.12 0.37); + /* Original value preserved for browsers that support it */ + color: lab(55% 78 35); + + background: linear-gradient(to right, #cd4e15, #3887ab); + background: linear-gradient( + to right, + oklch(65% 0.25 10deg), + oklch(65% 0.25 250deg) + ); +} +``` + +This layered approach ensures optimal color rendering across all browsers while allowing you to use the latest color technologies in your designs. + +#### Color function + +The `color()` function provides a standardized way to specify colors in various predefined color spaces, expanding your design options beyond the traditional RGB space. This allows you to access wider color gamuts and create more vibrant designs. + +```css +.vivid-element { + /* Using the Display P3 color space for wider gamut colors */ + color: color(display-p3 1 0.1 0.3); + + /* Using A98 RGB color space */ + background-color: color(a98-rgb 0.44 0.5 0.37); +} +``` + +For browsers that don't support these advanced color functions yet, Bun's CSS bundler provides appropriate RGB fallbacks: + +```css +.vivid-element { + /* RGB fallback first for maximum compatibility */ + color: #fa1a4c; + /* Keep original for browsers that support it */ + color: color(display-p3 1 0.1 0.3); + + background-color: #6a805d; + background-color: color(a98-rgb 0.44 0.5 0.37); +} +``` + +This functionality lets you use modern color spaces immediately while ensuring your designs remain functional across all browsers, with optimal colors displayed in supporting browsers and reasonable approximations elsewhere. + +#### HWB colors + +The HWB (Hue, Whiteness, Blackness) color model provides an intuitive way to express colors based on how much white or black is mixed with a pure hue. Many designers find this approach more natural for creating color variations compared to manipulating RGB or HSL values. + +```css +.easy-theming { + /* Pure cyan with no white or black added */ + --primary: hwb(180 0% 0%); + + /* Same hue, but with 20% white added (tint) */ + --primary-light: hwb(180 20% 0%); + + /* Same hue, but with 30% black added (shade) */ + --primary-dark: hwb(180 0% 30%); + + /* Muted version with both white and black added */ + --primary-muted: hwb(180 30% 20%); +} +``` + +Bun's CSS bundler automatically converts HWB colors to RGB for compatibility with all browsers: + +```css +.easy-theming { + --primary: #00ffff; + --primary-light: #33ffff; + --primary-dark: #00b3b3; + --primary-muted: #339999; +} +``` + +The HWB model makes it particularly easy to create systematic color variations for design systems, providing a more intuitive approach to creating consistent tints and shades than working directly with RGB or HSL values. + +#### Color notation + +Modern CSS has introduced more intuitive and concise ways to express colors. Space-separated color syntax eliminates the need for commas in RGB and HSL values, while hex colors with alpha channels provide a compact way to specify transparency. + +```css +.modern-styling { + /* Space-separated RGB notation (no commas) */ + color: rgb(50 100 200); + + /* Space-separated RGB with alpha */ + border-color: rgba(100 50 200 / 75%); + + /* Hex with alpha channel (8 digits) */ + background-color: #00aaff80; + + /* HSL with simplified notation */ + box-shadow: 0 5px 10px hsl(200 50% 30% / 40%); +} +``` + +Bun's CSS bundler automatically converts these modern color formats to ensure compatibility with older browsers: + +```css +.modern-styling { + /* Converted to comma format for older browsers */ + color: rgb(50, 100, 200); + + /* Alpha channels handled appropriately */ + border-color: rgba(100, 50, 200, 0.75); + + /* Hex+alpha converted to rgba when needed */ + background-color: rgba(0, 170, 255, 0.5); + + box-shadow: 0 5px 10px rgba(38, 115, 153, 0.4); +} +``` + +This conversion process lets you write cleaner, more modern CSS while ensuring your styles work correctly across all browsers. + +#### light-dark() color function + +The `light-dark()` function provides an elegant solution for implementing color schemes that respect the user's system preference without requiring complex media queries. This function accepts two color values and automatically selects the appropriate one based on the current color scheme context. + +```css +:root { + /* Define color scheme support */ + color-scheme: light dark; +} + +.themed-component { + /* Automatically picks the right color based on system preference */ + background-color: light-dark(#ffffff, #121212); + color: light-dark(#333333, #eeeeee); + border-color: light-dark(#dddddd, #555555); +} + +/* Override system preference when needed */ +.light-theme { + color-scheme: light; +} + +.dark-theme { + color-scheme: dark; +} +``` + +For browsers that don't support this feature yet, Bun's CSS bundler converts it to use CSS variables with proper fallbacks: + +```css +:root { + --lightningcss-light: initial; + --lightningcss-dark: ; + color-scheme: light dark; +} + +@media (prefers-color-scheme: dark) { + :root { + --lightningcss-light: ; + --lightningcss-dark: initial; + } +} + +.light-theme { + --lightningcss-light: initial; + --lightningcss-dark: ; + color-scheme: light; +} + +.dark-theme { + --lightningcss-light: ; + --lightningcss-dark: initial; + color-scheme: dark; +} + +.themed-component { + background-color: var(--lightningcss-light, #ffffff) + var(--lightningcss-dark, #121212); + color: var(--lightningcss-light, #333333) var(--lightningcss-dark, #eeeeee); + border-color: var(--lightningcss-light, #dddddd) + var(--lightningcss-dark, #555555); +} +``` + +This approach gives you a clean way to handle light and dark themes without duplicating styles or writing complex media queries, while maintaining compatibility with browsers that don't yet support the feature natively. + +#### Logical properties + +CSS logical properties let you define layout, spacing, and sizing relative to the document's writing mode and text direction rather than physical screen directions. This is crucial for creating truly international layouts that automatically adapt to different writing systems. + +```css +.multilingual-component { + /* Margin that adapts to writing direction */ + margin-inline-start: 1rem; + + /* Padding that makes sense regardless of text direction */ + padding-block: 1rem 2rem; + + /* Border radius for the starting corner at the top */ + border-start-start-radius: 4px; + + /* Size that respects the writing mode */ + inline-size: 80%; + block-size: auto; +} +``` + +For browsers that don't fully support logical properties, Bun's CSS bundler compiles them to physical properties with appropriate directional adjustments: + +```css +/* For left-to-right languages */ +.multilingual-component:dir(ltr) { + margin-left: 1rem; + padding-top: 1rem; + padding-bottom: 2rem; + border-top-left-radius: 4px; + width: 80%; + height: auto; +} + +/* For right-to-left languages */ +.multilingual-component:dir(rtl) { + margin-right: 1rem; + padding-top: 1rem; + padding-bottom: 2rem; + border-top-right-radius: 4px; + width: 80%; + height: auto; +} +``` + +If the `:dir()` selector isn't supported, additional fallbacks are automatically generated to ensure your layouts work properly across all browsers and writing systems. This makes creating internationalized designs much simpler while maintaining compatibility with older browsers. + +#### :dir() selector + +The `:dir()` pseudo-class selector allows you to style elements based on their text direction (RTL or LTR), providing a powerful way to create direction-aware designs without JavaScript. This selector matches elements based on their directionality as determined by the document or explicit direction attributes. + +```css +/* Apply different styles based on text direction */ +.nav-arrow:dir(ltr) { + transform: rotate(0deg); +} + +.nav-arrow:dir(rtl) { + transform: rotate(180deg); +} + +/* Position elements based on text flow */ +.sidebar:dir(ltr) { + border-right: 1px solid #ddd; +} + +.sidebar:dir(rtl) { + border-left: 1px solid #ddd; +} +``` + +For browsers that don't support the `:dir()` selector yet, Bun's CSS bundler converts it to the more widely supported `:lang()` selector with appropriate language mappings: + +```css +/* Converted to use language-based selectors as fallback */ +.nav-arrow:lang(en, fr, de, es, it, pt, nl) { + transform: rotate(0deg); +} + +.nav-arrow:lang(ar, he, fa, ur) { + transform: rotate(180deg); +} + +.sidebar:lang(en, fr, de, es, it, pt, nl) { + border-right: 1px solid #ddd; +} + +.sidebar:lang(ar, he, fa, ur) { + border-left: 1px solid #ddd; +} +``` + +This conversion lets you write direction-aware CSS that works reliably across browsers, even those that don't yet support the `:dir()` selector natively. If multiple arguments to `:lang()` aren't supported, further fallbacks are automatically provided. + +#### :lang() selector + +The `:lang()` pseudo-class selector allows you to target elements based on the language they're in, making it easy to apply language-specific styling. Modern CSS allows the `:lang()` selector to accept multiple language codes, letting you group language-specific rules more efficiently. + +```css +/* Typography adjustments for CJK languages */ +:lang(zh, ja, ko) { + line-height: 1.8; + font-size: 1.05em; +} + +/* Different quote styles by language group */ +blockquote:lang(fr, it, es, pt) { + font-style: italic; +} + +blockquote:lang(de, nl, da, sv) { + font-weight: 500; +} +``` + +For browsers that don't support multiple arguments in the `:lang()` selector, Bun's CSS bundler converts this syntax to use the `:is()` selector to maintain the same behavior: + +```css +/* Multiple languages grouped with :is() for better browser support */ +:is(:lang(zh), :lang(ja), :lang(ko)) { + line-height: 1.8; + font-size: 1.05em; +} + +blockquote:is(:lang(fr), :lang(it), :lang(es), :lang(pt)) { + font-style: italic; +} + +blockquote:is(:lang(de), :lang(nl), :lang(da), :lang(sv)) { + font-weight: 500; +} +``` + +If needed, Bun can provide additional fallbacks for `:is()` as well, ensuring your language-specific styles work across all browsers. This approach simplifies creating internationalized designs with distinct typographic and styling rules for different language groups. + +#### :is() selector + +The `:is()` pseudo-class function (formerly `:matches()`) allows you to create more concise and readable selectors by grouping multiple selectors together. It accepts a selector list as its argument and matches if any of the selectors in that list match, significantly reducing repetition in your CSS. + +```css +/* Instead of writing these separately */ +/* +.article h1, +.article h2, +.article h3 { + margin-top: 1.5em; +} +*/ + +/* You can write this */ +.article :is(h1, h2, h3) { + margin-top: 1.5em; +} + +/* Complex example with multiple groups */ +:is(header, main, footer) :is(h1, h2, .title) { + font-family: "Heading Font", sans-serif; +} +``` + +For browsers that don't support `:is()`, Bun's CSS bundler provides fallbacks using vendor-prefixed alternatives: + +```css +/* Fallback using -webkit-any */ +.article :-webkit-any(h1, h2, h3) { + margin-top: 1.5em; +} + +/* Fallback using -moz-any */ +.article :-moz-any(h1, h2, h3) { + margin-top: 1.5em; +} + +/* Original preserved for modern browsers */ +.article :is(h1, h2, h3) { + margin-top: 1.5em; +} + +/* Complex example with fallbacks */ +:-webkit-any(header, main, footer) :-webkit-any(h1, h2, .title) { + font-family: "Heading Font", sans-serif; +} + +:-moz-any(header, main, footer) :-moz-any(h1, h2, .title) { + font-family: "Heading Font", sans-serif; +} + +:is(header, main, footer) :is(h1, h2, .title) { + font-family: "Heading Font", sans-serif; +} +``` + +It's worth noting that the vendor-prefixed versions have some limitations compared to the standardized `:is()` selector, particularly with complex selectors. Bun handles these limitations intelligently, only using prefixed versions when they'll work correctly. + +#### :not() selector + +The `:not()` pseudo-class allows you to exclude elements that match a specific selector. The modern version of this selector accepts multiple arguments, letting you exclude multiple patterns with a single, concise selector. + +```css +/* Select all buttons except primary and secondary variants */ +button:not(.primary, .secondary) { + background-color: #f5f5f5; + border: 1px solid #ddd; +} + +/* Apply styles to all headings except those inside sidebars or footers */ +h2:not(.sidebar *, footer *) { + margin-top: 2em; +} +``` + +For browsers that don't support multiple arguments in `:not()`, Bun's CSS bundler converts this syntax to a more compatible form while preserving the same behavior: + +```css +/* Converted to use :not with :is() for compatibility */ +button:not(:is(.primary, .secondary)) { + background-color: #f5f5f5; + border: 1px solid #ddd; +} + +h2:not(:is(.sidebar *, footer *)) { + margin-top: 2em; +} +``` + +And if `:is()` isn't supported, Bun can generate further fallbacks: + +```css +/* Even more fallbacks for maximum compatibility */ +button:not(:-webkit-any(.primary, .secondary)) { + background-color: #f5f5f5; + border: 1px solid #ddd; +} + +button:not(:-moz-any(.primary, .secondary)) { + background-color: #f5f5f5; + border: 1px solid #ddd; +} + +button:not(:is(.primary, .secondary)) { + background-color: #f5f5f5; + border: 1px solid #ddd; +} +``` + +This conversion ensures your negative selectors work correctly across all browsers while maintaining the correct specificity and behavior of the original selector. + +#### Math functions + +CSS now includes a rich set of mathematical functions that let you perform complex calculations directly in your stylesheets. These include standard math functions (`round()`, `mod()`, `rem()`, `abs()`, `sign()`), trigonometric functions (`sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `atan2()`), and exponential functions (`pow()`, `sqrt()`, `exp()`, `log()`, `hypot()`). + +```css +.dynamic-sizing { + /* Clamp a value between minimum and maximum */ + width: clamp(200px, 50%, 800px); + + /* Round to the nearest multiple */ + padding: round(14.8px, 5px); + + /* Trigonometry for animations or layouts */ + transform: rotate(calc(sin(45deg) * 50deg)); + + /* Complex math with multiple functions */ + --scale-factor: pow(1.25, 3); + font-size: calc(16px * var(--scale-factor)); +} +``` + +Bun's CSS bundler evaluates these mathematical expressions at build time when all values are known constants (not variables), resulting in optimized output: + +```css +.dynamic-sizing { + width: clamp(200px, 50%, 800px); + padding: 15px; + transform: rotate(35.36deg); + --scale-factor: 1.953125; + font-size: calc(16px * var(--scale-factor)); +} +``` + +This approach lets you write more expressive and maintainable CSS with meaningful mathematical relationships, which then gets compiled to optimized values for maximum browser compatibility and performance. + +#### Media query ranges + +Modern CSS supports intuitive range syntax for media queries, allowing you to specify breakpoints using comparison operators like `<`, `>`, `<=`, and `>=` instead of the more verbose `min-` and `max-` prefixes. This syntax is more readable and matches how we normally think about values and ranges. + +```css +/* Modern syntax with comparison operators */ +@media (width >= 768px) { + .container { + max-width: 720px; + } +} + +/* Inclusive range using <= and >= */ +@media (768px <= width <= 1199px) { + .sidebar { + display: flex; + } +} + +/* Exclusive range using < and > */ +@media (width > 320px) and (width < 768px) { + .mobile-only { + display: block; + } +} +``` + +Bun's CSS bundler converts these modern range queries to traditional media query syntax for compatibility with all browsers: + +```css +/* Converted to traditional min/max syntax */ +@media (min-width: 768px) { + .container { + max-width: 720px; + } +} + +@media (min-width: 768px) and (max-width: 1199px) { + .sidebar { + display: flex; + } +} + +@media (min-width: 321px) and (max-width: 767px) { + .mobile-only { + display: block; + } +} +``` + +This lets you write more intuitive and mathematical media queries while ensuring your stylesheets work correctly across all browsers, including those that don't support the modern range syntax. + +#### Shorthands + +CSS has introduced several modern shorthand properties that improve code readability and maintainability. Bun's CSS bundler ensures these convenient shorthands work on all browsers by converting them to their longhand equivalents when needed. + +```css +/* Alignment shorthands */ +.flex-container { + /* Shorthand for align-items and justify-items */ + place-items: center start; + + /* Shorthand for align-content and justify-content */ + place-content: space-between center; +} + +.grid-item { + /* Shorthand for align-self and justify-self */ + place-self: end center; +} + +/* Two-value overflow */ +.content-box { + /* First value for horizontal, second for vertical */ + overflow: hidden auto; +} + +/* Enhanced text-decoration */ +.fancy-link { + /* Combines multiple text decoration properties */ + text-decoration: underline dotted blue 2px; +} + +/* Two-value display syntax */ +.component { + /* Outer display type + inner display type */ + display: inline flex; +} +``` + +For browsers that don't support these modern shorthands, Bun converts them to their component longhand properties: + +```css +.flex-container { + /* Expanded alignment properties */ + align-items: center; + justify-items: start; + + align-content: space-between; + justify-content: center; +} + +.grid-item { + align-self: end; + justify-self: center; +} + +.content-box { + /* Separate overflow properties */ + overflow-x: hidden; + overflow-y: auto; +} + +.fancy-link { + /* Individual text decoration properties */ + text-decoration-line: underline; + text-decoration-style: dotted; + text-decoration-color: blue; + text-decoration-thickness: 2px; +} + +.component { + /* Single value display */ + display: inline-flex; +} +``` + +This conversion ensures your stylesheets remain clean and maintainable while providing the broadest possible browser compatibility. + +#### Double position gradients + +The double position gradient syntax is a modern CSS feature that allows you to create hard color stops in gradients by specifying the same color at two adjacent positions. This creates a sharp transition rather than a smooth fade, which is useful for creating stripes, color bands, and other multi-color designs. + +```css +.striped-background { + /* Creates a sharp transition from green to red at 30%-40% */ + background: linear-gradient( + to right, + yellow 0%, + green 20%, + green 30%, + red 30%, + /* Double position creates hard stop */ red 70%, + blue 70%, + blue 100% + ); +} + +.progress-bar { + /* Creates distinct color sections */ + background: linear-gradient( + to right, + #4caf50 0% 25%, + /* Green from 0% to 25% */ #ffc107 25% 50%, + /* Yellow from 25% to 50% */ #2196f3 50% 75%, + /* Blue from 50% to 75% */ #9c27b0 75% 100% /* Purple from 75% to 100% */ + ); +} +``` + +For browsers that don't support this syntax, Bun's CSS bundler automatically converts it to the traditional format by duplicating color stops: + +```css +.striped-background { + background: linear-gradient( + to right, + yellow 0%, + green 20%, + green 30%, + red 30%, + /* Split into two color stops */ red 70%, + blue 70%, + blue 100% + ); +} + +.progress-bar { + background: linear-gradient( + to right, + #4caf50 0%, + #4caf50 25%, + /* Two stops for green section */ #ffc107 25%, + #ffc107 50%, + /* Two stops for yellow section */ #2196f3 50%, + #2196f3 75%, + /* Two stops for blue section */ #9c27b0 75%, + #9c27b0 100% /* Two stops for purple section */ + ); +} +``` + +This conversion lets you use the cleaner double position syntax in your source code while ensuring gradients display correctly in all browsers. + +#### system-ui font + +The `system-ui` generic font family lets you use the device's native UI font, creating interfaces that feel more integrated with the operating system. This provides a more native look and feel without having to specify different font stacks for each platform. + +```css +.native-interface { + /* Use the system's default UI font */ + font-family: system-ui; +} + +.fallback-aware { + /* System UI font with explicit fallbacks */ + font-family: system-ui, sans-serif; +} +``` + +For browsers that don't support `system-ui`, Bun's CSS bundler automatically expands it to a comprehensive cross-platform font stack: + +```css +.native-interface { + /* Expanded to support all major platforms */ + font-family: + system-ui, + -apple-system, + BlinkMacSystemFont, + "Segoe UI", + Roboto, + "Noto Sans", + Ubuntu, + Cantarell, + "Helvetica Neue"; +} + +.fallback-aware { + /* Preserves the original fallback after the expanded stack */ + font-family: + system-ui, + -apple-system, + BlinkMacSystemFont, + "Segoe UI", + Roboto, + "Noto Sans", + Ubuntu, + Cantarell, + "Helvetica Neue", + sans-serif; +} +``` + +This approach gives you the simplicity of writing just `system-ui` in your source code while ensuring your interface adapts correctly to all operating systems and browsers. The expanded font stack includes appropriate system fonts for macOS/iOS, Windows, Android, Linux, and fallbacks for older browsers. + +## CSS Modules + +Bun's bundler also supports bundling [CSS modules](https://css-tricks.com/css-modules-part-1-need/) in addition to [regular CSS](/docs/bundler/css) with support for the following features: + +- Automatically detecting CSS module files (`.module.css`) with zero configuration +- Composition (`composes` property) +- Importing CSS modules into JSX/TSX +- Warnings/errors for invalid usages of CSS modules + +A CSS module is a CSS file (with the `.module.css` extension) where are all class names and animations are scoped to the file. This helps you avoid class name collisions as CSS declarations are globally scoped by default. + +Under the hood, Bun's bundler transforms locally scoped class names into unique identifiers. + +## Getting started + +Create a CSS file with the `.module.css` extension: + +```css +/* styles.module.css */ +.button { + color: red; +} + +/* other-styles.module.css */ +.button { + color: blue; +} +``` + +You can then import this file, for example into a TSX file: + +```tsx +import styles from "./styles.module.css"; +import otherStyles from "./other-styles.module.css"; + +export default function App() { + return ( + <> + + + + ); +} +``` + +The `styles` object from importing the CSS module file will be an object with all class names as keys and +their unique identifiers as values: + +```tsx +import styles from "./styles.module.css"; +import otherStyles from "./other-styles.module.css"; + +console.log(styles); +console.log(otherStyles); +``` + +This will output: + +```ts +{ + button: "button_123"; +} + +{ + button: "button_456"; +} +``` + +As you can see, the class names are unique to each file, avoiding any collisions! + +### Composition + +CSS modules allow you to _compose_ class selectors together. This lets you reuse style rules across multiple classes. + +For example: + +```css +/* styles.module.css */ +.button { + composes: background; + color: red; +} + +.background { + background-color: blue; +} +``` + +Would be the same as writing: + +```css +.button { + background-color: blue; + color: red; +} + +.background { + background-color: blue; +} +``` + +{% callout %} +There are a couple rules to keep in mind when using `composes`: + +- A `composes` property must come before any regular CSS properties or declarations +- You can only use `composes` on a **simple selector with a single class name**: + +```css +#button { + /* Invalid! `#button` is not a class selector */ + composes: background; +} + +.button, +.button-secondary { + /* Invalid! `.button, .button-secondary` is not a simple selector */ + composes: background; +} +``` + +{% /callout %} + +### Composing from a separate CSS module file + +You can also compose from a separate CSS module file: + +```css +/* background.module.css */ +.background { + background-color: blue; +} + +/* styles.module.css */ +.button { + composes: background from "./background.module.css"; + color: red; +} +``` + +{% callout %} +When composing classes from separate files, be sure that they do not contain the same properties. + +The CSS module spec says that composing classes from separate files with conflicting properties is +undefined behavior, meaning that the output may differ and be unreliable. +{% /callout %} diff --git a/docs/bundler/css_modules.md b/docs/bundler/css_modules.md new file mode 100644 index 00000000000000..271ab4621dc7bf --- /dev/null +++ b/docs/bundler/css_modules.md @@ -0,0 +1,145 @@ +# CSS Modules + +Bun's bundler also supports bundling [CSS modules](https://css-tricks.com/css-modules-part-1-need/) in addition to [regular CSS](/docs/bundler/css) with support for the following features: + +- Automatically detecting CSS module files (`.module.css`) with zero configuration +- Composition (`composes` property) +- Importing CSS modules into JSX/TSX +- Warnings/errors for invalid usages of CSS modules + +A CSS module is a CSS file (with the `.module.css` extension) where are all class names and animations are scoped to the file. This helps you avoid class name collisions as CSS declarations are globally scoped by default. + +Under the hood, Bun's bundler transforms locally scoped class names into unique identifiers. + +## Getting started + +Create a CSS file with the `.module.css` extension: + +```css +/* styles.module.css */ +.button { + color: red; +} + +/* other-styles.module.css */ +.button { + color: blue; +} +``` + +You can then import this file, for example into a TSX file: + +```tsx +import styles from "./styles.module.css"; +import otherStyles from "./other-styles.module.css"; + +export default function App() { + return ( + <> + + + + ); +} +``` + +The `styles` object from importing the CSS module file will be an object with all class names as keys and +their unique identifiers as values: + +```tsx +import styles from "./styles.module.css"; +import otherStyles from "./other-styles.module.css"; + +console.log(styles); +console.log(otherStyles); +``` + +This will output: + +```ts +{ + button: "button_123"; +} + +{ + button: "button_456"; +} +``` + +As you can see, the class names are unique to each file, avoiding any collisions! + +### Composition + +CSS modules allow you to _compose_ class selectors together. This lets you reuse style rules across multiple classes. + +For example: + +```css +/* styles.module.css */ +.button { + composes: background; + color: red; +} + +.background { + background-color: blue; +} +``` + +Would be the same as writing: + +```css +.button { + background-color: blue; + color: red; +} + +.background { + background-color: blue; +} +``` + +{% callout %} +There are a couple rules to keep in mind when using `composes`: + +- A `composes` property must come before any regular CSS properties or declarations +- You can only use `composes` on a **simple selector with a single class name**: + +```css +#button { + /* Invalid! `#button` is not a class selector */ + composes: background; +} + +.button, +.button-secondary { + /* Invalid! `.button, .button-secondary` is not a simple selector */ + composes: background; +} +``` + +{% /callout %} + +### Composing from a separate CSS module file + +You can also compose from a separate CSS module file: + +```css +/* background.module.css */ +.background { + background-color: blue; +} + +/* styles.module.css */ +.button { + composes: background from "./background.module.css"; + color: red; +} +``` + +{% callout %} +When composing classes from separate files, be sure that they do not contain the same properties. + +The CSS module spec says that composing classes from separate files with conflicting properties is +undefined behavior, meaning that the output may differ and be unreliable. +{% /callout %} diff --git a/docs/nav.ts b/docs/nav.ts index cdbe442e902760..518bd56472135a 100644 --- a/docs/nav.ts +++ b/docs/nav.ts @@ -218,6 +218,9 @@ export default { page("bundler/html", "Bundle frontend & static sites", { description: `Zero-config HTML bundler for single-page apps and multi-page apps. Automatic bundling, TailwindCSS plugins, TypeScript, JSX, React support, and incredibly fast builds`, }), + page("bundler/css", "Bundle, transpile, and minify CSS", { + description: `Production ready CSS bundler with support for modern CSS features, CSS modules, and more.`, + }), page("bundler/fullstack", "Fullstack Dev Server", { description: "Serve your frontend and backend from the same app with Bun's dev server.", }), diff --git a/src/baby_list.zig b/src/baby_list.zig index 0d8be0abf68105..60fb5dcf877981 100644 --- a/src/baby_list.zig +++ b/src/baby_list.zig @@ -316,6 +316,12 @@ pub fn BabyList(comptime Type: type) type { this.update(list__); } + pub fn insertSlice(this: *@This(), allocator: std.mem.Allocator, index: usize, vals: []const Type) !void { + var list__ = this.listManaged(allocator); + try list__.insertSlice(index, vals); + this.update(list__); + } + pub fn append(this: *@This(), allocator: std.mem.Allocator, value: []const Type) !void { var list__ = this.listManaged(allocator); try list__.appendSlice(value); diff --git a/src/bake/DevServer.zig b/src/bake/DevServer.zig index a6aa2357416d35..00b7013700b889 100644 --- a/src/bake/DevServer.zig +++ b/src/bake/DevServer.zig @@ -1321,6 +1321,15 @@ fn appendRouteEntryPointsIfNotStale(dev: *DevServer, entry_points: *EntryPointLi try entry_points.append(alloc, html.html_bundle.html_bundle.path, .{ .client = true }); }, } + + if (dev.has_tailwind_plugin_hack) |*map| { + for (map.keys()) |abs_path| { + const file = dev.client_graph.bundled_files.get(abs_path) orelse + continue; + if (file.flags.kind == .css) + entry_points.appendCss(alloc, abs_path) catch bun.outOfMemory(); + } + } } fn onFrameworkRequestWithBundle( @@ -3664,7 +3673,7 @@ pub fn IncrementalGraph(side: bake.Side) type { bundler_index: bun.JSAst.Index, ) !void { bun.assert(bundler_index.isValid()); - bun.assert(ctx.loaders[bundler_index.get()] == .css); + bun.assert(ctx.loaders[bundler_index.get()].isCSS()); var sfb = std.heap.stackFallback(@sizeOf(bun.JSAst.Index) * 64, temp_alloc); const queue_alloc = sfb.get(); diff --git a/src/bake/production.zig b/src/bake/production.zig index fcc67158072a0b..9e1fc0d59a83b0 100644 --- a/src/bake/production.zig +++ b/src/bake/production.zig @@ -286,7 +286,7 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa file.dest_path, file.entry_point_index, }); - if (file.loader == .css) { + if (file.loader.isCSS()) { if (css_chunks_count == 0) css_chunks_first = i; css_chunks_count += 1; } @@ -417,7 +417,7 @@ pub fn buildWithVm(ctx: bun.CLI.Command.Context, cwd: []const u8, vm: *VirtualMa const css_chunk_js_strings = try allocator.alloc(JSValue, css_chunks_count); for (bundled_outputs[css_chunks_first..][0..css_chunks_count], css_chunk_js_strings) |output_file, *str| { bun.assert(output_file.dest_path[0] != '.'); - bun.assert(output_file.loader == .css); + bun.assert(output_file.loader.isCSS()); str.* = (try bun.String.createFormat("{s}{s}", .{ public_path, output_file.dest_path })).toJS(global); } diff --git a/src/base64/base64.zig b/src/base64/base64.zig index b47856fe6861ef..3b4a816b9f69ca 100644 --- a/src/base64/base64.zig +++ b/src/base64/base64.zig @@ -48,6 +48,20 @@ pub fn encode(destination: []u8, source: []const u8) usize { return bun.simdutf.base64.encode(source, destination, false); } +pub fn simdutfEncodeLenUrlSafe(source_len: usize) usize { + return bun.simdutf.base64.encode_len(source_len, true); +} + +/// Encode with the following differences from regular `encode` function: +/// +/// * No padding is added (the extra `=` characters at the end) +/// * `-` and `_` are used instead of `+` and `/` +/// +/// See the documentation for simdutf's `binary_to_base64` function for more details (simdutf_impl.h). +pub fn simdutfEncodeUrlSafe(destination: []u8, source: []const u8) usize { + return bun.simdutf.base64.encode(source, destination, true); +} + pub fn decodeLenUpperBound(len: usize) usize { return zig_base64.standard.Decoder.calcSizeUpperBound(len) catch { //fallback diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index 45b40bfd2e0980..7ee4acafd735f2 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -3625,7 +3625,15 @@ const TOMLObject = struct { return globalThis.throwValue(log.toJS(globalThis, default_allocator, "Failed to print toml")); }; var writer = js_printer.BufferPrinter.init(buffer_writer); - _ = js_printer.printJSON(*js_printer.BufferPrinter, &writer, parse_result, &source, .{}) catch { + _ = js_printer.printJSON( + *js_printer.BufferPrinter, + &writer, + parse_result, + &source, + .{ + .mangled_props = null, + }, + ) catch { return globalThis.throwValue(log.toJS(globalThis, default_allocator, "Failed to print toml")); }; diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 905863038994ac..a0ed809d571bbd 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -7518,7 +7518,7 @@ pub fn NewServer(comptime NamespaceType: type, comptime ssl_enabled_: bool, comp &writer, bun.Global.BunInfo.generate(*Transpiler, &JSC.VirtualMachine.get().transpiler, allocator) catch unreachable, &source, - .{}, + .{ .mangled_props = null }, ) catch unreachable; resp.writeStatus("200 OK"); diff --git a/src/bun.js/bindings/bun-simdutf.cpp b/src/bun.js/bindings/bun-simdutf.cpp index e6dd1c471d4c6c..6aba5caa744a75 100644 --- a/src/bun.js/bindings/bun-simdutf.cpp +++ b/src/bun.js/bindings/bun-simdutf.cpp @@ -339,6 +339,11 @@ size_t simdutf__base64_encode(const char* input, size_t length, char* output, in return simdutf::binary_to_base64(input, length, output, is_urlsafe ? simdutf::base64_url : simdutf::base64_default); } +size_t simdutf__base64_length_from_binary(size_t length, int is_urlsafe) +{ + return simdutf::base64_length_from_binary(length, is_urlsafe ? simdutf::base64_url : simdutf::base64_default); +} + SIMDUTFResult simdutf__base64_decode_from_binary(const char* input, size_t length, char* output, size_t outlen_, int is_urlsafe) { size_t outlen = outlen_; diff --git a/src/bun.js/bindings/bun-simdutf.zig b/src/bun.js/bindings/bun-simdutf.zig index 7ef948dfc22c1f..246624c3f3c1ae 100644 --- a/src/bun.js/bindings/bun-simdutf.zig +++ b/src/bun.js/bindings/bun-simdutf.zig @@ -387,11 +387,16 @@ pub const base64 = struct { extern fn simdutf__base64_encode(input: [*]const u8, length: usize, output: [*]u8, is_urlsafe: c_int) usize; extern fn simdutf__base64_decode_from_binary(input: [*]const u8, length: usize, output: [*]u8, outlen: usize, is_urlsafe: c_int) SIMDUTFResult; extern fn simdutf__base64_decode_from_binary16(input: [*]const u16, length: usize, output: [*]u8, outlen: usize, is_urlsafe: c_int) SIMDUTFResult; + extern fn simdutf__base64_length_from_binary(length: usize, options: c_int) usize; pub fn encode(input: []const u8, output: []u8, is_urlsafe: bool) usize { return simdutf__base64_encode(input.ptr, input.len, output.ptr, @intFromBool(is_urlsafe)); } + pub fn encode_len(input: usize, is_urlsafe: bool) usize { + return simdutf__base64_length_from_binary(input, @intFromBool(is_urlsafe)); + } + pub fn decode(input: []const u8, output: []u8, is_urlsafe: bool) SIMDUTFResult { return simdutf__base64_decode_from_binary(input.ptr, input.len, output.ptr, output.len, @intFromBool(is_urlsafe)); } diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index 71dcae1294f162..76f27263a583b7 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -67,7 +67,7 @@ const linker = @import("../linker.zig"); const sourcemap = bun.sourcemap; const StringJoiner = bun.StringJoiner; const base64 = bun.base64; -const Ref = @import("../ast/base.zig").Ref; +pub const Ref = @import("../ast/base.zig").Ref; const Define = @import("../defines.zig").Define; const DebugOptions = @import("../cli.zig").Command.DebugOptions; const ThreadPoolLib = @import("../thread_pool.zig"); @@ -134,6 +134,8 @@ const DataURL = @import("../resolver/resolver.zig").DataURL; const logPartDependencyTree = Output.scoped(.part_dep_tree, false); +pub const MangledProps = std.AutoArrayHashMapUnmanaged(Ref, []const u8); + fn tracer(comptime src: std.builtin.SourceLocation, comptime name: [:0]const u8) bun.tracy.Ctx { return bun.tracy.traceNamed(src, "Transpiler." ++ name); } @@ -616,7 +618,7 @@ pub const BundleV2 = struct { } const is_js = v.all_loaders[source_index.get()].isJavaScriptLike(); - const is_css = v.all_loaders[source_index.get()] == .css; + const is_css = v.all_loaders[source_index.get()].isCSS(); const import_record_list_id = source_index; // when there are no import records, v index will be invalid @@ -3206,7 +3208,7 @@ pub const BundleV2 = struct { } if (this.transpiler.options.dev_server) |dev_server| brk: { - if (loader == .css) { + if (loader.isCSS()) { // Do not use cached files for CSS. break :brk; } @@ -3511,12 +3513,12 @@ pub const BundleV2 = struct { const input_file_loaders = this.graph.input_files.items(.loader); const save_import_record_source_index = this.transpiler.options.dev_server == null or result.loader == .html or - result.loader == .css; + result.loader.isCSS(); if (this.resolve_tasks_waiting_for_import_source_index.fetchSwapRemove(result.source.index.get())) |pending_entry| { for (pending_entry.value.slice()) |to_assign| { if (save_import_record_source_index or - input_file_loaders[to_assign.to_source_index.get()] == .css) + input_file_loaders[to_assign.to_source_index.get()].isCSS()) { import_records.slice()[to_assign.import_record_index].source_index = to_assign.to_source_index; } @@ -4179,6 +4181,7 @@ pub const ParseTask = struct { loader: Loader, unique_key_prefix: u64, unique_key_for_additional_file: *FileLoaderHash, + has_any_css_locals: *std.atomic.Value(u32), ) !JSAst { switch (loader) { .jsx, .tsx, .js, .ts => { @@ -4380,6 +4383,7 @@ pub const ParseTask = struct { return JSAst.init(ast); }, .css => { + // make css ast var import_records = BabyList(ImportRecord){}; const source_code = source.contents; var temp_log = bun.logger.Log.init(allocator); @@ -4387,11 +4391,22 @@ pub const ParseTask = struct { temp_log.appendToMaybeRecycled(log, &source) catch bun.outOfMemory(); } - var css_ast = switch (bun.css.BundlerStyleSheet.parseBundler( + const css_module_suffix = ".module.css"; + const enable_css_modules = source.path.pretty.len > css_module_suffix.len and + strings.eqlComptime(source.path.pretty[source.path.pretty.len - css_module_suffix.len ..], css_module_suffix); + const parser_options = if (enable_css_modules) init: { + var parseropts = bun.css.ParserOptions.default(allocator, &temp_log); + parseropts.filename = bun.path.basename(source.path.pretty); + parseropts.css_modules = bun.css.CssModuleConfig{}; + break :init parseropts; + } else bun.css.ParserOptions.default(allocator, &temp_log); + + var css_ast, var extra = switch (bun.css.BundlerStyleSheet.parseBundler( allocator, source_code, - bun.css.ParserOptions.default(allocator, &temp_log), + parser_options, &import_records, + source.index, )) { .result => |v| v, .err => |e| { @@ -4399,16 +4414,29 @@ pub const ParseTask = struct { return error.SyntaxError; }, }; + // Make sure the css modules local refs have a valid tag + if (comptime bun.Environment.isDebug) { + if (css_ast.local_scope.count() > 0) { + for (css_ast.local_scope.values()) |entry| { + const ref = entry.ref; + bun.assert(ref.innerIndex() < extra.symbols.len); + } + } + } if (css_ast.minify(allocator, bun.css.MinifyOptions{ .targets = bun.css.Targets.forBundlerTarget(transpiler.options.target), .unused_symbols = .{}, - }).asErr()) |e| { + }, &extra).asErr()) |e| { try e.addToLogger(&temp_log, &source, allocator); return error.MinifyError; } + if (css_ast.local_scope.count() > 0) { + _ = has_any_css_locals.fetchAdd(1, .monotonic); + } + // If this is a css module, the final exports object wil be set in `generateCodeForLazyExport`. const root = Expr.init(E.Object, E.Object{}, Logger.Loc{ .start = 0 }); const css_ast_heap = bun.create(allocator, bun.css.BundlerStyleSheet, css_ast); - var ast = JSAst.init((try js_parser.newLazyExportAST(allocator, transpiler.options.define, opts, &temp_log, root, &source, "")).?); + var ast = JSAst.init((try js_parser.newLazyExportASTImpl(allocator, transpiler.options.define, opts, &temp_log, root, &source, "", extra.symbols)).?); ast.css = css_ast_heap; ast.import_records = import_records; return ast; @@ -5077,9 +5105,9 @@ pub const ParseTask = struct { .content_hash = 0, }; var ast: JSAst = if (!is_empty) - try getAST(log, transpiler, opts, allocator, resolver, source, loader, task.ctx.unique_key, &unique_key_for_additional_file) + try getAST(log, transpiler, opts, allocator, resolver, source, loader, task.ctx.unique_key, &unique_key_for_additional_file, &task.ctx.linker.has_any_css_locals) else switch (opts.module_type == .esm) { - inline else => |as_undefined| if (loader == .css) try getEmptyCSSAST( + inline else => |as_undefined| if (loader.isCSS()) try getEmptyCSSAST( log, transpiler, opts, @@ -6256,10 +6284,15 @@ pub const LinkerContext = struct { /// to know whether or not we can free it safely. pending_task_count: std.atomic.Value(u32) = std.atomic.Value(u32).init(0), + /// + has_any_css_locals: std.atomic.Value(u32) = std.atomic.Value(u32).init(0), + /// Used by Bake to extract []CompileResult before it is joined dev_server: ?*bun.bake.DevServer = null, framework: ?*const bake.Framework = null, + mangled_props: MangledProps = .{}, + fn pathWithPrettyInitialized(this: *LinkerContext, path: Fs.Path) !Fs.Path { return genericPathWithPrettyInitialized(path, this.options.target, this.resolver.fs.top_level_dir, this.graph.allocator); } @@ -7177,6 +7210,10 @@ pub const LinkerContext = struct { wrapping_conditions: *BabyList(bun.css.ImportConditions), wrapping_import_records: *BabyList(ImportRecord), ) void { + debug( + "Visit file: {d}={s}", + .{ source_index.get(), visitor.parse_graph.input_files.items(.source)[source_index.get()].path.pretty }, + ); // The CSS specification strangely does not describe what to do when there // is a cycle. So we are left with reverse-engineering the behavior from a // real browser. Here's what the WebKit code base has to say about this: @@ -7188,6 +7225,10 @@ pub const LinkerContext = struct { // WebKit for more information. for (visitor.visited.slice()) |visitedSourceIndex| { if (visitedSourceIndex.get() == source_index.get()) { + debug( + "Skip file: {d}={s}", + .{ source_index.get(), visitor.parse_graph.input_files.items(.source)[source_index.get()].path.pretty }, + ); return; } } @@ -7273,24 +7314,29 @@ pub const LinkerContext = struct { }, ) catch bun.outOfMemory(); } + debug( + "Push external: {d}={s}", + .{ source_index.get(), visitor.parse_graph.input_files.items(.source)[source_index.get()].path.pretty }, + ); visitor.has_external_import = true; } } } - // TODO: composes from css modules + // Iterate over the "composes" directives. Note that the order doesn't + // matter for these because the output order is explicitly undfened + // in the specification. + for (visitor.all_import_records[source_index.get()].sliceConst()) |*record| { + if (record.kind == .composes and record.source_index.isValid()) { + visitor.visit(record.source_index, wrapping_conditions, wrapping_import_records); + } + } if (comptime bun.Environment.isDebug) { debug( - "Looking at file: {d}={s}", + "Push file: {d}={s}", .{ source_index.get(), visitor.parse_graph.input_files.items(.source)[source_index.get()].path.pretty }, ); - for (visitor.visited.slice()) |idx| { - debug( - "Visit: {d}", - .{idx.get()}, - ); - } } // Accumulate imports in depth-first postorder visitor.order.push(visitor.allocator, Chunk.CssImportOrder{ @@ -7321,6 +7367,8 @@ pub const LinkerContext = struct { const css_asts: []const ?*bun.css.BundlerStyleSheet = this.graph.ast.items(.css); + debugCssOrder(this, &order, .BEFORE_HOISTING); + // CSS syntax unfortunately only allows "@import" rules at the top of the // file. This means we must hoist all external "@import" rules to the top of // the file when bundling, even though doing so will change the order of CSS @@ -7620,6 +7668,7 @@ pub const LinkerContext = struct { } const CssOrderDebugStep = enum { + BEFORE_HOISTING, AFTER_HOISTING, AFTER_REMOVING_DUPLICATES, WHILE_OPTIMIZING_REDUNDANT_LAYER_RULES, @@ -7648,6 +7697,7 @@ pub const LinkerContext = struct { const writer = arrlist.writer(arena.allocator()); const W = @TypeOf(writer); arrlist.appendSlice(arena.allocator(), "[") catch unreachable; + var symbols = Symbol.Map{}; for (entry.conditions.sliceConst(), 0..) |*condition_, j| { const condition: *const bun.css.ImportConditions = condition_; const scratchbuf = std.ArrayList(u8).init(arena.allocator()); @@ -7661,6 +7711,8 @@ pub const LinkerContext = struct { .ast_urls_for_css = this.parse_graph.ast.items(.url_for_css), .ast_unique_key_for_additional_file = this.parse_graph.input_files.items(.unique_key_for_additional_file), }, + &this.mangled_props, + &symbols, ); condition.toCss(W, &printer) catch unreachable; @@ -7795,11 +7847,13 @@ pub const LinkerContext = struct { const all_import_records = this.graph.ast.items(.import_records); const all_loaders = this.parse_graph.input_files.items(.loader); + const all_parts = this.graph.ast.items(.parts); const visit = struct { fn visit( c: *LinkerContext, import_records: []const BabyList(ImportRecord), + parts: []const Part.List, loaders: []const Loader, temp: std.mem.Allocator, visits: *BitSet, @@ -7811,25 +7865,31 @@ pub const LinkerContext = struct { visits.set(source_index.get()); const records: []ImportRecord = import_records[source_index.get()].slice(); - - for (records) |record| { - if (record.source_index.isValid()) { - // Traverse any files imported by this part. Note that CommonJS calls - // to "require()" count as imports too, sort of as if the part has an - // ESM "import" statement in it. This may seem weird because ESM imports - // are a compile-time concept while CommonJS imports are a run-time - // concept. But we don't want to manipulate