Skip to content

Reduce the size of @tailwindcss/browser and only scan the elements that changed - #20703

Draft
adamwathan wants to merge 1 commit into
mainfrom
perf/browser-build-size-and-speed
Draft

adamwathan wants to merge 1 commit into
mainfrom
perf/browser-build-size-and-speed

Conversation

@adamwathan

Copy link
Copy Markdown
Contributor

Summary

This PR makes @tailwindcss/browser smaller, and makes it do less work on every DOM change.

Before After
Minified 295,631 B 230,193 B −22.1%
Gzip 78,228 B 60,367 B −22.8%
Brotli 63,971 B 51,423 B −19.6%

Build-time switches in core

Most of the size was code that can never run in the browser. Core now has two build-time switches, documented in packages/tailwindcss/src/node.d.ts:

  • TAILWIND_INTELLISENSE: class and variant suggestions, and canonicalization.
  • TAILWIND_JS_PLUGINS: @plugin, @config, and the JS theme they rely on.

The code checks process.env.TAILWIND_… !== 'false' right where it is used. The published tailwindcss build defines both switches as "true", so it does not change. @tailwindcss/browser defines both as "false", and esbuild removes the code. This replaces the patch-intellisense-apis esbuild plugin, which replaced modules by file path.

Two more things happen in the tsup.config.ts of the browser build:

  • Every bundled module is marked as free of side effects. Without this, esbuild keeps the v3 default theme in the bundle because of its top-level object spreads, even when nothing uses it.
  • The embedded stylesheets (preflight.css, theme.css, …) are minified with the CSS parser of core. The build fails when a minified stylesheet does not parse back to the same AST.

Runtime

  • On a DOM change, only the elements in the mutation records are checked for new classes, instead of document.querySelectorAll('[class]'). On a page with 603 elements, a class change on two elements now reads 2 class attributes instead of 604. A change to a stylesheet still scans the whole document.
  • The stylesheet is not written when the generated CSS did not change.
  • A failed build no longer prevents all later builds. Before, the build queue stayed rejected after the first error (e.g. an @apply with a class that does not exist), and the page did not get new CSS until a reload.

Behavior changes in @tailwindcss/browser

  • Legacy theme() paths only resolve when they map to a theme variable, because the JS theme is not in the bundle anymore. theme(colors.red.500), theme(colors.red.500 / 50%), theme(screens.md) and theme(fontSize.sm) still work. theme(spacing.4), theme(opacity.50) and theme(zIndex.10) now fail with "Could not resolve value for theme function", unless they have a fallback. theme(blur.sm) now resolves to the v4 value (8px) instead of the v3 value (4px).
  • The error for @plugin and @config is now "This build of Tailwind CSS does not support @plugin or @config."

To get the old theme() behavior back, set TAILWIND_JS_PLUGINS to "true" in packages/@tailwindcss-browser/tsup.config.ts. This adds about 14 KB gzipped.

Test plan

  • Added packages/tailwindcss/src/build-switches.test.ts. It checks that the generated CSS for more than 10,000 candidates is the same with the switches turned off, and covers the behavior of each switch.
  • Added packages/@tailwindcss-browser/src/bundle.test.ts. It builds the real bundle and fails when IntelliSense, the JS plugin API, JS config files or the default JS theme end up in it, when process.env is left in it, or when it grows past 64,000 bytes gzipped.
  • The test suite of tailwindcss passes (5,407 tests).
  • Built tailwindcss and checked that the published build still has suggestions, canonicalization and plugin support, and no process.env.TAILWIND_… left in it.
  • Compared the old and the new bundle in a stand-in DOM in Node: they generate the same CSS (apart from one space in an @supports rule of Preflight).
  • Added three Playwright tests to packages/@tailwindcss-browser/tests/ui.spec.ts: elements inside of new elements, recovering from invalid CSS, and legacy theme() paths. The Playwright suite was not run locally (Chromium could not be started in the sandbox this was written in), so these tests run for the first time on CI.
  • The timings were not measured in a real browser. In Node, the time until the first CSS went down by about 6%.

🤖 Generated with Claude Code

…that changed

Add two build-time switches to core, `TAILWIND_INTELLISENSE` and
`TAILWIND_JS_PLUGINS`. They are on in the published `tailwindcss` build
and off in `@tailwindcss/browser`, where the bundler now drops the code
for suggestions, canonicalization, `@plugin`, `@config` and the v3
default theme. The stylesheets embedded in the browser build are
minified.

The browser build goes from 78,228 to 60,367 bytes gzipped (-23%).

At runtime the browser build only reads the classes of the elements in
the mutation records instead of the whole document, does not write the
stylesheet when the CSS did not change, and keeps building after a
build failed.

Without the JS theme, legacy `theme()` paths in the browser build only
resolve when they map to a theme variable (e.g. `theme(colors.red.500)`
but not `theme(spacing.4)`).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant