Skip to content

Prevent non-module SCSS/CSS imports in JS/TS files #76744

Open

Description

@mirka

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.scss chain, it gets loaded twice.
  • No RTL processing. Most packages rely on the build-time RTL transformation to generate -rtl.css stylesheets. 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.js imports ./style.scss. This is redundant because the same file is already in the root packages/block-editor/src/style.scss.
  • packages/editor/src/components/collaborators-presence/index.tsx imports ./styles/collaborators-presence.scss, and list.tsx imports ./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-presence stylesheets, also add them to packages/editor/src/style.scss.
  • Add a lint rule to prevent importing non-module .scss/.css files in JS/TS files. This should flag side-effect-only imports like import './style.scss' while allowing CSS module imports that have a binding (e.g., import styles from './style.module.scss').
  • Consider restricting wp-build inline injection to CSS modules only. Currently the compileInlineStyle transform in wp-build processes all .css/.scss imports the same way. If it only applied the runtime injection to .module.css/.module.scss files, developers would notice that the styles are missing.

Activity

  1. added
    [Type] Code Quality
    on Mar 21, 2026
  2. mirka commented on Mar 21, 2026

    @mirka
    MemberAuthor

    @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?

  3. jsnajdr commented on Mar 23, 2026

    @jsnajdr
    Member

    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.
    • non-mangled, global styles are not allowed as JS imports at all. Such styles must be in a stylesheet that's explicity exported by a package as CSS, like @wordpress/block-editor/style.css, and such styles are supposed to be loaded with wp_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 in routes/post-list:

    https://github.com/WordPress/gutenberg/blob/trunk/routes/post-list/stage.tsx#L51

    It defines class names like dataviews-action-modal__quick-edit or routes-post-list__tabs-wrapper that are then used in post-list JSX markup. There are 20+ usages in routes/* and packages/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-notices is 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 the components-notice style from wp-components, which is probably completely unwanted. And if the style was wanted, it should be a stylesheet exported by the @wordpress/notices package and registered with wp_enqueue_style( 'wp-notices' ).

  4. mirka commented on May 7, 2026

    @mirka
    MemberAuthor

    Heads up — the directly imported stylesheets in these packages/routes need to be either:

    1. Converted to CSS modules (which are linted for RTL compatibility by enforcing logical CSS properties).
    2. Moved to the package's style entry point so a RTL version of the stylesheet can be generated automatically at build time.

    This 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.

  5. youknowriad commented on May 7, 2026

    @youknowriad
    Contributor

    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

  6. mirka commented on May 8, 2026

    @mirka
    MemberAuthor

    I don't understand why we just don't apply this linting rule to all imported CSS files form JS

    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.

  7. youknowriad commented on May 8, 2026

    @youknowriad
    Contributor

    I think we can, for instance, add a rule for all routes

  8. mirka commented on May 13, 2026

    @mirka
    MemberAuthor

    I can start linting all route .scss files 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.

  9. youknowriad commented on May 13, 2026

    @youknowriad
    Contributor

    I'd encourage the migration as well, I'm busy with other things myself though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] Code Quality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions