-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
assets.spec.ts
496 lines (429 loc) · 14.7 KB
/
assets.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import path from 'node:path'
import fetch from 'node-fetch'
import { describe, expect, test } from 'vitest'
import {
browserLogs,
editFile,
findAssetFile,
getBg,
getColor,
isBuild,
isServe,
listAssets,
notifyRebuildComplete,
page,
readFile,
readManifest,
untilUpdated,
viteTestUrl,
watcher,
} from '~utils'
const assetMatch = isBuild
? /\/foo\/bar\/assets\/asset-\w{8}\.png/
: '/foo/bar/nested/asset.png'
const iconMatch = `/foo/bar/icon.png`
const fetchPath = (p: string) => {
return fetch(path.posix.join(viteTestUrl, p), {
headers: { Accept: 'text/html,*/*' },
})
}
test('should have no 404s', () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404')
})
})
test('should get a 404 when using incorrect case', async () => {
expect((await fetchPath('icon.png')).headers.get('Content-Type')).toBe(
'image/png',
)
// fallback to index.html
const iconPngResult = await fetchPath('ICON.png')
expect(iconPngResult.headers.get('Content-Type')).toBe(
isBuild ? 'text/html;charset=utf-8' : 'text/html',
)
expect(iconPngResult.status).toBe(200)
expect((await fetchPath('bar')).headers.get('Content-Type')).toBe('')
// fallback to index.html
const barResult = await fetchPath('BAR')
expect(barResult.headers.get('Content-Type')).toContain(
isBuild ? 'text/html;charset=utf-8' : 'text/html',
)
expect(barResult.status).toBe(200)
})
test('should fallback to index.html when accessing non-existant html file', async () => {
expect((await fetchPath('doesnt-exist.html')).status).toBe(200)
})
describe.runIf(isServe)('outside base', () => {
test('should get a 404 with html', async () => {
const res = await fetch(new URL('/baz', viteTestUrl), {
headers: { Accept: 'text/html,*/*' },
})
expect(res.status).toBe(404)
expect(res.headers.get('Content-Type')).toBe('text/html')
})
test('should get a 404 with text', async () => {
const res = await fetch(new URL('/baz', viteTestUrl))
expect(res.status).toBe(404)
expect(res.headers.get('Content-Type')).toBe('text/plain')
})
})
describe('injected scripts', () => {
test('@vite/client', async () => {
const hasClient = await page.$(
'script[type="module"][src="/foo/bar/@vite/client"]',
)
if (isBuild) {
expect(hasClient).toBeFalsy()
} else {
expect(hasClient).toBeTruthy()
}
})
test('html-proxy', async () => {
const hasHtmlProxy = await page.$(
'script[type="module"][src^="/foo/bar/index.html?html-proxy"]',
)
if (isBuild) {
expect(hasHtmlProxy).toBeFalsy()
} else {
expect(hasHtmlProxy).toBeTruthy()
}
})
})
describe('raw references from /public', () => {
test('load raw js from /public', async () => {
expect(await page.textContent('.raw-js')).toMatch('[success]')
})
test('load raw css from /public', async () => {
expect(await getColor('.raw-css')).toBe('red')
})
})
test('import-expression from simple script', async () => {
expect(await page.textContent('.import-expression')).toMatch(
'[success][success]',
)
})
describe('asset imports from js', () => {
test('relative', async () => {
expect(await page.textContent('.asset-import-relative')).toMatch(assetMatch)
})
test('absolute', async () => {
expect(await page.textContent('.asset-import-absolute')).toMatch(assetMatch)
})
test('from /public', async () => {
expect(await page.textContent('.public-import')).toMatch(iconMatch)
})
test('from /public (json)', async () => {
expect(await page.textContent('.public-json-import')).toMatch(
'/foo/bar/foo.json',
)
expect(await page.textContent('.public-json-import-content'))
.toMatchInlineSnapshot(`
"{
\\"foo\\": \\"bar\\"
}
"
`)
})
})
describe('css url() references', () => {
test('fonts', async () => {
expect(
await page.evaluate(() => {
return (document as any).fonts.check('700 32px Inter')
}),
).toBe(true)
})
test('relative', async () => {
expect(await getBg('.css-url-relative')).toMatch(assetMatch)
})
test('image-set relative', async () => {
const imageSet = await getBg('.css-image-set-relative')
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('image-set without the url() call', async () => {
const imageSet = await getBg('.css-image-set-without-url-call')
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('image-set with var', async () => {
const imageSet = await getBg('.css-image-set-with-var')
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('image-set with mix', async () => {
const imageSet = await getBg('.css-image-set-mix-url-var')
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('image-set with base64', async () => {
const imageSet = await getBg('.css-image-set-base64')
expect(imageSet).toContain('image-set(url("data:image/png;base64,')
})
test('image-set with gradient', async () => {
const imageSet = await getBg('.css-image-set-gradient')
expect(imageSet).toContain('image-set(url("data:image/png;base64,')
})
test('image-set with multiple descriptor', async () => {
const imageSet = await getBg('.css-image-set-multiple-descriptor')
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('image-set with multiple descriptor as inline style', async () => {
const imageSet = await getBg(
'.css-image-set-multiple-descriptor-inline-style',
)
imageSet.split(', ').forEach((s) => {
expect(s).toMatch(assetMatch)
})
})
test('relative in @import', async () => {
expect(await getBg('.css-url-relative-at-imported')).toMatch(assetMatch)
})
test('absolute', async () => {
expect(await getBg('.css-url-absolute')).toMatch(assetMatch)
})
test('from /public', async () => {
expect(await getBg('.css-url-public')).toMatch(iconMatch)
})
test('base64 inline', async () => {
const match = isBuild ? `data:image/png;base64` : `/foo/bar/nested/icon.png`
expect(await getBg('.css-url-base64-inline')).toMatch(match)
expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match)
const icoMatch = isBuild ? `data:image/x-icon;base64` : `favicon.ico`
const el = await page.$(`link.ico`)
const href = await el.getAttribute('href')
expect(href).toMatch(icoMatch)
})
test('multiple urls on the same line', async () => {
const bg = await getBg('.css-url-same-line')
expect(bg).toMatch(assetMatch)
expect(bg).toMatch(iconMatch)
})
test('aliased', async () => {
const bg = await getBg('.css-url-aliased')
expect(bg).toMatch(assetMatch)
})
test.runIf(isBuild)('generated paths in CSS', () => {
const css = findAssetFile(/\.css$/, 'foo')
// preserve postfix query/hash
expect(css).toMatch(`woff2?#iefix`)
// generate non-relative base for public path in CSS
expect(css).not.toMatch(`../icon.png`)
})
})
describe('image', () => {
test('srcset', async () => {
const img = await page.$('.img-src-set')
const srcset = await img.getAttribute('srcset')
srcset.split(', ').forEach((s) => {
expect(s).toMatch(
isBuild
? /\/foo\/bar\/assets\/asset-\w{8}\.png \dx/
: /\/foo\/bar\/nested\/asset.png \dx/,
)
})
})
})
describe('svg fragments', () => {
// 404 is checked already, so here we just ensure the urls end with #fragment
test('img url', async () => {
const img = await page.$('.svg-frag-img')
expect(await img.getAttribute('src')).toMatch(/svg#icon-clock-view$/)
})
test('via css url()', async () => {
const bg = await page.evaluate(() => {
return getComputedStyle(document.querySelector('.icon')).backgroundImage
})
expect(bg).toMatch(/svg#icon-clock-view"\)$/)
})
test('from js import', async () => {
const img = await page.$('.svg-frag-import')
expect(await img.getAttribute('src')).toMatch(/svg#icon-heart-view$/)
})
})
test('Unknown extension assets import', async () => {
expect(await page.textContent('.unknown-ext')).toMatch(
isBuild ? 'data:application/octet-stream;' : '/nested/foo.unknown',
)
})
test('?raw import', async () => {
expect(await page.textContent('.raw')).toMatch('SVG')
})
test('?url import', async () => {
const src = readFile('foo.js')
expect(await page.textContent('.url')).toMatch(
isBuild
? `data:application/javascript;base64,${Buffer.from(src).toString(
'base64',
)}`
: `/foo/bar/foo.js`,
)
})
test('?url import on css', async () => {
const src = readFile('css/icons.css')
const txt = await page.textContent('.url-css')
expect(txt).toEqual(
isBuild
? `data:text/css;base64,${Buffer.from(src).toString('base64')}`
: '/foo/bar/css/icons.css',
)
})
describe('unicode url', () => {
test('from js import', async () => {
const src = readFile('テスト-測試-white space.js')
expect(await page.textContent('.unicode-url')).toMatch(
isBuild
? `data:application/javascript;base64,${Buffer.from(src).toString(
'base64',
)}`
: `/foo/bar/テスト-測試-white space.js`,
)
})
})
describe.runIf(isBuild)('encodeURI', () => {
test('img src with encodeURI', async () => {
const img = await page.$('.encodeURI')
expect(
(await img.getAttribute('src')).startsWith('data:image/png;base64'),
).toBe(true)
})
})
test('new URL(..., import.meta.url)', async () => {
expect(await page.textContent('.import-meta-url')).toMatch(assetMatch)
})
test('new URL("@/...", import.meta.url)', async () => {
expect(await page.textContent('.import-meta-url-dep')).toMatch(assetMatch)
})
test('new URL("/...", import.meta.url)', async () => {
expect(await page.textContent('.import-meta-url-base-path')).toMatch(
iconMatch,
)
})
test('new URL(..., import.meta.url) without extension', async () => {
expect(await page.textContent('.import-meta-url-without-extension')).toMatch(
isBuild ? 'data:application/javascript' : 'nested/test.js',
)
expect(
await page.textContent('.import-meta-url-content-without-extension'),
).toContain('export default class')
})
test('new URL(`${dynamic}`, import.meta.url)', async () => {
expect(await page.textContent('.dynamic-import-meta-url-1')).toMatch(
isBuild ? 'data:image/png;base64' : '/foo/bar/nested/icon.png',
)
expect(await page.textContent('.dynamic-import-meta-url-2')).toMatch(
assetMatch,
)
expect(await page.textContent('.dynamic-import-meta-url-js')).toMatch(
isBuild ? 'data:application/javascript;base64' : '/foo/bar/nested/test.js',
)
})
test('new URL(`./${dynamic}?abc`, import.meta.url)', async () => {
expect(await page.textContent('.dynamic-import-meta-url-1-query')).toMatch(
isBuild ? 'data:image/png;base64' : '/foo/bar/nested/icon.png?abc',
)
expect(await page.textContent('.dynamic-import-meta-url-2-query')).toMatch(
isBuild
? /\/foo\/bar\/assets\/asset-\w{8}\.png\?abc/
: '/foo/bar/nested/asset.png?abc',
)
})
test('new URL(`./${1 === 0 ? static : dynamic}?abc`, import.meta.url)', async () => {
expect(await page.textContent('.dynamic-import-meta-url-1-ternary')).toMatch(
isBuild ? 'data:image/png;base64' : '/foo/bar/nested/icon.png?abc',
)
expect(await page.textContent('.dynamic-import-meta-url-2-ternary')).toMatch(
isBuild
? /\/foo\/bar\/assets\/asset-\w{8}\.png\?abc/
: '/foo/bar/nested/asset.png?abc',
)
})
test('new URL(`non-existent`, import.meta.url)', async () => {
// the inlined script tag is extracted in a separate file
const importMetaUrl = new URL(
isBuild ? '/foo/bar/assets/index.js' : '/foo/bar/index.html',
page.url(),
)
expect(await page.textContent('.non-existent-import-meta-url')).toMatch(
new URL('non-existent', importMetaUrl).pathname,
)
})
test.runIf(isBuild)('manifest', async () => {
const manifest = readManifest('foo')
const entry = manifest['index.html']
for (const file of listAssets('foo')) {
if (file.endsWith('.css')) {
expect(entry.css).toContain(`assets/${file}`)
} else if (!file.endsWith('.js')) {
expect(entry.assets).toContain(`assets/${file}`)
}
}
})
describe.runIf(isBuild)('css and assets in css in build watch', () => {
test('css will not be lost and css does not contain undefined', async () => {
editFile('index.html', (code) => code.replace('Assets', 'assets'), true)
await notifyRebuildComplete(watcher)
const cssFile = findAssetFile(/index-\w+\.css$/, 'foo')
expect(cssFile).not.toBe('')
expect(cssFile).not.toMatch(/undefined/)
})
test('import module.css', async () => {
expect(await getColor('#foo')).toBe('red')
editFile('css/foo.module.css', (code) => code.replace('red', 'blue'), true)
await notifyRebuildComplete(watcher)
await page.reload()
expect(await getColor('#foo')).toBe('blue')
})
test('import with raw query', async () => {
expect(await page.textContent('.raw-query')).toBe('foo')
editFile('static/foo.txt', (code) => code.replace('foo', 'zoo'), true)
await notifyRebuildComplete(watcher)
await page.reload()
expect(await page.textContent('.raw-query')).toBe('zoo')
})
})
test('inline style test', async () => {
expect(await getBg('.inline-style')).toMatch(assetMatch)
expect(await getBg('.style-url-assets')).toMatch(assetMatch)
})
if (!isBuild) {
test('@import in html style tag hmr', async () => {
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 136, 255)')
const loadPromise = page.waitForEvent('load')
editFile(
'./css/import.css',
(code) => code.replace('#0088ff', '#00ff88'),
true,
)
await loadPromise
await untilUpdated(() => getColor('.import-css'), 'rgb(0, 255, 136)')
})
}
test('html import word boundary', async () => {
expect(await page.textContent('.obj-import-express')).toMatch(
'ignore object import prop',
)
expect(await page.textContent('.string-import-express')).toMatch('no load')
})
test('relative path in html asset', async () => {
expect(await page.textContent('.relative-js')).toMatch('hello')
expect(await getColor('.relative-css')).toMatch('red')
})
test('url() contains file in publicDir, in <style> tag', async () => {
expect(await getBg('.style-public-assets')).toContain(iconMatch)
})
test('url() contains file in publicDir, as inline style', async () => {
expect(await getBg('.inline-style-public')).toContain(iconMatch)
})
test.runIf(isBuild)('assets inside <noscript> is rewrote', async () => {
const indexHtml = readFile('./dist/foo/index.html')
expect(indexHtml).toMatch(
/<img class="noscript" src="\/foo\/bar\/assets\/asset-\w+\.png" \/>/,
)
})