Skip to content

Commit af1a983

Browse files
committed
fix: do not modify filename when cssModules.auto: false
1 parent ca03721 commit af1a983

File tree

8 files changed

+95
-3
lines changed

8 files changed

+95
-3
lines changed

packages/core/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,7 @@ async function composeLibRsbuildConfig(
15081508
});
15091509
const cssConfig = composeCssConfig(
15101510
outBase,
1511+
cssModulesAuto,
15111512
config.bundle,
15121513
banner?.css,
15131514
footer?.css,

packages/core/src/css/cssConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const PLUGIN_NAME = 'rsbuild:lib-css';
134134
// 2. replace CssExtractPlugin with LibCssExtractPlugin
135135
const pluginLibCss = (
136136
rootDir: string,
137+
auto: CssLoaderOptionsAuto,
137138
banner?: string,
138139
footer?: string,
139140
): RsbuildPlugin => ({
@@ -168,6 +169,7 @@ const pluginLibCss = (
168169
.loader(require.resolve('./libCssExtractLoader.js'))
169170
.options({
170171
rootDir,
172+
auto,
171173
banner,
172174
footer,
173175
});
@@ -185,6 +187,7 @@ const pluginLibCss = (
185187

186188
export const composeCssConfig = (
187189
rootDir: string | null,
190+
auto: CssLoaderOptionsAuto,
188191
bundle = true,
189192
banner?: string,
190193
footer?: string,
@@ -194,7 +197,7 @@ export const composeCssConfig = (
194197
}
195198

196199
return {
197-
plugins: [pluginLibCss(rootDir, banner, footer)],
200+
plugins: [pluginLibCss(rootDir, auto, banner, footer)],
198201
tools: {
199202
cssLoader: {
200203
// Otherwise, external variables will be executed by css-extract and cause an error.

packages/core/src/css/libCssExtractLoader.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import path, { extname } from 'node:path';
99
import type { Rspack } from '@rsbuild/core';
10+
import { type CssLoaderOptionsAuto, isCssModulesFile } from './cssConfig';
1011

1112
export const BASE_URI = 'webpack://';
1213
export const MODULE_TYPE = 'css/mini-extract';
@@ -38,6 +39,7 @@ export interface CssExtractRspackLoaderOptions {
3839
defaultExport?: boolean;
3940

4041
rootDir?: string;
42+
auto?: CssLoaderOptionsAuto;
4143
banner?: string;
4244
footer?: string;
4345
}
@@ -89,6 +91,7 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
8991
const callback = this.async();
9092
const filepath = this.resourcePath;
9193
const rootDir = options.rootDir ?? this.rootContext;
94+
const auto = options.auto;
9295
const banner = options.banner;
9396
const footer = options.footer;
9497

@@ -265,7 +268,10 @@ export const pitch: Rspack.LoaderDefinition['pitch'] = function (
265268
if (ext !== 'css') {
266269
distFilepath = distFilepath.replace(ext, '.css');
267270
}
268-
distFilepath = distFilepath.replace(/\.module\.css/, '_module.css');
271+
console.log(111, distFilepath, auto);
272+
if (isCssModulesFile(filepath, auto)) {
273+
distFilepath = distFilepath.replace(/\.module\.css/, '_module.css');
274+
}
269275
const cssFilename = path.basename(distFilepath);
270276
if (content.trim()) {
271277
m.get(distFilepath)

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
$border-dark: rgba($base-color, 0.88);
2+
13
.content-wrapper {
2-
background-color: #fff;
4+
background-color: $border-dark;
35
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "css-modules-bundle-false-auto-false-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { pluginSass } from '@rsbuild/plugin-sass';
2+
import { defineConfig } from '@rslib/core';
3+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
4+
5+
export default defineConfig({
6+
lib: [
7+
generateBundleEsmConfig({ bundle: false }),
8+
generateBundleCjsConfig({ bundle: false }),
9+
],
10+
source: {
11+
entry: {
12+
index: ['../__fixtures__/basic/src/**'],
13+
},
14+
},
15+
plugins: [
16+
pluginSass({
17+
sassLoaderOptions: {
18+
additionalData: '$base-color: #c6538c;',
19+
},
20+
}),
21+
],
22+
output: {
23+
target: 'web',
24+
cssModules: {
25+
auto: false,
26+
},
27+
},
28+
});

tests/integration/style/css-modules/index.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,47 @@ test('should extract css-modules successfully in bundle-false with output.cssMod
8585
'require("./reset.css")',
8686
);
8787
});
88+
89+
test('should extract css-modules successfully in bundle-false with output.cssModules.auto:false config', async () => {
90+
const fixturePath = join(__dirname, 'bundle-false-auto-false');
91+
const result = await buildAndGetResults({ fixturePath, type: 'all' });
92+
93+
const contents = result.css.contents;
94+
const jsContents = result.js.contents;
95+
96+
const esmFiles = Object.keys(contents.esm);
97+
expect(esmFiles).toMatchInlineSnapshot(`
98+
[
99+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto-false/dist/esm/button/index.module.css",
100+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto-false/dist/esm/reset.css",
101+
]
102+
`);
103+
expectFileContainContent(
104+
jsContents.esm,
105+
'button/index.js',
106+
'from "./index.module.css"',
107+
);
108+
expectFileContainContent(
109+
jsContents.esm,
110+
'dist/esm/index.js',
111+
'import "./reset.css"',
112+
);
113+
114+
const cjsFiles = Object.keys(contents.cjs);
115+
expect(cjsFiles).toMatchInlineSnapshot(`
116+
[
117+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto-false/dist/cjs/button/index.module.css",
118+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto-false/dist/cjs/reset.css",
119+
]
120+
`);
121+
expectFileContainContent(
122+
jsContents.cjs,
123+
'button/index.cjs',
124+
'require("./index.module.css")',
125+
);
126+
expectFileContainContent(
127+
jsContents.cjs,
128+
'dist/cjs/index.cjs',
129+
'require("./reset.css")',
130+
);
131+
});

0 commit comments

Comments
 (0)