@@ -17,11 +17,27 @@ const resolveConfigWithPlugin = (
1717 )
1818}
1919
20+ const ENTRY_ID = 'entry.js'
21+ const RESOLVED_ENTRY_ID = `\0${ ENTRY_ID } `
22+ const resolveEntryPlugin : Plugin = {
23+ name : 'resolve-entry.js' ,
24+ resolveId ( id ) {
25+ if ( id === ENTRY_ID ) {
26+ return RESOLVED_ENTRY_ID
27+ }
28+ } ,
29+ load ( id ) {
30+ if ( id === RESOLVED_ENTRY_ID ) {
31+ return 'export default {}'
32+ }
33+ } ,
34+ }
35+
2036const createServerWithPlugin = async ( plugin : Plugin ) => {
2137 const server = await createServer ( {
2238 configFile : false ,
2339 root : import . meta. dirname ,
24- plugins : [ plugin ] ,
40+ plugins : [ plugin , resolveEntryPlugin ] ,
2541 logLevel : 'error' ,
2642 server : {
2743 middlewareMode : true ,
@@ -62,28 +78,13 @@ const buildWithPlugin = async (plugin: Plugin) => {
6278 build : {
6379 write : false ,
6480 } ,
65- plugins : [
66- {
67- name : 'resolve-entry.js' ,
68- resolveId ( id ) {
69- if ( id === 'entry.js' ) {
70- return '\0' + id
71- }
72- } ,
73- load ( id ) {
74- if ( id === '\0entry.js' ) {
75- return 'export default {}'
76- }
77- } ,
78- } ,
79- plugin ,
80- ] ,
81+ plugins : [ plugin , resolveEntryPlugin ] ,
8182 } )
8283}
8384
8485describe ( 'supports plugin context' , ( ) => {
8586 test ( 'config hook' , async ( ) => {
86- expect . assertions ( 3 )
87+ expect . assertions ( 4 )
8788
8889 await resolveConfigWithPlugin ( {
8990 name : 'test' ,
@@ -96,14 +97,15 @@ describe('supports plugin context', () => {
9697 meta : expect . any ( Object ) ,
9798 } )
9899 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
100+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
99101 // @ts -expect-error watchMode should not exist in types
100102 expect ( this . meta . watchMode ) . toBeUndefined ( )
101103 } ,
102104 } )
103105 } )
104106
105107 test ( 'configEnvironment hook' , async ( ) => {
106- expect . assertions ( 3 )
108+ expect . assertions ( 4 )
107109
108110 await resolveConfigWithPlugin ( {
109111 name : 'test' ,
@@ -118,14 +120,15 @@ describe('supports plugin context', () => {
118120 meta : expect . any ( Object ) ,
119121 } )
120122 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
123+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
121124 // @ts -expect-error watchMode should not exist in types
122125 expect ( this . meta . watchMode ) . toBeUndefined ( )
123126 } ,
124127 } )
125128 } )
126129
127130 test ( 'configResolved hook' , async ( ) => {
128- expect . assertions ( 3 )
131+ expect . assertions ( 4 )
129132
130133 await resolveConfigWithPlugin ( {
131134 name : 'test' ,
@@ -138,13 +141,14 @@ describe('supports plugin context', () => {
138141 meta : expect . any ( Object ) ,
139142 } )
140143 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
144+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
141145 expect ( this . meta . watchMode ) . toBe ( true )
142146 } ,
143147 } )
144148 } )
145149
146150 test ( 'configureServer hook' , async ( ) => {
147- expect . assertions ( 3 )
151+ expect . assertions ( 4 )
148152
149153 await createServerWithPlugin ( {
150154 name : 'test' ,
@@ -157,13 +161,14 @@ describe('supports plugin context', () => {
157161 meta : expect . any ( Object ) ,
158162 } )
159163 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
164+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
160165 expect ( this . meta . watchMode ) . toBe ( true )
161166 } ,
162167 } )
163168 } )
164169
165170 test ( 'configurePreviewServer hook' , async ( ) => {
166- expect . assertions ( 3 )
171+ expect . assertions ( 4 )
167172
168173 await createPreviewServerWithPlugin ( {
169174 name : 'test' ,
@@ -176,13 +181,14 @@ describe('supports plugin context', () => {
176181 meta : expect . any ( Object ) ,
177182 } )
178183 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
184+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
179185 expect ( this . meta . watchMode ) . toBe ( false )
180186 } ,
181187 } )
182188 } )
183189
184190 test ( 'transformIndexHtml hook in dev' , async ( ) => {
185- expect . assertions ( 3 )
191+ expect . assertions ( 4 )
186192
187193 const server = await createServerWithPlugin ( {
188194 name : 'test' ,
@@ -195,14 +201,15 @@ describe('supports plugin context', () => {
195201 meta : expect . any ( Object ) ,
196202 } )
197203 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
204+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
198205 expect ( this . meta . watchMode ) . toBe ( true )
199206 } ,
200207 } )
201208 await server . transformIndexHtml ( '/index.html' , '<html></html>' )
202209 } )
203210
204211 test ( 'transformIndexHtml hook in build' , async ( ) => {
205- expect . assertions ( 3 )
212+ expect . assertions ( 4 )
206213
207214 await buildWithPlugin ( {
208215 name : 'test' ,
@@ -215,13 +222,14 @@ describe('supports plugin context', () => {
215222 meta : expect . any ( Object ) ,
216223 } )
217224 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
225+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
218226 expect ( this . meta . watchMode ) . toBe ( false )
219227 } ,
220228 } )
221229 } )
222230
223231 test ( 'handleHotUpdate hook' , async ( ) => {
224- expect . assertions ( 3 )
232+ expect . assertions ( 4 )
225233
226234 const { promise, resolve } = promiseWithResolvers < void > ( )
227235 const server = await createServerWithPlugin ( {
@@ -235,6 +243,7 @@ describe('supports plugin context', () => {
235243 meta : expect . any ( Object ) ,
236244 } )
237245 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
246+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
238247 expect ( this . meta . watchMode ) . toBe ( true )
239248 resolve ( )
240249 } ,
@@ -248,7 +257,7 @@ describe('supports plugin context', () => {
248257 } )
249258
250259 test ( 'hotUpdate hook' , async ( ) => {
251- expect . assertions ( 3 )
260+ expect . assertions ( 4 )
252261
253262 const { promise, resolve } = promiseWithResolvers < void > ( )
254263 const server = await createServerWithPlugin ( {
@@ -265,6 +274,7 @@ describe('supports plugin context', () => {
265274 environment : expect . any ( Object ) ,
266275 } )
267276 expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
277+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
268278 expect ( this . meta . watchMode ) . toBe ( true )
269279 resolve ( )
270280 } ,
@@ -276,4 +286,48 @@ describe('supports plugin context', () => {
276286
277287 await promise
278288 } )
289+
290+ test ( 'transform hook in dev' , async ( ) => {
291+ expect . assertions ( 4 )
292+
293+ const server = await createServerWithPlugin ( {
294+ name : 'test' ,
295+ transform ( _code , id ) {
296+ if ( id !== RESOLVED_ENTRY_ID ) return
297+ expect ( this ) . toMatchObject ( {
298+ debug : expect . any ( Function ) ,
299+ info : expect . any ( Function ) ,
300+ warn : expect . any ( Function ) ,
301+ error : expect . any ( Function ) ,
302+ meta : expect . any ( Object ) ,
303+ } )
304+ expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
305+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
306+ expect ( this . meta . watchMode ) . toBe ( true )
307+ } ,
308+ } )
309+ await server . transformRequest ( ENTRY_ID )
310+ await server . close ( )
311+ } )
312+
313+ test ( 'transform hook in build' , async ( ) => {
314+ expect . assertions ( 4 )
315+
316+ await buildWithPlugin ( {
317+ name : 'test' ,
318+ transform ( _code , id ) {
319+ if ( id !== RESOLVED_ENTRY_ID ) return
320+ expect ( this ) . toMatchObject ( {
321+ debug : expect . any ( Function ) ,
322+ info : expect . any ( Function ) ,
323+ warn : expect . any ( Function ) ,
324+ error : expect . any ( Function ) ,
325+ meta : expect . any ( Object ) ,
326+ } )
327+ expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
328+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
329+ expect ( this . meta . watchMode ) . toBe ( false )
330+ } ,
331+ } )
332+ } )
279333} )
0 commit comments