|
1 |
| -import { dirname } from 'node:path'; |
| 1 | +import fs from 'node:fs'; |
| 2 | +import { dirname, join, resolve } from 'node:path'; |
2 | 3 | import { fileURLToPath } from 'node:url';
|
3 | 4 | import { expect, test } from '@playwright/test';
|
4 | 5 | import { createRsbuild } from '@rsbuild/core';
|
5 |
| -import { pluginTypedCSSModules } from '../../src'; |
6 |
| -import { getRandomPort } from '../helper'; |
| 6 | +import { pluginLess } from '@rsbuild/plugin-less'; |
| 7 | +import { pluginSass } from '@rsbuild/plugin-sass'; |
| 8 | +import { pluginStylus } from '@rsbuild/plugin-stylus'; |
| 9 | +import { pluginTypedCSSModules } from '../../dist'; |
7 | 10 |
|
8 | 11 | const __dirname = dirname(fileURLToPath(import.meta.url));
|
9 | 12 |
|
10 |
| -test('should render page as expected', async ({ page }) => { |
| 13 | +const generatorTempDir = async (testDir: string) => { |
| 14 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 15 | + await fs.promises.cp(join(__dirname, 'src'), testDir, { recursive: true }); |
| 16 | + |
| 17 | + return () => fs.promises.rm(testDir, { force: true, recursive: true }); |
| 18 | +}; |
| 19 | + |
| 20 | +test('generator TS declaration for cssModules.auto true', async () => { |
| 21 | + const testDir = join(__dirname, 'test-temp-src-1'); |
| 22 | + const clear = await generatorTempDir(testDir); |
| 23 | + |
| 24 | + const rsbuild = await createRsbuild({ |
| 25 | + cwd: __dirname, |
| 26 | + rsbuildConfig: { |
| 27 | + plugins: [ |
| 28 | + pluginLess(), |
| 29 | + pluginSass(), |
| 30 | + pluginStylus(), |
| 31 | + pluginTypedCSSModules(), |
| 32 | + ], |
| 33 | + source: { |
| 34 | + entry: { index: resolve(testDir, 'index.js') }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + await rsbuild.build(); |
| 40 | + |
| 41 | + expect(fs.existsSync(join(testDir, './a.css.d.ts'))).toBeFalsy(); |
| 42 | + expect(fs.existsSync(join(testDir, './b.module.scss.d.ts'))).toBeTruthy(); |
| 43 | + expect(fs.existsSync(join(testDir, './c.module.less.d.ts'))).toBeTruthy(); |
| 44 | + expect(fs.existsSync(join(testDir, './d.global.less.d.ts'))).toBeFalsy(); |
| 45 | + expect(fs.existsSync(join(testDir, './e.module.styl.d.ts'))).toBeTruthy(); |
| 46 | + |
| 47 | + const bContent = fs.readFileSync(join(testDir, './b.module.scss.d.ts'), { |
| 48 | + encoding: 'utf-8', |
| 49 | + }); |
| 50 | + |
| 51 | + expect(bContent).toEqual(`// This file is automatically generated. |
| 52 | +// Please do not change this file! |
| 53 | +interface CssExports { |
| 54 | + _underline: string; |
| 55 | + btn: string; |
| 56 | + default: string; |
| 57 | + primary: string; |
| 58 | + 'the-b-class': string; |
| 59 | + theBClass: string; |
| 60 | + underline: string; |
| 61 | +} |
| 62 | +declare const cssExports: CssExports; |
| 63 | +export default cssExports; |
| 64 | +`); |
| 65 | + |
| 66 | + const cContent = fs.readFileSync(join(testDir, './c.module.less.d.ts'), { |
| 67 | + encoding: 'utf-8', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(cContent).toEqual(`// This file is automatically generated. |
| 71 | +// Please do not change this file! |
| 72 | +interface CssExports { |
| 73 | + 'the-c-class': string; |
| 74 | + theCClass: string; |
| 75 | +} |
| 76 | +declare const cssExports: CssExports; |
| 77 | +export default cssExports; |
| 78 | +`); |
| 79 | + |
| 80 | + await clear(); |
| 81 | +}); |
| 82 | + |
| 83 | +test('generator TS declaration for cssModules.auto function', async () => { |
| 84 | + const testDir = join(__dirname, 'test-temp-src-2'); |
| 85 | + const clear = await generatorTempDir(testDir); |
| 86 | + |
| 87 | + const rsbuild = await createRsbuild({ |
| 88 | + cwd: __dirname, |
| 89 | + rsbuildConfig: { |
| 90 | + plugins: [ |
| 91 | + pluginLess(), |
| 92 | + pluginSass(), |
| 93 | + pluginStylus(), |
| 94 | + pluginTypedCSSModules(), |
| 95 | + ], |
| 96 | + source: { |
| 97 | + entry: { index: resolve(testDir, 'index.js') }, |
| 98 | + }, |
| 99 | + output: { |
| 100 | + cssModules: { |
| 101 | + auto: (path) => { |
| 102 | + return path.endsWith('.scss'); |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + await rsbuild.build(); |
| 110 | + |
| 111 | + expect(fs.existsSync(join(testDir, './a.css.d.ts'))).toBeFalsy(); |
| 112 | + expect(fs.existsSync(join(testDir, './b.module.scss.d.ts'))).toBeTruthy(); |
| 113 | + expect(fs.existsSync(join(testDir, './c.module.less.d.ts'))).toBeFalsy(); |
| 114 | + expect(fs.existsSync(join(testDir, './d.global.less.d.ts'))).toBeFalsy(); |
| 115 | + |
| 116 | + await clear(); |
| 117 | +}); |
| 118 | + |
| 119 | +test('generator TS declaration for cssModules.auto Regexp', async () => { |
| 120 | + const testDir = join(__dirname, 'test-temp-src-3'); |
| 121 | + const clear = await generatorTempDir(testDir); |
| 122 | + |
11 | 123 | const rsbuild = await createRsbuild({
|
12 | 124 | cwd: __dirname,
|
13 | 125 | rsbuildConfig: {
|
14 |
| - plugins: [pluginTypedCSSModules()], |
15 |
| - server: { |
16 |
| - port: getRandomPort(), |
| 126 | + plugins: [ |
| 127 | + pluginLess(), |
| 128 | + pluginSass(), |
| 129 | + pluginStylus(), |
| 130 | + pluginTypedCSSModules(), |
| 131 | + ], |
| 132 | + source: { |
| 133 | + entry: { index: resolve(testDir, 'index.js') }, |
| 134 | + }, |
| 135 | + output: { |
| 136 | + cssModules: { |
| 137 | + auto: /\.module\./i, |
| 138 | + }, |
17 | 139 | },
|
18 | 140 | },
|
19 | 141 | });
|
20 | 142 |
|
21 |
| - const { server, urls } = await rsbuild.startDevServer(); |
| 143 | + await rsbuild.build(); |
22 | 144 |
|
23 |
| - await page.goto(urls[0]); |
24 |
| - expect(await page.evaluate('window.test')).toBe(1); |
| 145 | + expect(fs.existsSync(join(testDir, './a.css.d.ts'))).toBeFalsy(); |
| 146 | + expect(fs.existsSync(join(testDir, './b.module.scss.d.ts'))).toBeTruthy(); |
| 147 | + expect(fs.existsSync(join(testDir, './c.module.less.d.ts'))).toBeTruthy(); |
| 148 | + expect(fs.existsSync(join(testDir, './d.global.less.d.ts'))).toBeFalsy(); |
25 | 149 |
|
26 |
| - await server.close(); |
| 150 | + await clear(); |
27 | 151 | });
|
28 | 152 |
|
29 |
| -test('should build succeed', async ({ page }) => { |
| 153 | +test('generator TS declaration for `asIs` convention', async () => { |
| 154 | + const testDir = join(__dirname, 'test-temp-src-4'); |
| 155 | + const clear = await generatorTempDir(testDir); |
| 156 | + |
30 | 157 | const rsbuild = await createRsbuild({
|
31 | 158 | cwd: __dirname,
|
32 | 159 | rsbuildConfig: {
|
33 |
| - plugins: [pluginTypedCSSModules()], |
| 160 | + plugins: [ |
| 161 | + pluginLess(), |
| 162 | + pluginSass(), |
| 163 | + pluginStylus(), |
| 164 | + pluginTypedCSSModules(), |
| 165 | + ], |
| 166 | + source: { |
| 167 | + entry: { index: resolve(testDir, 'index.js') }, |
| 168 | + }, |
| 169 | + output: { |
| 170 | + cssModules: { |
| 171 | + exportLocalsConvention: 'asIs', |
| 172 | + }, |
| 173 | + }, |
34 | 174 | },
|
35 | 175 | });
|
36 | 176 |
|
37 | 177 | await rsbuild.build();
|
38 |
| - const { server, urls } = await rsbuild.preview(); |
39 | 178 |
|
40 |
| - await page.goto(urls[0]); |
41 |
| - expect(await page.evaluate('window.test')).toBe(1); |
| 179 | + expect(fs.existsSync(join(testDir, './b.module.scss.d.ts'))).toBeTruthy(); |
| 180 | + expect(fs.existsSync(join(testDir, './c.module.less.d.ts'))).toBeTruthy(); |
| 181 | + |
| 182 | + const bContent = fs.readFileSync(join(testDir, './b.module.scss.d.ts'), { |
| 183 | + encoding: 'utf-8', |
| 184 | + }); |
| 185 | + const cContent = fs.readFileSync(join(testDir, './c.module.less.d.ts'), { |
| 186 | + encoding: 'utf-8', |
| 187 | + }); |
| 188 | + |
| 189 | + expect(bContent).toEqual(`// This file is automatically generated. |
| 190 | +// Please do not change this file! |
| 191 | +interface CssExports { |
| 192 | + _underline: string; |
| 193 | + btn: string; |
| 194 | + default: string; |
| 195 | + primary: string; |
| 196 | + 'the-b-class': string; |
| 197 | +} |
| 198 | +declare const cssExports: CssExports; |
| 199 | +export default cssExports; |
| 200 | +`); |
| 201 | + |
| 202 | + expect(cContent).toEqual(`// This file is automatically generated. |
| 203 | +// Please do not change this file! |
| 204 | +interface CssExports { |
| 205 | + 'the-c-class': string; |
| 206 | +} |
| 207 | +declare const cssExports: CssExports; |
| 208 | +export default cssExports; |
| 209 | +`); |
42 | 210 |
|
43 |
| - await server.close(); |
| 211 | + await clear(); |
44 | 212 | });
|
0 commit comments