forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcss-codesplit.spec.ts
70 lines (59 loc) · 2.32 KB
/
css-codesplit.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
import { describe, expect, test } from 'vitest'
import {
findAssetFile,
getColor,
isBuild,
listAssets,
page,
readManifest,
untilUpdated,
} from '~utils'
test('should load all stylesheets', async () => {
expect(await getColor('h1')).toBe('red')
expect(await getColor('h2')).toBe('blue')
expect(await getColor('.dynamic')).toBe('green')
expect(await getColor('.async-js')).toBe('blue')
expect(await getColor('.chunk')).toBe('magenta')
})
test('should load dynamic import with inline', async () => {
const css = await page.textContent('.dynamic-inline')
expect(css).toMatch('.inline')
expect(await getColor('.inline')).not.toBe('yellow')
})
test('should load dynamic import with module', async () => {
const css = await page.textContent('.dynamic-module')
expect(css).toMatch('_mod_')
expect(await getColor('.mod')).toBe('yellow')
})
test('style order should be consistent when style tag is inserted by JS', async () => {
expect(await getColor('.order-bulk')).toBe('orange')
await page.click('.order-bulk-update')
await untilUpdated(() => getColor('.order-bulk'), 'green')
})
describe.runIf(isBuild)('build', () => {
test('should remove empty chunk', async () => {
expect(findAssetFile(/style-.*\.js$/)).toBe('')
expect(findAssetFile('main.*.js$')).toMatch(`/* empty css`)
expect(findAssetFile('other.*.js$')).toMatch(`/* empty css`)
expect(findAssetFile(/async-[-\w]{8}\.js$/)).toBe('')
const assets = listAssets()
expect(assets).not.toContainEqual(
expect.stringMatching(/async-js-[-\w]{8}\.js$/),
)
})
test('should remove empty chunk, HTML without JS', async () => {
const sharedCSSWithJSChunk = findAssetFile('shared-css-with-js.*.js$')
expect(sharedCSSWithJSChunk).toMatch(`/* empty css`)
// there are functions and modules in the src code that should be tree-shaken
expect(sharedCSSWithJSChunk).not.toMatch('function')
expect(sharedCSSWithJSChunk).not.toMatch(/import(?!".\/modulepreload)/)
})
test('should generate correct manifest', async () => {
const manifest = readManifest()
expect(manifest['index.html'].css.length).toBe(2)
expect(manifest['other.js'].css.length).toBe(1)
})
test('should not mark a css chunk with ?url and normal import as pure css chunk', () => {
expect(findAssetFile(/chunk-.*\.js$/)).toBeTruthy()
})
})