This repository demonstrates a critical bug in @rslib/core v0.14.0 where injectStyles: true with ESM format generates CommonJS artifacts in ESM bundles, causing runtime errors.
The issue occurs when ALL these conditions are met:
- ✅ injectStyles: truein rslib config
- ✅ format: "esm"(ESM output)
- ✅ @font-facedeclarations in CSS
- ✅ Any export from the module importing CSS
Key Finding: Not React-specific - any export triggers the issue.
Files needed:
src/
├── index.ts     # import "./globals.css"; export const foo = "bar";
├── globals.css  # @font-face { font-family: "Test"; src: url("..."); }
Result: module.id references in ESM bundle → ReferenceError: module is not defined
- 
Install and build: npm install npm run build 
- 
Verify the bug: grep "module.id" dist/index.js # Output: module.id references found 
- 
Test runtime error: npm run build:consumer npm run preview # Open http://localhost:4173 → Console shows ReferenceError
| CSS Import | Export | module.id | Result | 
|---|---|---|---|
| None | Yes | 0 | ✅ Works | 
| @font-face | None | 1 | |
| @font-face | Yes | 1 | ❌ Runtime Error | 
Generated ESM code contains:
___CSS_LOADER_EXPORT___.push([
  module.id, // ← CommonJS artifact in ESM!
  `@font-face { ... }`,
]);Runtime error:
Uncaught ReferenceError: module is not defined
Problematic config:
export default defineConfig({
  lib: [{ format: "esm" }],
  output: { injectStyles: true }, // ← Causes issue with @font-face
});export default defineConfig({
  output: { injectStyles: false }, // Generate separate CSS
});import { createGlobalStyle } from "styled-components";
const GlobalStyles = createGlobalStyle`@font-face { ... }`;// Library: No CSS import
export const foo = "bar";
// Consumer: Import CSS separately
import "library/dist/index.css";
import { foo } from "library";Why this is an rslib issue (not Vite):
- rslib generates the module.idreferences during build
- ESM/CommonJS mismatch in rslib's CSS injection mechanism
- Vite correctly rejects CommonJS artifacts in ESM context
Evidence:
- injectStyles: true→ 14.9 kB bundle with- module.id
- injectStyles: false→ 0.03 kB bundle + separate CSS
- Blocks ESM adoption for libraries using fonts
- Forces workarounds or separate CSS handling
- Critical for modern library development
- src/- Minimal reproduction library
- consumer-app/- Test consumer application
- ISSUE_DETAILS.md- Comprehensive technical analysis
- rslib.config.ts- Configuration demonstrating issue
rslib should properly transform CSS loader output for ESM compatibility, eliminating CommonJS artifacts like module.id in ESM bundles.
- @rslib/core: 0.14.0
- @rsbuild/core: 1.5.12
- @rsbuild/plugin-react: 1.4.1
- Node.js: Latest
- Browser: Any modern browser
Status: ✅ Confirmed Critical Bug - Ready for rslib team investigation