Repository navigation
Open
Labels
[Type] Code QualityIssues or PRs that relate to code quality
Description
What problem does this address?
@wordpress/wp-build inlines all imported stylesheets into the JS bundle as runtime-injected <style> elements (with data-wp-hash deduplication). This means that if someone adds import './style.scss' in a JS file, it silently "works" at runtime, making it easy to miss that this is the wrong pattern.
In most packages, stylesheets should be imported through the package's root-level style.scss, not directly in JS. Importing in JS bypasses the standard stylesheet pipeline and causes the styles to be injected at runtime via document.head.appendChild. This has several problems:
- Redundancy. If the stylesheet is already in the root
style.scsschain, it gets loaded twice. - No RTL processing. Most packages rely on the build-time RTL transformation to generate
-rtl.cssstylesheets. Runtime-injected styles bypass this entirely, so any directional styles (margins, paddings, borders, etc.) will be broken in RTL locales.
I found three existing cases of this:
packages/block-editor/src/components/block-visibility/modal.jsimports./style.scss. This is redundant because the same file is already in the rootpackages/block-editor/src/style.scss.packages/editor/src/components/collaborators-presence/index.tsximports./styles/collaborators-presence.scss, andlist.tsximports./styles/collaborators-list.scss. These are not in the root stylesheet at all, so they're only loaded through the runtime injection.
What is your proposed solution?
- Fix the existing cases. Remove the SCSS imports from the JS/TS files above. For the
collaborators-presencestylesheets, also add them topackages/editor/src/style.scss. - Add a lint rule to prevent importing non-module
.scss/.cssfiles in JS/TS files. This should flag side-effect-only imports likeimport './style.scss'while allowing CSS module imports that have a binding (e.g.,import styles from './style.module.scss'). - Consider restricting
wp-buildinline injection to CSS modules only. Currently thecompileInlineStyletransform inwp-buildprocesses all.css/.scssimports the same way. If it only applied the runtime injection to.module.css/.module.scssfiles, developers would notice that the styles are missing.
Metadata
Metadata
Assignees
Labels
[Type] Code QualityIssues or PRs that relate to code quality
Activity
mirka commentedon Mar 21, 2026
@jsnajdr What do you think about solution 3? Was there any intentionality to the fact that we don't restrict the inlining behavior to CSS modules?
jsnajdr commentedon Mar 23, 2026
So the rule would be like the following?
import 'style.scss'statements in JS code are allowed only for CSS modules that ensure that the class names are mangled and unique.@wordpress/block-editor/style.css, and such styles are supposed to be loaded withwp_enqueue_style.Why not, but we need to ensure that this rule is really universal, both for Gutenberg and also all the 3rd party plugins that use
wp-build.At this moment,
import 'style.scss'is used at many places in Gutenberg that use the "new WP Admin routing framework", or how to call it. For example, there is this style import inroutes/post-list:https://github.com/WordPress/gutenberg/blob/trunk/routes/post-list/stage.tsx#L51
It defines class names like
dataviews-action-modal__quick-editorroutes-post-list__tabs-wrapperthat are then used inpost-listJSX markup. There are 20+ usages inroutes/*andpackages/boot, mostly added by @youknowriad. Should they be all migrated to CSS modules? Looks like a good idea to me.Then we have imports like this one in
packages/notices:https://github.com/WordPress/gutenberg/blob/trunk/packages/notices/src/components/inline-notices/index.tsx#L17
Because
wp-noticesis a registered script, this usage looks like one of the bugs you want to fix and avoid with this PR, is that right? The styles in question override thecomponents-noticestyle fromwp-components, which is probably completely unwanted. And if the style was wanted, it should be a stylesheet exported by the@wordpress/noticespackage and registered withwp_enqueue_style( 'wp-notices' ).mirka commentedon May 7, 2026
Heads up — the directly imported stylesheets in these packages/routes need to be either:
packages/boot: @youknowriadpackages/lazy-editor: @youknowriadpackages/notices: @gigituxpackages/workflow: @jorgefilipecostaroutes/connectors-home: @jorgefilipecostaroutes/experiments-home: @t-hamanoroutes/font-list: @youknowriadroutes/guidelines: @aswasif007routes/navigation-edit: @youknowriadroutes/navigation-list: @youknowriadroutes/pattern-list: @youknowriadroutes/post-list: @youknowriadroutes/post-type-edit: @ntsekourasroutes/post-types-list: @ntsekourasroutes/styles: @youknowriadroutes/taxonomies-list: @ntsekourasroutes/taxonomy-edit: @ntsekourasroutes/template-list: @youknowriadroutes/template-part-list: @youknowriadThis stylesheet import pattern is now a lint error, but the existing violations in the paths above are bulk suppressed. If you're able, please switch the stylesheet imports to whichever pattern is preferable for the package/route. Otherwise, RTL styles are likely broken.
youknowriad commentedon May 7, 2026
Moving these to modules is ok for me but I don't understand why we just don't apply this linting rule to all imported CSS files form JS
mirka commentedon May 8, 2026
I don't think we can detect that automatically, but if folks want to set up exceptions (stylelint, eslint) for their package/route I think that's fine. Though, I do think transitioning to CSS modules would be better for overall architecture, so it isn't bad to encourage adoption.
We just need to be intentional and consistent at the package level, because most of the violations I already fixed in the first pass were clearly just mistakes that deviated from the package's convention.
youknowriad commentedon May 8, 2026
I think we can, for instance, add a rule for all routes
mirka commentedon May 13, 2026
I can start linting all route
.scssfiles for logical properties as a stopgap, but it doesn't really remove the need to encourage CSS modules when importing stylesheets from JS files.Gutenberg contains a lot of fragile styling practices where class names in a given stylesheet aren't clearly encapsulated to a single JS file, and are actually used in multiple JS files. When you continue doing that while making it behave like there's a clear dependency relationship between the single JS file and the stylesheet, that's going to start introducing bugs that wouldn't even have happened when there was just a central stylesheet entry. So to prevent a further regression of the Gutenberg CSS architecture, the cleanup work still needs to happen.
youknowriad commentedon May 13, 2026
I'd encourage the migration as well, I'm busy with other things myself though.