|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { dirname, join, resolve } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { expect, test } from '@playwright/test'; |
| 5 | +import { createRsbuild } from '@rsbuild/core'; |
| 6 | +import { pluginLess } from '@rsbuild/plugin-less'; |
| 7 | +import { pluginSass } from '@rsbuild/plugin-sass'; |
| 8 | +import { pluginTypedCSSModules } from '../../dist'; |
| 9 | + |
| 10 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | + |
| 12 | +const generatorTempDir = async (testDir: string) => { |
| 13 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 14 | + await fs.promises.cp(join(__dirname, 'src'), testDir, { recursive: true }); |
| 15 | + |
| 16 | + return () => fs.promises.rm(testDir, { force: true, recursive: true }); |
| 17 | +}; |
| 18 | + |
| 19 | +test('generator TS declaration for multiple Less or Sass plugins', async () => { |
| 20 | + const testDir = join(__dirname, 'test-temp-src-1'); |
| 21 | + const clear = await generatorTempDir(testDir); |
| 22 | + |
| 23 | + const rsbuild = await createRsbuild({ |
| 24 | + cwd: __dirname, |
| 25 | + rsbuildConfig: { |
| 26 | + plugins: [ |
| 27 | + pluginSass({ |
| 28 | + include: [/a\.module/], |
| 29 | + }), |
| 30 | + pluginSass({ |
| 31 | + include: [/b\.module/], |
| 32 | + }), |
| 33 | + pluginLess({ |
| 34 | + include: [/c\.module/], |
| 35 | + }), |
| 36 | + pluginLess({ |
| 37 | + include: [/d\.module/], |
| 38 | + }), |
| 39 | + pluginTypedCSSModules(), |
| 40 | + ], |
| 41 | + source: { |
| 42 | + entry: { index: resolve(testDir, 'index.js') }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + await rsbuild.build(); |
| 48 | + |
| 49 | + expect(fs.existsSync(join(testDir, './a.module.scss.d.ts'))).toBeTruthy(); |
| 50 | + expect(fs.existsSync(join(testDir, './b.module.scss.d.ts'))).toBeTruthy(); |
| 51 | + expect(fs.existsSync(join(testDir, './c.module.less.d.ts'))).toBeTruthy(); |
| 52 | + expect(fs.existsSync(join(testDir, './d.module.less.d.ts'))).toBeTruthy(); |
| 53 | + |
| 54 | + const aContent = fs.readFileSync(join(testDir, './a.module.scss.d.ts'), { |
| 55 | + encoding: 'utf-8', |
| 56 | + }); |
| 57 | + expect(aContent).toEqual(`// This file is automatically generated. |
| 58 | +// Please do not change this file! |
| 59 | +interface CssExports { |
| 60 | + a: string; |
| 61 | +} |
| 62 | +declare const cssExports: CssExports; |
| 63 | +export default cssExports; |
| 64 | +`); |
| 65 | + |
| 66 | + const bContent = fs.readFileSync(join(testDir, './b.module.scss.d.ts'), { |
| 67 | + encoding: 'utf-8', |
| 68 | + }); |
| 69 | + expect(bContent).toEqual(`// This file is automatically generated. |
| 70 | +// Please do not change this file! |
| 71 | +interface CssExports { |
| 72 | + b: string; |
| 73 | +} |
| 74 | +declare const cssExports: CssExports; |
| 75 | +export default cssExports; |
| 76 | +`); |
| 77 | + |
| 78 | + const cContent = fs.readFileSync(join(testDir, './c.module.less.d.ts'), { |
| 79 | + encoding: 'utf-8', |
| 80 | + }); |
| 81 | + expect(cContent).toEqual(`// This file is automatically generated. |
| 82 | +// Please do not change this file! |
| 83 | +interface CssExports { |
| 84 | + c: string; |
| 85 | +} |
| 86 | +declare const cssExports: CssExports; |
| 87 | +export default cssExports; |
| 88 | +`); |
| 89 | + |
| 90 | + const dContent = fs.readFileSync(join(testDir, './d.module.less.d.ts'), { |
| 91 | + encoding: 'utf-8', |
| 92 | + }); |
| 93 | + expect(dContent).toEqual(`// This file is automatically generated. |
| 94 | +// Please do not change this file! |
| 95 | +interface CssExports { |
| 96 | + d: string; |
| 97 | +} |
| 98 | +declare const cssExports: CssExports; |
| 99 | +export default cssExports; |
| 100 | +`); |
| 101 | + |
| 102 | + await clear(); |
| 103 | +}); |
0 commit comments