Repository navigation
Reduce the size of @tailwindcss/browser and only scan the elements that changed - #20703
Draft
adamwathan wants to merge 1 commit into
Draft
adamwathan wants to merge 1 commit into
adamwathan wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes
@tailwindcss/browsersmaller, and makes it do less work on every DOM change.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 publishedtailwindcssbuild defines both switches as"true", so it does not change.@tailwindcss/browserdefines both as"false", and esbuild removes the code. This replaces thepatch-intellisense-apisesbuild plugin, which replaced modules by file path.Two more things happen in the
tsup.config.tsof the browser build: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
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.@applywith a class that does not exist), and the page did not get new CSS until a reload.Behavior changes in
@tailwindcss/browsertheme()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)andtheme(fontSize.sm)still work.theme(spacing.4),theme(opacity.50)andtheme(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).@pluginand@configis now "This build of Tailwind CSS does not support@pluginor@config."To get the old
theme()behavior back, setTAILWIND_JS_PLUGINSto"true"inpackages/@tailwindcss-browser/tsup.config.ts. This adds about 14 KB gzipped.Test plan
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.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, whenprocess.envis left in it, or when it grows past 64,000 bytes gzipped.tailwindcsspasses (5,407 tests).tailwindcssand checked that the published build still has suggestions, canonicalization and plugin support, and noprocess.env.TAILWIND_…left in it.@supportsrule of Preflight).packages/@tailwindcss-browser/tests/ui.spec.ts: elements inside of new elements, recovering from invalid CSS, and legacytheme()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.🤖 Generated with Claude Code