Repository navigation
wp-build: Derive CSS module class names from each stylesheet's CSS - #85900
Conversation
`generateScopedName: '[contenthash]__[local]'` was hashed by `generic-names` from the file path and local name only, so class names stayed the same across package versions while the CSS behind them changed. Two bundled copies of a package then applied their styles to each other's elements. Hash the relative file path, the CSS of the file that defines each class, and the local name instead. The CSS comes from `postcss-modules-scope`, which also covers files reached through `composes`, and is run through the plugins that precede `postcss-modules` first so that a changed token fallback also renames the affected classes. Fixes #85896.
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +68 B (0%) Total Size: 8.57 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Navigates the list via UP/DOWN arrow keys from action buttons (Site Editor v2) in
|
Tighten the class name format test, test that a token fallback change alone renames classes, and shorten the changelog entry.
…ule-class-names-from-css
aduth
left a comment
There was a problem hiding this comment.
LGTM 👍
Exploring this locally with an agent to understand defaults and alternative options, I wonder if we need to do all the PostCSS processing in the name generator, or if it's enough to hash from the original raw CSS? In that approach, there's a risk that a change that alters compiled CSS but not the source doesn't change the class name, but I don't know how likely that would be in practice.
|
The raw source would cover most changes, since a |
|
There was a conflict while trying to cherry-pick the commit to the wp/7.2 branch. Please resolve the conflict manually and create a PR to the wp/7.2 branch. PRs to wp/7.2 are similar to PRs to trunk, but you should base your PR on the wp/7.2 branch instead of trunk. |
…'s CSS (#86062) * wp-build: Derive CSS module class names from each stylesheet's CSS (#85900) * wp-build: Derive CSS module class names from each stylesheet's CSS `generateScopedName: '[contenthash]__[local]'` was hashed by `generic-names` from the file path and local name only, so class names stayed the same across package versions while the CSS behind them changed. Two bundled copies of a package then applied their styles to each other's elements. Hash the relative file path, the CSS of the file that defines each class, and the local name instead. The CSS comes from `postcss-modules-scope`, which also covers files reached through `composes`, and is run through the plugins that precede `postcss-modules` first so that a changed token fallback also renames the affected classes. Fixes #85896. * Add changelog link * Address review feedback Tighten the class name format test, test that a token fallback change alone renames classes, and shorten the changelog entry. * Add changelog entries to packages that ship CSS modules Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: aduth <aduth@git.wordpress.org> Co-authored-by: Nikschavan <nikschavan@git.wordpress.org> (cherry picked from commit d473413) * Link backport changelog entries to #86062 --------- Co-authored-by: aduth <aduth@git.wordpress.org> Co-authored-by: Nikschavan <nikschavan@git.wordpress.org>
|
It's unfortunate that we have to compile a It could be solved by splitting the loads the If we later migrate the Not sure if this refactoring is worth doing now. |
|
could we leverage the package version instead of the CSS contents? |
…ordPress#85900) * wp-build: Derive CSS module class names from each stylesheet's CSS `generateScopedName: '[contenthash]__[local]'` was hashed by `generic-names` from the file path and local name only, so class names stayed the same across package versions while the CSS behind them changed. Two bundled copies of a package then applied their styles to each other's elements. Hash the relative file path, the CSS of the file that defines each class, and the local name instead. The CSS comes from `postcss-modules-scope`, which also covers files reached through `composes`, and is run through the plugins that precede `postcss-modules` first so that a changed token fallback also renames the affected classes. Fixes WordPress#85896. * Add changelog link * Address review feedback Tighten the class name format test, test that a token fallback change alone renames classes, and shorten the changelog entry. * Add changelog entries to packages that ship CSS modules Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: aduth <aduth@git.wordpress.org> Co-authored-by: Nikschavan <nikschavan@git.wordpress.org>
What?
Fixes #85896
This PR changes how
@wordpress/buildgenerates CSS module class names. Each name now changes whenever the CSS that defines it changes. Previously, names depended only on the file path and the local class name.Why?
When two copies of a package are loaded, for example
@wordpress/uiin WordPress Core and an older copy bundled in a plugin, their class names are the same even where their CSS differs. Both style tags are injected, and each one styles the other copy's elements. The issue shows this mix of 0.24.0 and 0.25.0Tabsstyles.Why the class names did not depend on the CSS
compile-inline-style.mjspassesgenerateScopedName: '[contenthash]__[local]'topostcss-modules. Despite the[contenthash]placeholder,postcss-moduleshands a string pattern togeneric-names, andgeneric-nameshasheshashPrefix + path.relative( process.cwd(), filepath ) + "\0" + localName. The CSS is never part of that hash, so theTabstab class isa5fd8814f195aa5e__tabin@wordpress/ui0.22.0, 0.24.0 and 0.25.0 alike. This has been the case since #74651 introduced the pattern, and the earlier[name]__[local]__[hash:base64:5]pattern was also path-based.Since #76743, the
data-wp-hashdeduplication key is a hash of the transformed CSS. So when two copies of a package differ in a stylesheet, their keys differ but their class names do not.Mini retrospective — What went wrong?
I wanted to figure out where we went wrong here, because at least I personally assumed that it was already hashing the style content.
[contenthash]was taken to mean a hash of the stylesheet, butloader-utilstreats[hash]and[contenthash]the same, andgeneric-namesonly gives it the path and the local name. Nothing checked what it actually hashed.data-wp-hashdedupe key #76743 said two stylesheets should share a hash only when they have the same class names and the same styles. It applied that rule to thedata-wp-hashdedup key, but nobody checked whether the class names followed it too.How?
generateScopedNameis now a function that hashes the file path, the CSS of the file that defines the class, and the local class name. Classes reached throughcomposesare hashed with the CSS of their own file, and the design token fallbacks are part of the hashed CSS. Two files with the same path and identical CSS still produce identical names, so unchanged components in two copies of a package still share their class names and style tags.Implementation details
postcss-modules-scopecalls the function asgenerateScopedName( name, filename, css ), wherecssis the source of the file that defines the class. That includes files reached throughcomposes, whichpostcss-modulescompiles with the same generator and inlines into the importing file's output. The name is a hash of three values, followed by__<local>:process.cwd(), with forward slashes, which is the same rootgeneric-namesused. Output does not depend on the checkout location or the operating system.postcss-modules, which is currently the design token fallback plugin.A name that would start with a digit gets a
_prefix, like before.The design token fallbacks are applied after the source is read, so the
cssargument does not contain them. The generator runs the preceding plugins on that source before hashing it, so a changed fallback value renames the classes in the files that use it. The fallback values come from the installed@wordpress/theme(design-tokens.js, loaded through@wordpress/theme-build-tools). Hashing them as a separate salt would rename every class whenever any token changed, and the salt would have to be read differently onwp/7.2, where the plugin is loaded from@wordpress/themedirectly. Reusing the plugin list avoids both problems. The processed source is cached per file, so each stylesheet is processed once more per build.Why not
hashPrefixThe issue proposes running the token fallbacks in a separate PostCSS pass and passing a hash of the result as
hashPrefix. That has two problems:@wordpress/uicomposes classes across files, for examplecomposes: header from "../utils/css/overlay-chrome.module.css"inDialog,DrawerandAlertDialog, andcomposes: dropdown-motion from "../utils/css/dropdown-motion.module.css"inMenu. The composed classes are named by the same generator and would use the importing file'shashPrefix. If onlyoverlay-chrome.module.csschanged, those classes inDialogwould keep their old names with the new CSS, which is the same collision.postcss-modulescompiles a file reached throughcomposes, it re-runs only the plugins that come before it in the same processor. A separate fallback pass would drop the fallbacks from the inlined composed CSS.Limitations
This change only affects copies of these packages that are built after it. Copies built before it, including those in WordPress and plugin versions that are already released, keep their path-based class names.
@wordpress/ui. This is fixed only when those plugins update to versions built after this change.@wordpress/buildget this change only when they update@wordpress/buildand rebuild.Testing Instructions
npm run buildand check the class names inpackages/ui/build-module/tabs/tab.mjs. The tab class is no longera5fd8814f195aa5e__tab, which is its name in the published@wordpress/ui@0.25.0.npm run buildagain and confirm that the class names do not change.packages/ui/src/utils/css/overlay-chrome.module.cssand rebuild. Inpackages/ui/build-module/dialog/header.mjs, the classes composed from that file (__header,__title,__contentand__footer) get new names, and the classes defined indialog/style.module.csskeep theirs.Dialog,Drawer,AlertDialogandMenufrom the built package, open each one, and compare it with a build from trunk. The composed header, footer, title, content and dropdown motion styles still apply, and the computed styles match. Storybook compiles@wordpress/uifrom source with Vite's CSS module support, so it does not exercise this code path.✍️ Dev note for WP 7.2
Update bundled
@wordpress/*packages and@wordpress/buildto fix CSS module class name collisionsIf your plugin bundles
@wordpress/*packages that use CSS modules, or compiles its own CSS modules with@wordpress/build, take these steps before WordPress 7.2:@wordpress/ui0.26.0,@wordpress/dataviews22.0.0,@wordpress/admin-ui3.1.0, and@wordpress/grid0.11.0. If your plugin bundles@wordpress/componentsinstead of using thewp-componentsscript, update it to 44.0.0 or later.@wordpress/build, update to 0.27.0 or later and rebuild.The problem
Some
@wordpress/*packages are bundled into each plugin that uses them instead of being loaded once as shared scripts. A bug in@wordpress/build, the tool that compiles these packages, made bundled copies of a package at different versions share CSS module class names. Class names were meant to change whenever the CSS that defines them changes, so that each copy's styles apply only to its own components. Instead, they depended only on the file path and the class name. When two copies with different styles were loaded on one page, for example one in WordPress and another bundled in a plugin, each copy's styles also applied to the other copy's components. The result could match neither version, such as a focus ring appearing on every hovered tab (#85896).This affects every version of these packages published before the fix.
What changes in WordPress 7.2
Starting with WordPress 7.2, class names change whenever the CSS that defines them changes (#85900). Two copies share class names only when their CSS is identical, so a copy with different styles no longer affects the other.
The packages that ship with WordPress 7.2 are built this way. A copy your plugin bundled from an earlier version no longer collides with them. However, two copies built before the change can still collide with each other, for example when two plugins on the same site bundle different older versions of
@wordpress/ui.