vite: `sass` dynamic import is undefined when building with "modern-compiler" and sass-embedded

Describe the bug

I expect to do builds without any crashes relating to “sass” being an undefined import.

The actual behavior is that in css.ts, the dynamic import is expecting properties to be on the default export but instead properties are exported individually (named exports w/e you want to call it).

Reproduction

stackblitz link wasn’t working out. had a different sass-embedded error lmao

just have sass files and try importing them and then doing a vite build with sass-embedded. the sass-embedded dynamic import will try to grab a default export but sass embedded doesn’t have one

Steps to reproduce

  1. import sass file
  2. run vite build

System Info

System:
    OS: macOS 15.1.1
    CPU: (8) arm64 Apple M2
    Memory: 1.10 GB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.3.0 - ~/.local/state/fnm_multishells/60516_1734950576395/bin/node
    Yarn: 4.5.3 - /opt/homebrew/bin/yarn
    npm: 10.9.0 - ~/.local/state/fnm_multishells/60516_1734950576395/bin/npm
    bun: 1.1.42 - /opt/homebrew/bin/bun
    Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 131.0.6778.205
    Firefox Nightly: 134.0a1
    Safari: 18.1.1
  npmPackages:
    @vitejs/plugin-react: ^4.3.1 => 4.3.3
    rollup: ^4.21.2 => 4.27.3
    vite: ^6.0.5 => 6.0.5

Used Package Manager

yarn

Logs

      compilerPromise ??= sass.initAsyncCompiler();
                               ^


[vite:css] [sass] Cannot read properties of undefined (reading 'initAsyncCompiler')
file: /Users/cod1r/workspace/repositories/games/spelltower/spelltower.scss```

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitejs/vite/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vite.dev/guide).
- [X] Check that there isn't [already an issue](https://github.com/vitejs/vite/issues) that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to [vuejs/core](https://github.com/vuejs/core) instead.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitejs/vite/discussions) or join our [Discord Chat Server](https://chat.vite.dev/).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 23 (5 by maintainers)

Most upvoted comments

While yes, this is a TSX or TypeScript-itself issue, this kinda blocks SSR apps in ESM. For dev servers, the Vite SSR Guide uses a Vite middleware on your backend app… of which, if you’re using TypeScript ESM; won’t work.

And the workaround is pretty simple…

https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/css.ts#L2483-L2485

^ these lines could just become:

      const sassImport = await import(pathToFileURL(sassPath).href);
      const sass: typeof Sass = sassImport.default ?? sassImport; // workaround from #19052
      compilerPromise ??= sass.initAsyncCompiler()

Then a backend using TSX could still run the Vite dev server with modern-compiler. Otherwise, we’re kinda just waiting for a resolution on privatenumber/tsx#442 … which seems kinda low-priority to the TSX team, or might even be a TypeScript design decision.

I’m not sure if it’s a bug, but tsx resolves imports differently than modern node versions. When vite tries to import sass/sass-embedded, it’s expecting an object with a ‘default’ key, but there is none when using tsx, which is why sass ends up being undefined. I think it might have something to do with tsx doing something weird with dynamic imports, but I’m not sure.

Another solution might be to use another runtime like deno/bun to run typescript scripts instead of using tsx/ts-node.