Skip to content

Commit 711ad57

Browse files
authored
Merge pull request #471 from dev-five-git/fix-turbo-default-theme-issue
Fix default theme issue
2 parents 8e675cf + 8a37e11 commit 711ad57

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

.changeset/rich-facts-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@devup-ui/next-plugin': patch
3+
---
4+
5+
Fix default theme issue

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
22
import { join, resolve } from 'node:path'
33

4-
import { getThemeInterface } from '@devup-ui/wasm'
4+
import { getDefaultTheme, getThemeInterface } from '@devup-ui/wasm'
55
import { DevupUIWebpackPlugin } from '@devup-ui/webpack-plugin'
66

77
import { DevupUI } from '../plugin'
@@ -14,6 +14,19 @@ vi.mock('@devup-ui/wasm', async (original) => ({
1414
...(await original()),
1515
registerTheme: vi.fn(),
1616
getThemeInterface: vi.fn(),
17+
getDefaultTheme: vi.fn(),
18+
exportSheet: vi.fn(() =>
19+
JSON.stringify({
20+
css: {},
21+
font_faces: {},
22+
global_css_files: [],
23+
imports: {},
24+
keyframes: {},
25+
properties: {},
26+
}),
27+
),
28+
exportClassMap: vi.fn(() => JSON.stringify({})),
29+
exportFileMap: vi.fn(() => JSON.stringify({})),
1730
}))
1831

1932
describe('DevupUINextPlugin', () => {
@@ -312,5 +325,67 @@ describe('DevupUINextPlugin', () => {
312325
})
313326
expect(writeFileSync).toHaveBeenCalledWith(join('df', '.gitignore'), '*')
314327
})
328+
it('should set DEVUP_UI_DEFAULT_THEME when getDefaultTheme returns a value', async () => {
329+
vi.stubEnv('TURBOPACK', '1')
330+
vi.stubEnv('DEVUP_UI_DEFAULT_THEME', '')
331+
vi.mocked(existsSync)
332+
.mockReturnValueOnce(true)
333+
.mockReturnValueOnce(true)
334+
.mockReturnValueOnce(true)
335+
.mockReturnValueOnce(false)
336+
vi.mocked(getDefaultTheme).mockReturnValue('dark')
337+
const config: any = {}
338+
const ret = DevupUI(config)
339+
340+
expect(process.env.DEVUP_UI_DEFAULT_THEME).toBe('dark')
341+
expect(ret.env).toEqual({
342+
DEVUP_UI_DEFAULT_THEME: 'dark',
343+
})
344+
expect(config.env).toEqual({
345+
DEVUP_UI_DEFAULT_THEME: 'dark',
346+
})
347+
})
348+
it('should not set DEVUP_UI_DEFAULT_THEME when getDefaultTheme returns undefined', async () => {
349+
vi.stubEnv('TURBOPACK', '1')
350+
vi.stubEnv('DEVUP_UI_DEFAULT_THEME', '')
351+
vi.mocked(existsSync)
352+
.mockReturnValueOnce(true)
353+
.mockReturnValueOnce(true)
354+
.mockReturnValueOnce(true)
355+
.mockReturnValueOnce(false)
356+
vi.mocked(getDefaultTheme).mockReturnValue(undefined)
357+
const config: any = {}
358+
const ret = DevupUI(config)
359+
360+
expect(process.env.DEVUP_UI_DEFAULT_THEME).toBe('')
361+
expect(ret.env).toBeUndefined()
362+
expect(config.env).toBeUndefined()
363+
})
364+
it('should set DEVUP_UI_DEFAULT_THEME and preserve existing env vars', async () => {
365+
vi.stubEnv('TURBOPACK', '1')
366+
vi.stubEnv('DEVUP_UI_DEFAULT_THEME', '')
367+
vi.mocked(existsSync)
368+
.mockReturnValueOnce(true)
369+
.mockReturnValueOnce(true)
370+
.mockReturnValueOnce(true)
371+
.mockReturnValueOnce(false)
372+
vi.mocked(getDefaultTheme).mockReturnValue('light')
373+
const config: any = {
374+
env: {
375+
CUSTOM_VAR: 'value',
376+
},
377+
}
378+
const ret = DevupUI(config)
379+
380+
expect(process.env.DEVUP_UI_DEFAULT_THEME).toBe('light')
381+
expect(ret.env).toEqual({
382+
CUSTOM_VAR: 'value',
383+
DEVUP_UI_DEFAULT_THEME: 'light',
384+
})
385+
expect(config.env).toEqual({
386+
CUSTOM_VAR: 'value',
387+
DEVUP_UI_DEFAULT_THEME: 'light',
388+
})
389+
})
315390
})
316391
})

packages/next-plugin/src/plugin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
exportClassMap,
66
exportFileMap,
77
exportSheet,
8+
getDefaultTheme,
89
getThemeInterface,
910
registerTheme,
1011
} from '@devup-ui/wasm'
@@ -96,6 +97,15 @@ export function DevupUI(
9697
const defaultSheet = JSON.parse(exportSheet())
9798
const defaultClassMap = JSON.parse(exportClassMap())
9899
const defaultFileMap = JSON.parse(exportFileMap())
100+
// for theme script
101+
const defaultTheme = getDefaultTheme()
102+
if (defaultTheme) {
103+
process.env.DEVUP_UI_DEFAULT_THEME = defaultTheme
104+
config.env ??= {}
105+
Object.assign(config.env, {
106+
DEVUP_UI_DEFAULT_THEME: defaultTheme,
107+
})
108+
}
99109

100110
const rules: NonNullable<typeof config.turbopack.rules> = {
101111
[`./${relative(process.cwd(), cssDir).replaceAll('\\', '/')}/*.css`]: [

0 commit comments

Comments
 (0)