Skip to content

CSS Modules not included with import statment and resolve alias #8245

@hannessolo

Description

@hannessolo

Describe the bug

CSS module imports are not generated correctly (empty string instead of object holding class names) under the following conditions:

  • The css module file is imported through a resolve alias, eg import styles from "custom:css"
  • The javascript file importing the component is loaded lazily through React.lazy
  • The build is done using vite build - under vite dev, this bug does not occur!

Example (also in the reproduction linked below):

import style from 'custom:css';

function App() {
  console.log(style);
  return <div className={style.theClass}></div>;
}

export default App;
const App = lazy(() => import('./App'));

  return (
    <Suspense>
      <App />
    </Suspense>
  );

Generated code:

import{j as a}from"./index.0b3cfb79.js";
var s="";
function o(){return console.log(s),a("div",{className:s.theClass})}export{o as default};

I would expect s here to be something like {"theClass": "hashedClassName"} and not an empty string.

In the reproduction, navigate to dist/assets/App.hash.js to see the faulty output.

Reproduction

https://stackblitz.com/edit/vitejs-vite-sdjzjs?file=vite.config.js

System Info

System:
    OS: macOS 12.2.1
    CPU: (8) arm64 Apple M1
    Memory: 82.41 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Firefox: 100.0.1
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-react: ^1.2.0 => 1.3.1
    vite: ^2.8.4 => 2.9.9

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: cssp3-minor-bugAn edge case that only affects very specific usage (priority)regressionThe issue only appears after a new release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions