This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bundle.test.ts
98 lines (89 loc) · 2.85 KB
/
bundle.test.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
import { fileURLToPath } from 'node:url'
import fsp from 'node:fs/promises'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { execaCommand } from 'execa'
import { globby } from 'globby'
import { join } from 'pathe'
import { isWindows } from 'std-env'
describe.skipIf(isWindows)('minimal nuxt application', () => {
const rootDir = fileURLToPath(new URL('./fixtures/minimal', import.meta.url))
const publicDir = join(rootDir, '.output/public')
const serverDir = join(rootDir, '.output/server')
const stats = {
client: { totalBytes: 0, files: [] as string[] },
server: { totalBytes: 0, files: [] as string[] }
}
beforeAll(async () => {
await execaCommand(`pnpm nuxi build ${rootDir}`)
}, 120 * 1000)
afterAll(async () => {
await fsp.writeFile(join(rootDir, '.output/test-stats.json'), JSON.stringify(stats, null, 2))
})
it('default client bundle size', async () => {
stats.client = await analyzeSizes('**/*.js', publicDir)
expect(stats.client.totalBytes).toBeLessThan(110000)
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[
"_nuxt/composables.js",
"_nuxt/entry.js",
"_nuxt/error-404.js",
"_nuxt/error-500.js",
"_nuxt/error-component.js",
]
`)
})
it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(stats.server.totalBytes).toBeLessThan(120000)
const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(modules.totalBytes).toBeLessThan(2700000)
const packages = modules.files
.filter(m => m.endsWith('package.json'))
.map(m => m.replace('/package.json', '').replace('node_modules/', ''))
.sort()
expect(packages).toMatchInlineSnapshot(`
[
"@babel/parser",
"@vue/compiler-core",
"@vue/compiler-dom",
"@vue/compiler-ssr",
"@vue/reactivity",
"@vue/runtime-core",
"@vue/runtime-dom",
"@vue/server-renderer",
"@vue/shared",
"@vueuse/shared",
"buffer-from",
"cookie-es",
"destr",
"estree-walker",
"h3",
"hookable",
"node-fetch-native",
"ofetch",
"ohash",
"pathe",
"radix3",
"scule",
"source-map",
"source-map-support",
"ufo",
"unctx",
"unenv",
"unstorage",
"vue",
"vue-bundle-renderer",
"vue-demi",
]
`)
})
})
async function analyzeSizes (pattern: string | string[], rootDir: string) {
const files: string[] = await globby(pattern, { cwd: rootDir })
let totalBytes = 0
for (const file of files) {
const bytes = Buffer.byteLength(await fsp.readFile(join(rootDir, file)))
totalBytes += bytes
}
return { files, totalBytes }
}