Skip to content
This repository was archived by the owner on Aug 28, 2026. It is now read-only.
This repository was archived by the owner on Aug 28, 2026. It is now read-only.

CSS modules with path which contains spaces does not work as expected #1626

Open

Description

@JounQin

Bug report

Actual Behavior

generated src_render_js.js:

image

source render.js:

import * as styles from './space folder/render.module.css';

export function render() {
  const el = document.createElement('div');
  el.className = styles.text; // styles.text contains a space which breaks styling
  document.getElementsByTagName('body')[0].appendChild(el);
  el.innerHTML = 'hello, world';
}

Expected Behavior

https://stackoverflow.com/questions/50812118/is-it-possible-to-use-the-space-character-in-css-class-names

The generated class is src-space folder-render-module__text-WVNeAh which contains a space, but it's not possible to escape spaces, so the spaces in path should be changed to __ for example.

How Do We Reproduce?

https://github.com/JounQin/rspack-css-modules-spaces

Run pnpm dev:webpack

Please paste the results of npx webpack-cli info here, and mention other relevant information

Activity

  1. JounQin commented on Dec 26, 2024

    @JounQin
    ContributorAuthor

    I tried to add space pattern into https://github.com/webpack-contrib/css-loader/blob/9dd19662bd2df289b75283ebf277d1875bbe5089/src/utils.js#L263 and then it works as expected, I'd raise a PR if it is accepted.

  2. JounQin commented on Dec 26, 2024

    @JounQin
    ContributorAuthor

    Not sure if #997 related.

  3. JounQin commented on Jan 13, 2025

    @JounQin
    ContributorAuthor

    @alexander-akait Any time to take a look?

  4. alexander-akait commented on Feb 4, 2025

    @alexander-akait
    Member

    Yeah, technically we have CSS.escape to escape special character before inserting them in JS and it works good for non space characters, but there are some limitation with the class attribute

    CSS.escape('1@a'); // -> \\31 \\@a, and you can using them in HTML class attribute
    CSS.escape('a b'); // -> a\\ b, you can't
    

    But https://html.spec.whatwg.org/multipage/dom.html#global-attributes:classes-2

    When specified on HTML elements, the class attribute must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

    Let's fix it

  5. JounQin commented on Apr 15, 2025

    @JounQin
    ContributorAuthor

    @alexander-akait I don't know how to add a test case for it easily, or we just change the filenameReservedRegex?

  6. alexander-akait commented on Apr 16, 2025

    @alexander-akait
    Member

    @JounQin I think we can change filenameReservedRegex and look at our tests, if we don't broken anything we can merge and do a patch release

  7. JounQin commented on Apr 16, 2025

    @JounQin
    ContributorAuthor
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

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions