Skip to content

Commit 04a1091

Browse files
committed
Fix vite inline style issue
1 parent 95e4fd6 commit 04a1091

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

.changeset/plenty-books-begin.md

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

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('devupUIPlugin', () => {
3939
watchChange: expect.any(Function),
4040
enforce: 'pre',
4141
transform: expect.any(Function),
42+
apply: expect.any(Function),
4243
})
4344
expect(existsSync).toHaveBeenCalledWith(devupPath)
4445
expect(getThemeInterface).toHaveBeenCalledWith(
@@ -107,6 +108,14 @@ describe('devupUIPlugin', () => {
107108
} as any)
108109
;(plugin as any).transform('code', 'correct.ts')
109110
expect(writeFileSync).toBeCalledTimes(0)
111+
;(plugin as any).apply({}, { command: 'serve' })
112+
vi.clearAllMocks()
113+
vi.mocked(codeExtract).mockReturnValueOnce({
114+
css: 'css code',
115+
code: 'code',
116+
} as any)
117+
;(plugin as any).transform('code', 'correct.ts')
118+
expect(writeFileSync).toBeCalledTimes(1)
110119
})
111120
it('should not extract code', () => {
112121
const devupPath = 'devup.json'
@@ -130,6 +139,7 @@ describe('devupUIPlugin', () => {
130139
watchChange: expect.any(Function),
131140
enforce: 'pre',
132141
transform: expect.any(Function),
142+
apply: expect.any(Function),
133143
})
134144
expect(existsSync).toHaveBeenCalledWith(devupPath)
135145
expect(getThemeInterface).toHaveBeenCalledWith(
@@ -168,6 +178,41 @@ describe('devupUIPlugin', () => {
168178
watchChange: expect.any(Function),
169179
enforce: 'pre',
170180
transform: expect.any(Function),
181+
apply: expect.any(Function),
182+
})
183+
expect(existsSync).toHaveBeenCalledWith(devupPath)
184+
expect(getThemeInterface).toHaveBeenCalledWith(
185+
libPackage,
186+
'DevupThemeColors',
187+
'DevupThemeTypography',
188+
'DevupTheme',
189+
)
190+
expect(readFileSync).toHaveBeenCalledWith(devupPath, 'utf-8')
191+
expect(existsSync).toHaveBeenCalledWith(interfacePath)
192+
})
193+
194+
it('should return true on apply', () => {
195+
const devupPath = 'devup.json'
196+
const interfacePath = '.df'
197+
const cssFile = join(_dirname, 'devup-ui.css')
198+
const libPackage = '@devup-ui/react'
199+
vi.mocked(existsSync).mockReturnValueOnce(true).mockReturnValueOnce(false)
200+
vi.mocked(getThemeInterface).mockReturnValue('interface code')
201+
vi.mocked(readFileSync).mockReturnValueOnce('{"theme": {}}')
202+
const options = {
203+
package: libPackage,
204+
cssFile,
205+
devupPath,
206+
interfacePath,
207+
}
208+
const plugin = DevupUI(options)
209+
expect(plugin).toEqual({
210+
name: 'devup-ui',
211+
config: expect.any(Function),
212+
watchChange: expect.any(Function),
213+
enforce: 'pre',
214+
transform: expect.any(Function),
215+
apply: expect.any(Function),
171216
})
172217
expect(existsSync).toHaveBeenCalledWith(devupPath)
173218
expect(getThemeInterface).toHaveBeenCalledWith(
@@ -178,5 +223,6 @@ describe('devupUIPlugin', () => {
178223
)
179224
expect(readFileSync).toHaveBeenCalledWith(devupPath, 'utf-8')
180225
expect(existsSync).toHaveBeenCalledWith(interfacePath)
226+
expect((plugin as any).apply({}, { command: 'build' })).toBe(true)
181227
})
182228
})

packages/vite-plugin/src/plugin.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function DevupUI({
5959
console.error(error)
6060
}
6161
}
62+
let command: null | 'build' | 'serve' = null
6263
return {
6364
name: 'devup-ui',
6465
config() {
@@ -70,6 +71,10 @@ export function DevupUI({
7071
},
7172
}
7273
},
74+
apply(_, env) {
75+
command = env.command
76+
return true
77+
},
7378
watchChange(id) {
7479
if (resolve(id) !== resolve(devupPath)) return
7580
if (existsSync(devupPath)) {
@@ -98,8 +103,9 @@ export function DevupUI({
98103
writeFileSync(cssFile, css, {
99104
encoding: 'utf-8',
100105
})
101-
return {
102-
code: `${retCode}
106+
if (command === 'serve')
107+
return {
108+
code: `${retCode}
103109
const exists = !!document.getElementById('devup-ui');
104110
const style = document.getElementById('devup-ui') || document.createElement('style');
105111
style.id = 'devup-ui';
@@ -108,7 +114,7 @@ export function DevupUI({
108114
\`;
109115
if (!exists) document.head.appendChild(style);
110116
`,
111-
}
117+
}
112118
}
113119
return {
114120
code: retCode,

0 commit comments

Comments
 (0)