Skip to content

Commit 5741737

Browse files
committed
Implement test code
1 parent a8a110a commit 5741737

File tree

4 files changed

+137
-49
lines changed

4 files changed

+137
-49
lines changed

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

Lines changed: 121 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { join, resolve } from 'node:path'
44
import { DevupUIWebpackPlugin } from '@devup-ui/webpack-plugin'
55

66
import { DevupUI } from '../plugin'
7+
import { preload } from '../preload'
78

89
vi.mock('@devup-ui/webpack-plugin')
910
vi.mock('node:fs')
11+
vi.mock('../preload')
1012

1113
describe('DevupUINextPlugin', () => {
1214
describe('webpack', () => {
@@ -85,20 +87,42 @@ describe('DevupUINextPlugin', () => {
8587
loader: '@devup-ui/webpack-plugin/css-loader',
8688
},
8789
],
88-
'*.{tsx,ts,js,mjs}': [
89-
{
90-
loader: '@devup-ui/webpack-plugin/loader',
91-
options: {
92-
package: '@devup-ui/react',
93-
cssDir: resolve('df', 'devup-ui'),
94-
sheetFile: join('df', 'sheet.json'),
95-
classMapFile: join('df', 'classMap.json'),
96-
fileMapFile: join('df', 'fileMap.json'),
97-
watch: false,
98-
singleCss: false,
90+
'*.{tsx,ts,js,mjs}': {
91+
loaders: [
92+
{
93+
loader: '@devup-ui/webpack-plugin/loader',
94+
options: {
95+
package: '@devup-ui/react',
96+
cssDir: resolve('df', 'devup-ui'),
97+
sheetFile: join('df', 'sheet.json'),
98+
classMapFile: join('df', 'classMap.json'),
99+
fileMapFile: join('df', 'fileMap.json'),
100+
watch: false,
101+
singleCss: false,
102+
theme: {},
103+
defaultClassMap: {},
104+
defaultFileMap: {},
105+
defaultSheet: {
106+
css: {},
107+
font_faces: {},
108+
global_css_files: [],
109+
imports: {},
110+
keyframes: {},
111+
properties: {},
112+
},
113+
},
114+
},
115+
],
116+
condition: {
117+
not: {
118+
path: new RegExp(
119+
`node_modules(?!.*(${['@devup-ui']
120+
.join('|')
121+
.replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))`,
122+
),
99123
},
100124
},
101-
],
125+
},
102126
},
103127
},
104128
})
@@ -118,20 +142,42 @@ describe('DevupUINextPlugin', () => {
118142
loader: '@devup-ui/webpack-plugin/css-loader',
119143
},
120144
],
121-
'*.{tsx,ts,js,mjs}': [
122-
{
123-
loader: '@devup-ui/webpack-plugin/loader',
124-
options: {
125-
package: '@devup-ui/react',
126-
cssDir: resolve('df', 'devup-ui'),
127-
sheetFile: join('df', 'sheet.json'),
128-
classMapFile: join('df', 'classMap.json'),
129-
fileMapFile: join('df', 'fileMap.json'),
130-
watch: false,
131-
singleCss: false,
145+
'*.{tsx,ts,js,mjs}': {
146+
condition: {
147+
not: {
148+
path: new RegExp(
149+
`node_modules(?!.*(${['@devup-ui']
150+
.join('|')
151+
.replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))`,
152+
),
132153
},
133154
},
134-
],
155+
loaders: [
156+
{
157+
loader: '@devup-ui/webpack-plugin/loader',
158+
options: {
159+
package: '@devup-ui/react',
160+
cssDir: resolve('df', 'devup-ui'),
161+
sheetFile: join('df', 'sheet.json'),
162+
classMapFile: join('df', 'classMap.json'),
163+
fileMapFile: join('df', 'fileMap.json'),
164+
watch: false,
165+
singleCss: false,
166+
theme: {},
167+
defaultClassMap: {},
168+
defaultFileMap: {},
169+
defaultSheet: {
170+
css: {},
171+
font_faces: {},
172+
global_css_files: [],
173+
imports: {},
174+
keyframes: {},
175+
properties: {},
176+
},
177+
},
178+
},
179+
],
180+
},
135181
},
136182
},
137183
})
@@ -158,21 +204,42 @@ describe('DevupUINextPlugin', () => {
158204
loader: '@devup-ui/webpack-plugin/css-loader',
159205
},
160206
],
161-
'*.{tsx,ts,js,mjs}': [
162-
{
163-
loader: '@devup-ui/webpack-plugin/loader',
164-
options: {
165-
package: '@devup-ui/react',
166-
cssDir: resolve('df', 'devup-ui'),
167-
sheetFile: join('df', 'sheet.json'),
168-
classMapFile: join('df', 'classMap.json'),
169-
fileMapFile: join('df', 'fileMap.json'),
170-
watch: false,
171-
singleCss: false,
172-
theme: 'theme',
207+
'*.{tsx,ts,js,mjs}': {
208+
condition: {
209+
not: {
210+
path: new RegExp(
211+
`node_modules(?!.*(${['@devup-ui']
212+
.join('|')
213+
.replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))`,
214+
),
173215
},
174216
},
175-
],
217+
loaders: [
218+
{
219+
loader: '@devup-ui/webpack-plugin/loader',
220+
options: {
221+
package: '@devup-ui/react',
222+
cssDir: resolve('df', 'devup-ui'),
223+
sheetFile: join('df', 'sheet.json'),
224+
classMapFile: join('df', 'classMap.json'),
225+
fileMapFile: join('df', 'fileMap.json'),
226+
watch: false,
227+
singleCss: false,
228+
theme: 'theme',
229+
defaultClassMap: {},
230+
defaultFileMap: {},
231+
defaultSheet: {
232+
css: {},
233+
font_faces: {},
234+
global_css_files: [],
235+
imports: {},
236+
keyframes: {},
237+
properties: {},
238+
},
239+
},
240+
},
241+
],
242+
},
176243
},
177244
},
178245
})
@@ -184,9 +251,23 @@ describe('DevupUINextPlugin', () => {
184251
it('should throw error if NODE_ENV is production', () => {
185252
vi.stubEnv('NODE_ENV', 'production')
186253
vi.stubEnv('TURBOPACK', '1')
187-
const ret = () => DevupUI({})
188-
expect(ret).toThrow(
189-
'Devup UI is not supported in production with turbopack',
254+
vi.mocked(preload).mockReturnValue()
255+
const ret = DevupUI({})
256+
expect(ret).toEqual({
257+
turbopack: {
258+
rules: expect.any(Object),
259+
},
260+
})
261+
expect(preload).toHaveBeenCalledWith(
262+
new RegExp(
263+
`node_modules(?!.*(${['@devup-ui']
264+
.join('|')
265+
.replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))`,
266+
),
267+
'@devup-ui/react',
268+
false,
269+
'theme',
270+
expect.any(String),
190271
)
191272
})
192273
})

packages/next-plugin/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ export function DevupUI(
6969
)
7070

7171
if (process.env.NODE_ENV !== 'production') {
72+
// dev
7273
process.env.TURBOPACK_DEBUG_JS = '*'
7374
process.env.NODE_OPTIONS ??= ''
7475
process.env.NODE_OPTIONS += ' --inspect-brk'
7576
} else {
77+
// build
7678
preload(excludeRegex, libPackage, singleCss, theme, cssDir)
7779
}
7880

packages/next-plugin/src/preload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function preload(
99
excludeRegex: RegExp,
1010
libPackage: string,
1111
singleCss: boolean,
12-
theme: object | undefined,
12+
theme: object,
1313
cssDir: string,
1414
) {
1515
const projectRoot = findRoot(process.cwd())
@@ -18,7 +18,7 @@ export function preload(
1818
cwd: projectRoot,
1919
exclude: (filename) => excludeRegex.test(filename),
2020
})
21-
if (theme) registerTheme(theme)
21+
registerTheme(theme)
2222
for (const file of collected) {
2323
const filePath = relative(process.cwd(), join(projectRoot, file))
2424
const { cssFile, css } = codeExtract(

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import {
1010
registerTheme,
1111
} from '@devup-ui/wasm'
1212

13-
import devupUILoader from '../loader'
14-
1513
vi.mock('@devup-ui/wasm')
1614
vi.mock('node:fs/promises')
1715
vi.mock('node:path', async (original: any) => {
@@ -24,6 +22,7 @@ vi.mock('node:path', async (original: any) => {
2422

2523
beforeEach(() => {
2624
vi.resetAllMocks()
25+
vi.resetModules()
2726
Date.now = vi.fn().mockReturnValue(0)
2827
})
2928

@@ -33,6 +32,7 @@ describe('devupUILoader', () => {
3332
updatedBaseStyle: [true, false],
3433
}),
3534
)('should extract code with css', async (options) => {
35+
const { default: devupUILoader } = await import('../loader')
3636
const _compiler = {
3737
__DEVUP_CACHE: '',
3838
}
@@ -102,7 +102,8 @@ describe('devupUILoader', () => {
102102
})
103103
})
104104

105-
it('should extract code without css', () => {
105+
it('should extract code without css', async () => {
106+
const { default: devupUILoader } = await import('../loader')
106107
const t = {
107108
getOptions: () => ({
108109
package: 'package',
@@ -141,7 +142,8 @@ describe('devupUILoader', () => {
141142
})
142143
})
143144

144-
it('should handle error', () => {
145+
it('should handle error', async () => {
146+
const { default: devupUILoader } = await import('../loader')
145147
const t = {
146148
getOptions: () => ({
147149
package: 'package',
@@ -162,7 +164,8 @@ describe('devupUILoader', () => {
162164
expect(t.async()).toHaveBeenCalledWith(new Error('error'))
163165
})
164166

165-
it('should load with date now on watch', () => {
167+
it('should load with date now on watch', async () => {
168+
const { default: devupUILoader } = await import('../loader')
166169
const t = {
167170
getOptions: () => ({
168171
package: 'package',
@@ -197,7 +200,8 @@ describe('devupUILoader', () => {
197200
)
198201
})
199202

200-
it('should load with nowatch', () => {
203+
it('should load with nowatch', async () => {
204+
const { default: devupUILoader } = await import('../loader')
201205
const t = {
202206
getOptions: () => ({
203207
package: 'package',
@@ -221,7 +225,8 @@ describe('devupUILoader', () => {
221225
vi.mocked(relative).mockReturnValue('./foo/index.tsx')
222226
devupUILoader.bind(t as any)(Buffer.from('code'), '/foo/index.tsx')
223227
})
224-
it('should load with theme', () => {
228+
it('should load with theme', async () => {
229+
const { default: devupUILoader } = await import('../loader')
225230
const t = {
226231
getOptions: () => ({
227232
package: 'package',

0 commit comments

Comments
 (0)