Skip to content

Commit b281570

Browse files
committed
chore: add css-modules and less test cases
1 parent aad6617 commit b281570

File tree

29 files changed

+360
-7
lines changed

29 files changed

+360
-7
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import button from './button/index';
2-
// @ts-ignore
3-
import stylesAuto from './reset.scss';
2+
import './reset.scss';
43

5-
export { stylesAuto, button };
4+
export { button };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "css-modules-bundle-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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: [generateBundleEsmConfig(), generateBundleCjsConfig()],
7+
source: {
8+
entry: {
9+
index: ['../__fixtures__/basic/src/index.ts'],
10+
},
11+
},
12+
plugins: [
13+
pluginSass({
14+
sassLoaderOptions: {
15+
additionalData: '$base-color: #c6538c;',
16+
},
17+
}),
18+
],
19+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { join } from 'node:path';
2+
import { buildAndGetResults, getFileBySuffix } from 'test-helper';
3+
import { expect, test } from 'vitest';
4+
5+
function expectFileContainContent(
6+
files: Record<string, string>,
7+
suffix: string,
8+
content: string,
9+
) {
10+
const fileContent = getFileBySuffix(files, suffix);
11+
expect(fileContent).toContain(content);
12+
}
13+
14+
test('should extract css-modules successfully in bundle', async () => {
15+
const fixturePath = join(__dirname, 'bundle');
16+
const { contents } = await buildAndGetResults(fixturePath, 'css');
17+
18+
const esmFiles = Object.keys(contents.esm);
19+
expect(esmFiles).toMatchInlineSnapshot(`
20+
[
21+
"<ROOT>/tests/integration/style/css-modules/bundle/dist/esm/static/css/index.css",
22+
]
23+
`);
24+
25+
const cjsFiles = Object.keys(contents.cjs);
26+
expect(cjsFiles).toMatchInlineSnapshot(`
27+
[
28+
"<ROOT>/tests/integration/style/css-modules/bundle/dist/cjs/static/css/index.css",
29+
]
30+
`);
31+
});
32+
33+
test('should extract css-modules successfully in bundle-false', async () => {
34+
const fixturePath = join(__dirname, 'bundle-false');
35+
const { contents } = await buildAndGetResults(fixturePath, 'css');
36+
const { contents: jsContents } = await buildAndGetResults(fixturePath, 'js');
37+
38+
const esmFiles = Object.keys(contents.esm);
39+
expect(esmFiles).toMatchInlineSnapshot(`
40+
[
41+
"<ROOT>/tests/integration/style/css-modules/bundle-false/dist/esm/button/index_module.css",
42+
"<ROOT>/tests/integration/style/css-modules/bundle-false/dist/esm/reset.css",
43+
]
44+
`);
45+
expectFileContainContent(
46+
jsContents.esm,
47+
'button/index.module.js',
48+
'import "./index_module.css"',
49+
);
50+
51+
const cjsFiles = Object.keys(contents.cjs);
52+
expect(cjsFiles).toMatchInlineSnapshot(`
53+
[
54+
"<ROOT>/tests/integration/style/css-modules/bundle-false/dist/cjs/button/index_module.css",
55+
"<ROOT>/tests/integration/style/css-modules/bundle-false/dist/cjs/reset.css",
56+
]
57+
`);
58+
expectFileContainContent(
59+
jsContents.cjs,
60+
'button/index.module.cjs',
61+
'require("./index_module.css")',
62+
);
63+
});
64+
65+
test('should extract css-modules successfully in bundle-false with output.cssModules.auto config', async () => {
66+
const fixturePath = join(__dirname, 'bundle-false-auto');
67+
const { contents } = await buildAndGetResults(fixturePath, 'css');
68+
const { contents: jsContents } = await buildAndGetResults(fixturePath, 'js');
69+
70+
const esmFiles = Object.keys(contents.esm);
71+
expect(esmFiles).toMatchInlineSnapshot(`
72+
[
73+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto/dist/esm/button/index_module.css",
74+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto/dist/esm/reset.css",
75+
]
76+
`);
77+
expectFileContainContent(jsContents.esm, 'reset.js', 'import "./reset.css"');
78+
79+
const cjsFiles = Object.keys(contents.cjs);
80+
expect(cjsFiles).toMatchInlineSnapshot(`
81+
[
82+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto/dist/cjs/button/index_module.css",
83+
"<ROOT>/tests/integration/style/css-modules/bundle-false-auto/dist/cjs/reset.css",
84+
]
85+
`);
86+
expectFileContainContent(
87+
jsContents.cjs,
88+
'reset.cjs',
89+
'require("./reset.css")',
90+
);
91+
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { join } from 'node:path';
22
import { buildAndGetResults } from 'test-helper';
33
import { expect, test } from 'vitest';
44

5-
test('should extract css successfully when set bundle: true', async () => {
5+
test('should extract css successfully in bundle', async () => {
66
const fixturePath = join(__dirname, 'bundle');
77
const { contents } = await buildAndGetResults(fixturePath, 'css');
88
const esmFiles = Object.keys(contents.esm);
@@ -21,7 +21,7 @@ test('should extract css successfully when set bundle: true', async () => {
2121
`);
2222
});
2323

24-
test('should extract css successfully when set bundle: false', async () => {
24+
test('should extract css successfully in bundle-false', async () => {
2525
const fixturePath = join(__dirname, 'bundle-false');
2626
const { contents } = await buildAndGetResults(fixturePath, 'css');
2727
const esmFiles = Object.keys(contents.esm);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!node_modules
2+
node_modules/.*
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import './nest/nest.less';
2+
@import '~/alias.css';
3+
4+
@url: './assets/normal.ttf';
5+
6+
.url-variable {
7+
font-family: url(@url);
8+
}
9+
10+
@border-dark: rgba(@base-color, 0.88);
11+
12+
.alert {
13+
border: 1px solid @border-dark;
14+
margin: 10px / 2;
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
color: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.div {
2+
font-family: url('../assets/normal.ttf');
3+
}

0 commit comments

Comments
 (0)