2022-10-19 15:29:01 +00:00
|
|
|
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)
|
2023-03-20 21:46:12 +00:00
|
|
|
expect(stats.client.totalBytes).toBeLessThan(106650)
|
2022-10-19 15:29:01 +00:00
|
|
|
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
|
|
|
|
[
|
2023-02-16 15:00:40 +00:00
|
|
|
"_nuxt/_plugin-vue_export-helper.js",
|
2022-10-19 15:29:01 +00:00
|
|
|
"_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)
|
2023-03-17 21:50:51 +00:00
|
|
|
expect(stats.server.totalBytes).toBeLessThan(93900)
|
2022-10-19 15:29:01 +00:00
|
|
|
|
|
|
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
2023-03-16 07:34:22 +00:00
|
|
|
expect(modules.totalBytes).toBeLessThan(2693900)
|
2022-10-19 15:29:01 +00:00
|
|
|
|
|
|
|
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",
|
2022-11-15 16:26:38 +00:00
|
|
|
"@unhead/dom",
|
2023-02-16 11:44:31 +00:00
|
|
|
"@unhead/shared",
|
2022-11-15 16:26:38 +00:00
|
|
|
"@unhead/ssr",
|
2022-10-19 15:29:01 +00:00
|
|
|
"@vue/compiler-core",
|
|
|
|
"@vue/compiler-dom",
|
|
|
|
"@vue/compiler-ssr",
|
|
|
|
"@vue/reactivity",
|
|
|
|
"@vue/runtime-core",
|
|
|
|
"@vue/runtime-dom",
|
|
|
|
"@vue/server-renderer",
|
|
|
|
"@vue/shared",
|
|
|
|
"cookie-es",
|
2022-11-15 17:36:57 +00:00
|
|
|
"defu",
|
2022-10-19 15:29:01 +00:00
|
|
|
"destr",
|
|
|
|
"estree-walker",
|
|
|
|
"h3",
|
|
|
|
"hookable",
|
2023-02-09 06:02:07 +00:00
|
|
|
"iron-webcrypto",
|
2022-10-19 15:29:01 +00:00
|
|
|
"node-fetch-native",
|
2022-11-15 14:33:43 +00:00
|
|
|
"ofetch",
|
2022-10-19 15:29:01 +00:00
|
|
|
"ohash",
|
|
|
|
"pathe",
|
|
|
|
"radix3",
|
|
|
|
"scule",
|
|
|
|
"source-map",
|
|
|
|
"ufo",
|
2023-02-07 10:00:24 +00:00
|
|
|
"uncrypto",
|
2022-10-19 15:29:01 +00:00
|
|
|
"unctx",
|
|
|
|
"unenv",
|
2023-02-16 11:44:31 +00:00
|
|
|
"unhead",
|
2022-10-19 15:29:01 +00:00
|
|
|
"unstorage",
|
|
|
|
"vue",
|
|
|
|
"vue-bundle-renderer",
|
|
|
|
]
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
async function analyzeSizes (pattern: string | string[], rootDir: string) {
|
|
|
|
const files: string[] = await globby(pattern, { cwd: rootDir })
|
|
|
|
let totalBytes = 0
|
|
|
|
for (const file of files) {
|
2023-01-20 16:17:31 +00:00
|
|
|
const path = join(rootDir, file)
|
|
|
|
const isSymlink = (await fsp.lstat(path).catch(() => null))?.isSymbolicLink()
|
|
|
|
|
|
|
|
if (!isSymlink) {
|
|
|
|
const bytes = Buffer.byteLength(await fsp.readFile(path))
|
|
|
|
totalBytes += bytes
|
|
|
|
}
|
2022-10-19 15:29:01 +00:00
|
|
|
}
|
|
|
|
return { files, totalBytes }
|
|
|
|
}
|