Skip to content

Commit b9d8a17

Browse files
committed
Support next build cache
1 parent c5336c9 commit b9d8a17

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

.changeset/fine-rabbits-think.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@devup-ui/webpack-plugin": patch
3+
"@devup-ui/next-plugin": patch
4+
---
5+
6+
Support next build cache

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ describe('plugin', () => {
1313
it('should apply webpack plugin', async () => {
1414
const ret = DevupUI({})
1515

16-
ret.webpack!({ plugins: [] }, {} as any)
16+
ret.webpack!({ plugins: [] }, { buildId: 'tmpBuildId' } as any)
1717

18-
expect(DevupUIWebpackPlugin).toHaveBeenCalledWith({})
18+
expect(DevupUIWebpackPlugin).toHaveBeenCalledWith({
19+
cssFile: resolve('.next/cache', 'devup-ui_tmpBuildId.css'),
20+
})
1921
})
2022

2123
it('should apply webpack plugin with config', async () => {
@@ -26,10 +28,11 @@ describe('plugin', () => {
2628
},
2729
)
2830

29-
ret.webpack!({ plugins: [] }, {} as any)
31+
ret.webpack!({ plugins: [] }, { buildId: 'tmpBuildId' } as any)
3032

3133
expect(DevupUIWebpackPlugin).toHaveBeenCalledWith({
3234
package: 'new-package',
35+
cssFile: resolve('.next/cache', 'devup-ui_tmpBuildId.css'),
3336
})
3437
})
3538

@@ -44,10 +47,11 @@ describe('plugin', () => {
4447
},
4548
)
4649

47-
ret.webpack!({ plugins: [] }, {} as any)
50+
ret.webpack!({ plugins: [] }, { buildId: 'tmpBuildId' } as any)
4851

4952
expect(DevupUIWebpackPlugin).toHaveBeenCalledWith({
5053
package: 'new-package',
54+
cssFile: resolve('.next/cache', 'devup-ui_tmpBuildId.css'),
5155
})
5256
expect(webpack).toHaveBeenCalled()
5357
})

packages/next-plugin/src/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export function DevupUI(
6565

6666
const { webpack } = config
6767
config.webpack = (config, _options) => {
68+
options.cssFile ??= resolve(
69+
options.interfacePath ?? '.next/cache',
70+
`devup-ui_${_options.buildId}.css`,
71+
)
6872
config.plugins.push(
6973
new DevupUIWebpackPlugin({
7074
...options,

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ describe('devupUIPlugin', () => {
103103
const plugin = new DevupUIWebpackPlugin({
104104
devupPath: 'custom-devup.json',
105105
})
106+
107+
vi.mocked(getCss).mockReturnValue('css')
106108
const compiler = {
107109
options: {
108110
module: {
@@ -114,6 +116,9 @@ describe('devupUIPlugin', () => {
114116
afterCompile: {
115117
tap: vi.fn(),
116118
},
119+
done: {
120+
tap: vi.fn(),
121+
},
117122
watchRun: {
118123
tapAsync: vi.fn(),
119124
},
@@ -131,6 +136,23 @@ describe('devupUIPlugin', () => {
131136
},
132137
})
133138
expect(add).toHaveBeenCalledWith(resolve('custom-devup.json'))
139+
expect(compiler.hooks.done.tap).toHaveBeenCalled()
140+
141+
vi.mocked(compiler.hooks.done.tap).mock.calls[0][1]({
142+
hasErrors: () => true,
143+
})
144+
expect(writeFileSync).not.toHaveBeenCalled()
145+
146+
vi.mocked(compiler.hooks.done.tap).mock.calls[0][1]({
147+
hasErrors: () => false,
148+
})
149+
expect(writeFileSync).toHaveBeenCalledWith(
150+
resolve('.df', 'devup-ui.css'),
151+
'css',
152+
{
153+
encoding: 'utf-8',
154+
},
155+
)
134156
})
135157

136158
it('should skip writing css file', () => {
@@ -155,6 +177,9 @@ describe('devupUIPlugin', () => {
155177
afterCompile: {
156178
tap: vi.fn(),
157179
},
180+
done: {
181+
tap: vi.fn(),
182+
},
158183
watchRun: {
159184
tapAsync: vi.fn(),
160185
},
@@ -191,6 +216,9 @@ describe('devupUIPlugin', () => {
191216
afterCompile: {
192217
tap: vi.fn(),
193218
},
219+
done: {
220+
tap: vi.fn(),
221+
},
194222
watchRun: {
195223
tapAsync: vi.fn(),
196224
},
@@ -208,6 +236,7 @@ describe('devupUIPlugin', () => {
208236
encoding: 'utf-8',
209237
},
210238
)
239+
expect(compiler.hooks.done.tap).not.toHaveBeenCalled()
211240
})
212241
it('should register devup watch', () => {
213242
const plugin = new DevupUIWebpackPlugin({
@@ -224,6 +253,9 @@ describe('devupUIPlugin', () => {
224253
afterCompile: {
225254
tap: vi.fn(),
226255
},
256+
done: {
257+
tap: vi.fn(),
258+
},
227259
watchRun: {
228260
tapAsync: vi.fn(),
229261
},
@@ -310,6 +342,9 @@ describe('devupUIPlugin', () => {
310342
afterCompile: {
311343
tap: vi.fn(),
312344
},
345+
done: {
346+
tap: vi.fn(),
347+
},
313348
watchRun: {
314349
tapAsync: vi.fn(),
315350
},

packages/webpack-plugin/src/plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createRequire } from 'node:module'
99
import { join, resolve } from 'node:path'
1010

1111
import {
12+
getCss,
1213
getDefaultTheme,
1314
getThemeInterface,
1415
importClassMap,
@@ -138,6 +139,14 @@ export class DevupUIWebpackPlugin {
138139
'process.env.DEVUP_UI_DEFAULT_THEME': JSON.stringify(getDefaultTheme()),
139140
}),
140141
)
142+
if (!this.options.watch) {
143+
compiler.hooks.done.tap('DevupUIWebpackPlugin', (stats) => {
144+
if (!stats.hasErrors()) {
145+
// write css file
146+
writeFileSync(this.options.cssFile, getCss(), { encoding: 'utf-8' })
147+
}
148+
})
149+
}
141150

142151
compiler.options.module.rules.push(
143152
{

0 commit comments

Comments
 (0)