Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep importing .module.scss file compiles in dev but not build #19003

Open
7 tasks done
moishinetzer opened this issue Dec 18, 2024 · 1 comment
Open
7 tasks done

Deep importing .module.scss file compiles in dev but not build #19003

moishinetzer opened this issue Dec 18, 2024 · 1 comment

Comments

@moishinetzer
Copy link

moishinetzer commented Dec 18, 2024

Describe the bug

Description

When importing .module.scss files directly from a dependency package (in my reproduction case @acme/ui is the "external" package, but you could imagine any dependency instead) in a Vite project, the imports work correctly in development mode but none of the styles are applied/compiled during production build. This does not work both when importing a module from another module scss file or directly from the application code.

Reproduction

Project structure:

apps/
  vite-project/
    src/
      App.tsx
packages/
  ui/
    src/
      button.module.scss
      button.tsx

Code Examples

apps/vite-project/src/App.tsx

import '@acme/ui/src/button.module.scss'
// This import works in dev but fails in build

packages/ui/package.json

{
  "name": "@acme/ui",
  "exports": {
    ".": {
      "types": "./dist/src/index.d.ts",
      "default": "./src/index.ts"
    },
    "./*": {
      "types": "./dist/src/*.d.ts",
      "default": [
        "./src/*.ts",
        "./src/*.tsx"
      ]
    }
  }
}

Expected Behavior

  • SCSS Modules imports from workspace packages should work consistently in both development and production builds even when deeply imported from dependency
  • Styles should be properly processed and included in the final bundle

Actual Behavior

  • Development mode (pnpm dev): CSS Modules imports work correctly

image

  • Production build (pnpm build): Build does not apply styles with module resolution errors for deep imports of .module.scss files from node modules

image

Environment

  • Vite: 6.0.1 (IMPORTANT: Also not working in 5.4.10)
  • Node.js: 20.18
  • OS: MacOS Latest
  • Package Manager: pnpm (also appears using npm, yarn)

Additional Context

This issue appears to be related to how Vite handles CSS Modules imports from node_modules during production builds. While the development server can resolve these imports through the workspace, the production build seems to have different module resolution rules.

Would appreciate guidance on the recommended approach to handle SCSS Modules imports from both workspace packages and dependencies in Vite projects.

Reproduction

https://github.com/moishinetzer/vite-scss-modules-reproduction

Steps to reproduce

Compare pnpm dev with pnpm build

System Info

System:
    OS: macOS 14.7.2
    CPU: (8) arm64 Apple M2
    Memory: 60.22 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.12.2/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
    Watchman: 2024.11.11.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 131.0.6778.140
    Safari: 18.2

Used Package Manager

pnpm

Logs

VITE v6.0.3 ready in 115 ms

➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help
Deprecation Warning: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.

More info: https://sass-lang.com/d/legacy-js-api

Validations

@sapphi-red
Copy link
Member

More smaller reproduction: https://stackblitz.com/edit/vitejs-vite-kakz5upv?terminal=dev

This is caused by #16051.
The workaround is to add this plugin: #16051 (comment).

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

No branches or pull requests

2 participants