11import { existsSync , readFileSync , writeFileSync } from 'node:fs'
2+ import { writeFile } from 'node:fs/promises'
23import { dirname , join , resolve } from 'node:path'
34import { fileURLToPath } from 'node:url'
45
56import { codeExtract , getThemeInterface } from '@devup-ui/wasm'
6- import { expect } from 'vitest'
7+ import { describe } from 'vitest'
78
89import { DevupUI } from '../plugin'
910
1011vi . mock ( '@devup-ui/wasm' )
1112vi . mock ( 'node:fs' )
13+ vi . mock ( 'node:fs/promises' )
1214
1315const _filename = fileURLToPath ( import . meta. url )
1416const _dirname = resolve ( dirname ( _filename ) , '..' )
@@ -23,7 +25,7 @@ describe('devupUIPlugin', () => {
2325 const interfacePath = '.df'
2426 const cssFile = join ( _dirname , 'devup-ui.css' )
2527 const libPackage = '@devup-ui/react'
26- vi . mocked ( existsSync ) . mockReturnValueOnce ( true ) . mockReturnValueOnce ( false )
28+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
2729 vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
2830 vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
2931 const options = {
@@ -35,6 +37,8 @@ describe('devupUIPlugin', () => {
3537 const plugin = DevupUI ( options )
3638 expect ( plugin ) . toEqual ( {
3739 name : 'devup-ui' ,
40+ load : expect . any ( Function ) ,
41+ resolveId : expect . any ( Function ) ,
3842 config : expect . any ( Function ) ,
3943 watchChange : expect . any ( Function ) ,
4044 enforce : 'pre' ,
@@ -56,6 +60,13 @@ describe('devupUIPlugin', () => {
5660 ignored : [ `!${ devupPath } ` ] ,
5761 } ,
5862 } ,
63+ build : {
64+ rollupOptions : {
65+ output : {
66+ manualChunks : expect . any ( Function ) ,
67+ } ,
68+ } ,
69+ } ,
5970 } )
6071 vi . clearAllMocks ( )
6172 vi . mocked ( existsSync ) . mockReturnValue ( true )
@@ -99,30 +110,30 @@ describe('devupUIPlugin', () => {
99110 code : 'code' ,
100111 } as any )
101112 ; ( plugin as any ) . transform ( 'code' , 'correct.ts' )
102- expect ( writeFileSync ) . toBeCalledTimes ( 1 )
113+ expect ( writeFile ) . toBeCalledTimes ( 1 )
103114
104115 vi . clearAllMocks ( )
105116 vi . mocked ( codeExtract ) . mockReturnValueOnce ( {
106117 css : undefined ,
107118 code : 'code' ,
108119 } as any )
109120 ; ( plugin as any ) . transform ( 'code' , 'correct.ts' )
110- expect ( writeFileSync ) . toBeCalledTimes ( 0 )
121+ expect ( writeFile ) . toBeCalledTimes ( 0 )
111122 ; ( plugin as any ) . apply ( { } , { command : 'serve' } )
112123 vi . clearAllMocks ( )
113124 vi . mocked ( codeExtract ) . mockReturnValueOnce ( {
114- css : 'css code' ,
125+ css : 'css code next ' ,
115126 code : 'code' ,
116127 } as any )
117128 ; ( plugin as any ) . transform ( 'code' , 'correct.ts' )
118- expect ( writeFileSync ) . toBeCalledTimes ( 1 )
129+ expect ( writeFile ) . toBeCalledTimes ( 1 )
119130 } )
120131 it ( 'should transform code' , ( ) => {
121132 const devupPath = 'devup.json'
122133 const interfacePath = '.df'
123134 const cssFile = join ( _dirname , 'devup-ui.css' )
124135 const libPackage = '@devup-ui/react'
125- vi . mocked ( existsSync ) . mockReturnValueOnce ( true ) . mockReturnValueOnce ( false )
136+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
126137 vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
127138 vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
128139 const options = {
@@ -134,6 +145,8 @@ describe('devupUIPlugin', () => {
134145 const plugin = DevupUI ( options )
135146 expect ( plugin ) . toEqual ( {
136147 name : 'devup-ui' ,
148+ load : expect . any ( Function ) ,
149+ resolveId : expect . any ( Function ) ,
137150 config : expect . any ( Function ) ,
138151 watchChange : expect . any ( Function ) ,
139152 enforce : 'pre' ,
@@ -151,25 +164,23 @@ describe('devupUIPlugin', () => {
151164 expect ( existsSync ) . toHaveBeenCalledWith ( interfacePath )
152165 vi . clearAllMocks ( )
153166 vi . mocked ( codeExtract ) . mockReturnValueOnce ( {
154- css : 'css code' ,
167+ css : 'css code 1223444 ' ,
155168 code : 'code' ,
156169 } as any )
157170 // eslint-disable-next-line prefer-spread
158171 ; ( plugin as any ) . apply ( null , {
159172 command : 'serve' ,
160173 } )
161174 vi . stubEnv ( 'NODE_ENV' , 'development' )
162- expect ( ( plugin as any ) . transform ( 'code' , 'correct.ts' ) . code ) . toContain (
163- 'document.head.appendChild' ,
164- )
165- expect ( writeFileSync ) . toBeCalledTimes ( 1 )
175+ ; ( plugin as any ) . transform ( 'code' , 'correct.ts' )
176+ expect ( writeFile ) . toBeCalledTimes ( 1 )
166177 } )
167178 it ( 'should not extract code' , ( ) => {
168179 const devupPath = 'devup.json'
169180 const interfacePath = '.df'
170181 const cssFile = join ( _dirname , 'devup-ui.css' )
171182 const libPackage = '@devup-ui/react'
172- vi . mocked ( existsSync ) . mockReturnValueOnce ( true ) . mockReturnValueOnce ( false )
183+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
173184 vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
174185 vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
175186 const options = {
@@ -182,6 +193,8 @@ describe('devupUIPlugin', () => {
182193 const plugin = DevupUI ( options )
183194 expect ( plugin ) . toEqual ( {
184195 name : 'devup-ui' ,
196+ load : expect . any ( Function ) ,
197+ resolveId : expect . any ( Function ) ,
185198 config : expect . any ( Function ) ,
186199 watchChange : expect . any ( Function ) ,
187200 enforce : 'pre' ,
@@ -206,7 +219,7 @@ describe('devupUIPlugin', () => {
206219 const interfacePath = '.df'
207220 const cssFile = join ( _dirname , 'devup-ui.css' )
208221 const libPackage = '@devup-ui/react'
209- vi . mocked ( existsSync ) . mockReturnValueOnce ( true ) . mockReturnValueOnce ( false )
222+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
210223 vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
211224 vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
212225 vi . mocked ( writeFileSync ) . mockImplementation ( ( ) => {
@@ -221,6 +234,8 @@ describe('devupUIPlugin', () => {
221234 const plugin = DevupUI ( options )
222235 expect ( plugin ) . toEqual ( {
223236 name : 'devup-ui' ,
237+ load : expect . any ( Function ) ,
238+ resolveId : expect . any ( Function ) ,
224239 config : expect . any ( Function ) ,
225240 watchChange : expect . any ( Function ) ,
226241 enforce : 'pre' ,
@@ -243,7 +258,7 @@ describe('devupUIPlugin', () => {
243258 const interfacePath = '.df'
244259 const cssFile = join ( _dirname , 'devup-ui.css' )
245260 const libPackage = '@devup-ui/react'
246- vi . mocked ( existsSync ) . mockReturnValueOnce ( true ) . mockReturnValueOnce ( false )
261+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
247262 vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
248263 vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
249264 const options = {
@@ -256,6 +271,8 @@ describe('devupUIPlugin', () => {
256271 expect ( plugin ) . toEqual ( {
257272 name : 'devup-ui' ,
258273 config : expect . any ( Function ) ,
274+ load : expect . any ( Function ) ,
275+ resolveId : expect . any ( Function ) ,
259276 watchChange : expect . any ( Function ) ,
260277 enforce : 'pre' ,
261278 transform : expect . any ( Function ) ,
@@ -272,4 +289,58 @@ describe('devupUIPlugin', () => {
272289 expect ( existsSync ) . toHaveBeenCalledWith ( interfacePath )
273290 expect ( ( plugin as any ) . apply ( { } , { command : 'build' } ) ) . toBe ( true )
274291 } )
292+
293+ describe ( 'basic' , ( ) => {
294+ const devupPath = 'devup.json'
295+ const interfacePath = '.df'
296+ const cssFile = join ( _dirname , 'devup-ui.css' )
297+ const libPackage = '@devup-ui/react'
298+ vi . mocked ( existsSync ) . mockReturnValueOnce ( false ) . mockReturnValueOnce ( true )
299+ vi . mocked ( getThemeInterface ) . mockReturnValue ( 'interface code' )
300+ vi . mocked ( readFileSync ) . mockReturnValueOnce ( '{"theme": {}}' )
301+ const options = {
302+ package : libPackage ,
303+ cssFile,
304+ devupPath,
305+ interfacePath,
306+ }
307+ const plugin = DevupUI ( options )
308+ it ( 'should merge chunks' , ( ) => {
309+ expect (
310+ ( plugin as any )
311+ . config ( )
312+ . build . rollupOptions . output . manualChunks ( 'code' , 'code' ) ,
313+ ) . toBeUndefined ( )
314+
315+ expect (
316+ ( plugin as any )
317+ . config ( )
318+ . build . rollupOptions . output . manualChunks ( 'devup-ui.css' , 'code' ) ,
319+ ) . toEqual ( 'devup-ui.css' )
320+ expect (
321+ ( plugin as any )
322+ . config ( )
323+ . build . rollupOptions . output . manualChunks ( 'devup-ui.css?v=1' , 'code' ) ,
324+ ) . toEqual ( 'devup-ui.css' )
325+ } )
326+ it ( 'should resolveId' , ( ) => {
327+ expect ( ( plugin as any ) . resolveId ( 'code' , 'code' ) ) . toBeUndefined ( )
328+ expect (
329+ ( plugin as any )
330+ . resolveId ( cssFile , 'code' )
331+ . startsWith ( 'devup-ui.css?v=' ) ,
332+ ) . toBe ( true )
333+ } )
334+ it ( 'should load' , ( ) => {
335+ expect ( ( plugin as any ) . load ( 'code' ) ) . toBeUndefined ( )
336+ expect ( ( plugin as any ) . load ( cssFile ) ) . toBeUndefined ( )
337+ expect (
338+ ( plugin as any ) . load ( 'devup-ui.css?v=some' ) . length . toString ( ) ,
339+ ) . toBe (
340+ ( plugin as any )
341+ . resolveId ( cssFile , 'code' )
342+ . substring ( 'devup-ui.css?v=' . length ) ,
343+ )
344+ } )
345+ } )
275346} )
0 commit comments