@@ -17,11 +17,27 @@ const resolveConfigWithPlugin = (
17
17
)
18
18
}
19
19
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
+
20
36
const createServerWithPlugin = async ( plugin : Plugin ) => {
21
37
const server = await createServer ( {
22
38
configFile : false ,
23
39
root : import . meta. dirname ,
24
- plugins : [ plugin ] ,
40
+ plugins : [ plugin , resolveEntryPlugin ] ,
25
41
logLevel : 'error' ,
26
42
server : {
27
43
middlewareMode : true ,
@@ -62,28 +78,13 @@ const buildWithPlugin = async (plugin: Plugin) => {
62
78
build : {
63
79
write : false ,
64
80
} ,
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 ] ,
81
82
} )
82
83
}
83
84
84
85
describe ( 'supports plugin context' , ( ) => {
85
86
test ( 'config hook' , async ( ) => {
86
- expect . assertions ( 3 )
87
+ expect . assertions ( 4 )
87
88
88
89
await resolveConfigWithPlugin ( {
89
90
name : 'test' ,
@@ -96,14 +97,15 @@ describe('supports plugin context', () => {
96
97
meta : expect . any ( Object ) ,
97
98
} )
98
99
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
100
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
99
101
// @ts -expect-error watchMode should not exist in types
100
102
expect ( this . meta . watchMode ) . toBeUndefined ( )
101
103
} ,
102
104
} )
103
105
} )
104
106
105
107
test ( 'configEnvironment hook' , async ( ) => {
106
- expect . assertions ( 3 )
108
+ expect . assertions ( 4 )
107
109
108
110
await resolveConfigWithPlugin ( {
109
111
name : 'test' ,
@@ -118,14 +120,15 @@ describe('supports plugin context', () => {
118
120
meta : expect . any ( Object ) ,
119
121
} )
120
122
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
123
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
121
124
// @ts -expect-error watchMode should not exist in types
122
125
expect ( this . meta . watchMode ) . toBeUndefined ( )
123
126
} ,
124
127
} )
125
128
} )
126
129
127
130
test ( 'configResolved hook' , async ( ) => {
128
- expect . assertions ( 3 )
131
+ expect . assertions ( 4 )
129
132
130
133
await resolveConfigWithPlugin ( {
131
134
name : 'test' ,
@@ -138,13 +141,14 @@ describe('supports plugin context', () => {
138
141
meta : expect . any ( Object ) ,
139
142
} )
140
143
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
144
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
141
145
expect ( this . meta . watchMode ) . toBe ( true )
142
146
} ,
143
147
} )
144
148
} )
145
149
146
150
test ( 'configureServer hook' , async ( ) => {
147
- expect . assertions ( 3 )
151
+ expect . assertions ( 4 )
148
152
149
153
await createServerWithPlugin ( {
150
154
name : 'test' ,
@@ -157,13 +161,14 @@ describe('supports plugin context', () => {
157
161
meta : expect . any ( Object ) ,
158
162
} )
159
163
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
164
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
160
165
expect ( this . meta . watchMode ) . toBe ( true )
161
166
} ,
162
167
} )
163
168
} )
164
169
165
170
test ( 'configurePreviewServer hook' , async ( ) => {
166
- expect . assertions ( 3 )
171
+ expect . assertions ( 4 )
167
172
168
173
await createPreviewServerWithPlugin ( {
169
174
name : 'test' ,
@@ -176,13 +181,14 @@ describe('supports plugin context', () => {
176
181
meta : expect . any ( Object ) ,
177
182
} )
178
183
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
184
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
179
185
expect ( this . meta . watchMode ) . toBe ( false )
180
186
} ,
181
187
} )
182
188
} )
183
189
184
190
test ( 'transformIndexHtml hook in dev' , async ( ) => {
185
- expect . assertions ( 3 )
191
+ expect . assertions ( 4 )
186
192
187
193
const server = await createServerWithPlugin ( {
188
194
name : 'test' ,
@@ -195,14 +201,15 @@ describe('supports plugin context', () => {
195
201
meta : expect . any ( Object ) ,
196
202
} )
197
203
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
204
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
198
205
expect ( this . meta . watchMode ) . toBe ( true )
199
206
} ,
200
207
} )
201
208
await server . transformIndexHtml ( '/index.html' , '<html></html>' )
202
209
} )
203
210
204
211
test ( 'transformIndexHtml hook in build' , async ( ) => {
205
- expect . assertions ( 3 )
212
+ expect . assertions ( 4 )
206
213
207
214
await buildWithPlugin ( {
208
215
name : 'test' ,
@@ -215,13 +222,14 @@ describe('supports plugin context', () => {
215
222
meta : expect . any ( Object ) ,
216
223
} )
217
224
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
225
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
218
226
expect ( this . meta . watchMode ) . toBe ( false )
219
227
} ,
220
228
} )
221
229
} )
222
230
223
231
test ( 'handleHotUpdate hook' , async ( ) => {
224
- expect . assertions ( 3 )
232
+ expect . assertions ( 4 )
225
233
226
234
const { promise, resolve } = promiseWithResolvers < void > ( )
227
235
const server = await createServerWithPlugin ( {
@@ -235,6 +243,7 @@ describe('supports plugin context', () => {
235
243
meta : expect . any ( Object ) ,
236
244
} )
237
245
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
246
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
238
247
expect ( this . meta . watchMode ) . toBe ( true )
239
248
resolve ( )
240
249
} ,
@@ -248,7 +257,7 @@ describe('supports plugin context', () => {
248
257
} )
249
258
250
259
test ( 'hotUpdate hook' , async ( ) => {
251
- expect . assertions ( 3 )
260
+ expect . assertions ( 4 )
252
261
253
262
const { promise, resolve } = promiseWithResolvers < void > ( )
254
263
const server = await createServerWithPlugin ( {
@@ -265,6 +274,7 @@ describe('supports plugin context', () => {
265
274
environment : expect . any ( Object ) ,
266
275
} )
267
276
expect ( this . meta . rollupVersion ) . toBeTypeOf ( 'string' )
277
+ expect ( this . meta . viteVersion ) . toBeTypeOf ( 'string' )
268
278
expect ( this . meta . watchMode ) . toBe ( true )
269
279
resolve ( )
270
280
} ,
@@ -276,4 +286,48 @@ describe('supports plugin context', () => {
276
286
277
287
await promise
278
288
} )
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
+ } )
279
333
} )
0 commit comments