Skip to content

Commit 6e06013

Browse files
committed
Fix vite, rsbuild
1 parent 9045f57 commit 6e06013

File tree

8 files changed

+226
-62
lines changed

8 files changed

+226
-62
lines changed

apps/rsbuild/rsbuild.config.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig } from '@rsbuild/core';
2-
import { pluginReact } from '@rsbuild/plugin-react';
3-
import { DevupUIRsbuildPlugin } from '@devup-ui/rsbuild-plugin';
1+
import { DevupUI } from '@devup-ui/rsbuild-plugin'
2+
import { defineConfig } from '@rsbuild/core'
3+
import { pluginReact } from '@rsbuild/plugin-react'
44

55
export default defineConfig({
6-
plugins: [pluginReact(), DevupUIRsbuildPlugin()],
7-
});
6+
plugins: [pluginReact(), DevupUI()],
7+
})

apps/vite/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function App() {
1414
hello
1515
<Lib />
1616
</Box>
17-
<Text color="#777777"></Text>
17+
<Text fontWeight="bold">wf</Text>
1818
<Text color="#777"></Text>
1919
<Text color="#777"></Text>
2020
<Text typography="header">typo</Text>

apps/vite/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig({
88
react(),
99
DevupUI({
1010
include: ['vite-lib-example'],
11+
// singleCss: true,
1112
}),
1213
],
1314
})

packages/rsbuild-plugin/src/__tests__/plugin.test.ts

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { existsSync } from 'node:fs'
22
import { mkdir, readFile, writeFile } from 'node:fs/promises'
3-
import { resolve } from 'node:path'
3+
import { join, resolve } from 'node:path'
44

5-
import { codeExtract, getThemeInterface } from '@devup-ui/wasm'
5+
import {
6+
codeExtract,
7+
getDefaultTheme,
8+
getThemeInterface,
9+
registerTheme,
10+
} from '@devup-ui/wasm'
611
import { vi } from 'vitest'
712

813
import { DevupUI } from '../plugin'
@@ -34,8 +39,10 @@ describe('DevupUIRsbuildPlugin', () => {
3439
expect(typeof plugin.setup).toBe('function')
3540

3641
const transform = vi.fn()
42+
const modifyRsbuildConfig = vi.fn()
3743
await plugin.setup({
3844
transform,
45+
modifyRsbuildConfig,
3946
} as any)
4047
expect(transform).toHaveBeenCalled()
4148
})
@@ -51,8 +58,10 @@ describe('DevupUIRsbuildPlugin', () => {
5158
expect(plugin).toBeDefined()
5259
expect(plugin.setup).toBeDefined()
5360
const transform = vi.fn()
61+
const modifyRsbuildConfig = vi.fn()
5462
await plugin.setup({
5563
transform,
64+
modifyRsbuildConfig,
5665
} as any)
5766
})
5867

@@ -66,8 +75,10 @@ describe('DevupUIRsbuildPlugin', () => {
6675
expect(plugin).toBeDefined()
6776
expect(plugin.setup).toBeDefined()
6877
const transform = vi.fn()
78+
const modifyRsbuildConfig = vi.fn()
6979
await plugin.setup({
7080
transform,
81+
modifyRsbuildConfig,
7182
} as any)
7283
})
7384

@@ -104,8 +115,10 @@ describe('DevupUIRsbuildPlugin', () => {
104115
expect(plugin).toBeDefined()
105116
expect(plugin.setup).toBeDefined()
106117
const transform = vi.fn()
118+
const modifyRsbuildConfig = vi.fn()
107119
await plugin.setup({
108120
transform,
121+
modifyRsbuildConfig,
109122
} as any)
110123
expect(transform).toHaveBeenCalled()
111124
expect(transform).toHaveBeenCalledWith(
@@ -130,8 +143,10 @@ describe('DevupUIRsbuildPlugin', () => {
130143
expect(plugin).toBeDefined()
131144
expect(plugin.setup).toBeDefined()
132145
const transform = vi.fn()
146+
const modifyRsbuildConfig = vi.fn()
133147
await plugin.setup({
134148
transform,
149+
modifyRsbuildConfig,
135150
} as any)
136151
expect(transform).toHaveBeenCalled()
137152
expect(transform).toHaveBeenCalledWith(
@@ -182,6 +197,7 @@ const App = () => <Box></Box>`,
182197
const transform = vi.fn()
183198
await plugin.setup({
184199
transform,
200+
modifyRsbuildConfig: vi.fn(),
185201
} as any)
186202
expect(transform).toHaveBeenCalled()
187203
expect(transform).toHaveBeenCalledWith(
@@ -220,4 +236,105 @@ const App = () => <Box></Box>`,
220236
map: undefined,
221237
})
222238
})
239+
it.each(
240+
createTestMatrix({
241+
watch: [true, false],
242+
existsDevupFile: [true, false],
243+
existsDistDir: [true, false],
244+
existsSheetFile: [true, false],
245+
existsClassMapFile: [true, false],
246+
existsFileMapFile: [true, false],
247+
existsCssDir: [true, false],
248+
getDefaultTheme: ['theme', ''],
249+
singleCss: [true, false],
250+
}),
251+
)('should write data files', async (options) => {
252+
vi.mocked(writeFile).mockResolvedValueOnce(undefined)
253+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify({}))
254+
vi.mocked(getThemeInterface).mockReturnValue('interface code')
255+
vi.mocked(getDefaultTheme).mockReturnValue(options.getDefaultTheme)
256+
vi.mocked(existsSync).mockImplementation((path) => {
257+
if (path === 'devup.json') return options.existsDevupFile
258+
if (path === 'df') return options.existsDistDir
259+
if (path === resolve('df', 'devup-ui')) return options.existsCssDir
260+
if (path === join('df', 'sheet.json')) return options.existsSheetFile
261+
if (path === join('df', 'classMap.json'))
262+
return options.existsClassMapFile
263+
if (path === join('df', 'fileMap.json')) return options.existsFileMapFile
264+
return false
265+
})
266+
const plugin = DevupUI({ singleCss: options.singleCss })
267+
await (plugin as any).setup({
268+
transform: vi.fn(),
269+
renderChunk: vi.fn(),
270+
generateBundle: vi.fn(),
271+
closeBundle: vi.fn(),
272+
resolve: vi.fn(),
273+
load: vi.fn(),
274+
modifyRsbuildConfig: vi.fn(),
275+
watchChange: vi.fn(),
276+
resolveId: vi.fn(),
277+
} as any)
278+
if (options.existsDevupFile) {
279+
expect(readFile).toHaveBeenCalledWith('devup.json', 'utf-8')
280+
expect(registerTheme).toHaveBeenCalledWith({})
281+
expect(getThemeInterface).toHaveBeenCalledWith(
282+
'@devup-ui/react',
283+
'DevupThemeColors',
284+
'DevupThemeTypography',
285+
'DevupTheme',
286+
)
287+
expect(writeFile).toHaveBeenCalledWith(
288+
join('df', 'theme.d.ts'),
289+
'interface code',
290+
'utf-8',
291+
)
292+
} else {
293+
expect(registerTheme).toHaveBeenCalledWith({})
294+
}
295+
296+
const modifyRsbuildConfig = vi.fn()
297+
await (plugin as any).setup({
298+
transform: vi.fn(),
299+
renderChunk: vi.fn(),
300+
generateBundle: vi.fn(),
301+
closeBundle: vi.fn(),
302+
resolve: vi.fn(),
303+
modifyRsbuildConfig,
304+
load: vi.fn(),
305+
watchChange: vi.fn(),
306+
resolveId: vi.fn(),
307+
} as any)
308+
if (options.getDefaultTheme) {
309+
expect(modifyRsbuildConfig).toHaveBeenCalledWith(expect.any(Function))
310+
const config = {
311+
source: {
312+
define: {},
313+
},
314+
}
315+
modifyRsbuildConfig.mock.calls[0][0](config)
316+
expect(config).toEqual({
317+
source: {
318+
define: {
319+
'process.env.DEVUP_UI_DEFAULT_THEME': JSON.stringify(
320+
options.getDefaultTheme,
321+
),
322+
},
323+
},
324+
})
325+
} else {
326+
expect(modifyRsbuildConfig).toHaveBeenCalledWith(expect.any(Function))
327+
const config = {
328+
source: {
329+
define: {},
330+
},
331+
}
332+
modifyRsbuildConfig.mock.calls[0][0](config)
333+
expect(config).toEqual({
334+
source: {
335+
define: {},
336+
},
337+
})
338+
}
339+
})
223340
})

packages/rsbuild-plugin/src/plugin.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { basename, join, resolve } from 'node:path'
44

55
import {
66
codeExtract,
7+
getCss,
8+
getDefaultTheme,
79
getThemeInterface,
810
registerTheme,
911
setDebug,
@@ -26,7 +28,7 @@ let globalCss = ''
2628
async function writeDataFiles(
2729
options: Omit<
2830
DevupUIRsbuildPluginOptions,
29-
'extractCss' | 'debug' | 'include' | 'singleCss'
31+
'extractCss' | 'debug' | 'include'
3032
>,
3133
) {
3234
try {
@@ -44,9 +46,11 @@ async function writeDataFiles(
4446
)
4547

4648
if (interfaceCode) {
47-
await writeFile(join(options.distDir, 'theme.d.ts'), interfaceCode, {
48-
encoding: 'utf-8',
49-
})
49+
await writeFile(
50+
join(options.distDir, 'theme.d.ts'),
51+
interfaceCode,
52+
'utf-8',
53+
)
5054
}
5155
} else {
5256
registerTheme({})
@@ -55,8 +59,14 @@ async function writeDataFiles(
5559
console.error(error)
5660
registerTheme({})
5761
}
58-
if (!existsSync(options.cssDir))
59-
await mkdir(options.cssDir, { recursive: true })
62+
await Promise.all([
63+
!existsSync(options.cssDir)
64+
? mkdir(options.cssDir, { recursive: true })
65+
: Promise.resolve(),
66+
!options.singleCss
67+
? writeFile(join(options.cssDir, 'devup-ui.css'), getCss())
68+
: Promise.resolve(),
69+
])
6070
}
6171

6272
export const DevupUI = ({
@@ -72,11 +82,16 @@ export const DevupUI = ({
7282
name: 'devup-ui-rsbuild-plugin',
7383
async setup(api) {
7484
setDebug(debug)
85+
86+
if (!existsSync(distDir)) await mkdir(distDir, { recursive: true })
87+
await writeFile(join(distDir, '.gitignore'), '*', 'utf-8')
88+
7589
await writeDataFiles({
7690
package: libPackage,
7791
cssDir,
7892
devupFile,
7993
distDir,
94+
singleCss,
8095
})
8196
if (!extractCss) return
8297

@@ -87,6 +102,19 @@ export const DevupUI = ({
87102
() => globalCss,
88103
)
89104

105+
api.modifyRsbuildConfig((config) => {
106+
const theme = getDefaultTheme()
107+
if (theme) {
108+
config.source ??= {}
109+
config.source.define = {
110+
'process.env.DEVUP_UI_DEFAULT_THEME':
111+
JSON.stringify(getDefaultTheme()),
112+
...config.source.define,
113+
}
114+
}
115+
return config
116+
})
117+
90118
api.transform(
91119
{
92120
test: /\.(tsx|ts|js|mjs|jsx)$/,

packages/rsbuild-plugin/tsconfig.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"compilerOptions": {
3-
"types": ["vite/client", "vitest/importMeta", "vitest/globals"],
3+
"types": [
4+
"vite/client",
5+
"vitest/importMeta",
6+
"vitest/globals"
7+
],
48
"strict": true,
59
"target": "ESNext",
610
"declaration": true,
@@ -22,5 +26,14 @@
2226
"noEmit": true,
2327
"baseUrl": ".",
2428
"jsx": "react-jsx"
25-
}
26-
}
29+
},
30+
"include": [
31+
"src",
32+
"vite.config.ts",
33+
"../../vitest.setup.ts"
34+
],
35+
"exclude": [
36+
"node_modules",
37+
"dist"
38+
]
39+
}

0 commit comments

Comments
 (0)