2022-10-19 15:29:01 +00:00
|
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import fsp from 'node:fs/promises'
|
2023-06-28 13:49:50 +00:00
|
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
2022-10-19 15:29:01 +00:00
|
|
|
import { execaCommand } from 'execa'
|
2024-06-24 14:39:34 +00:00
|
|
|
import { fdir } from 'fdir'
|
|
|
|
import { join, resolve } from 'pathe'
|
2022-10-19 15:29:01 +00:00
|
|
|
|
2023-06-07 20:07:58 +00:00
|
|
|
describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM_CI)('minimal nuxt application', () => {
|
2022-10-19 15:29:01 +00:00
|
|
|
const rootDir = fileURLToPath(new URL('./fixtures/minimal', import.meta.url))
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-06-28 13:49:50 +00:00
|
|
|
await Promise.all([
|
|
|
|
execaCommand(`pnpm nuxi build ${rootDir}`, { env: { EXTERNAL_VUE: 'false' } }),
|
2024-04-05 18:08:32 +00:00
|
|
|
execaCommand(`pnpm nuxi build ${rootDir}`, { env: { EXTERNAL_VUE: 'true' } }),
|
2023-06-28 13:49:50 +00:00
|
|
|
])
|
2022-10-19 15:29:01 +00:00
|
|
|
}, 120 * 1000)
|
|
|
|
|
2023-06-28 13:49:50 +00:00
|
|
|
// Identical behaviour between inline/external vue options as this should only affect the server build
|
|
|
|
for (const outputDir of ['.output', '.output-inline']) {
|
|
|
|
it('default client bundle size', async () => {
|
|
|
|
const clientStats = await analyzeSizes('**/*.js', join(rootDir, outputDir, 'public'))
|
2024-06-14 21:12:47 +00:00
|
|
|
expect.soft(roundToKilobytes(clientStats.totalBytes)).toMatchInlineSnapshot(`"106k"`)
|
2023-06-28 13:49:50 +00:00
|
|
|
expect(clientStats.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
|
|
|
|
[
|
|
|
|
"_nuxt/entry.js",
|
|
|
|
]
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
}
|
2022-10-19 15:29:01 +00:00
|
|
|
|
|
|
|
it('default server bundle size', async () => {
|
2023-06-28 13:49:50 +00:00
|
|
|
const serverDir = join(rootDir, '.output/server')
|
|
|
|
|
2024-06-24 14:39:34 +00:00
|
|
|
const serverStats = await analyzeSizes('**/*.mjs', serverDir, { excludeNodeModules: true })
|
2024-06-21 08:33:00 +00:00
|
|
|
expect.soft(roundToKilobytes(serverStats.totalBytes)).toMatchInlineSnapshot(`"210k"`)
|
2022-10-19 15:29:01 +00:00
|
|
|
|
|
|
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
2024-06-14 21:12:47 +00:00
|
|
|
expect.soft(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot(`"1340k"`)
|
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",
|
2023-04-07 10:34:35 +00:00
|
|
|
"devalue",
|
2023-12-29 08:53:46 +00:00
|
|
|
"entities",
|
2022-10-19 15:29:01 +00:00
|
|
|
"estree-walker",
|
|
|
|
"hookable",
|
2023-05-11 08:37:32 +00:00
|
|
|
"source-map-js",
|
2022-10-19 15:29:01 +00:00
|
|
|
"ufo",
|
2023-02-16 11:44:31 +00:00
|
|
|
"unhead",
|
2022-10-19 15:29:01 +00:00
|
|
|
"vue",
|
|
|
|
"vue-bundle-renderer",
|
|
|
|
]
|
|
|
|
`)
|
|
|
|
})
|
2023-06-28 13:49:50 +00:00
|
|
|
|
|
|
|
it('default server bundle size (inlined vue modules)', async () => {
|
|
|
|
const serverDir = join(rootDir, '.output-inline/server')
|
|
|
|
|
2024-06-24 14:39:34 +00:00
|
|
|
const serverStats = await analyzeSizes('**/*.mjs', serverDir, { excludeNodeModules: true })
|
2024-06-21 08:33:00 +00:00
|
|
|
expect.soft(roundToKilobytes(serverStats.totalBytes)).toMatchInlineSnapshot(`"531k"`)
|
2023-06-28 13:49:50 +00:00
|
|
|
|
|
|
|
const modules = await analyzeSizes('node_modules/**/*', serverDir)
|
2024-06-07 06:37:13 +00:00
|
|
|
expect.soft(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot(`"76.2k"`)
|
2023-06-28 13:49:50 +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(`
|
|
|
|
[
|
|
|
|
"@unhead/dom",
|
|
|
|
"@unhead/shared",
|
|
|
|
"@unhead/ssr",
|
|
|
|
"devalue",
|
|
|
|
"hookable",
|
|
|
|
"unhead",
|
|
|
|
]
|
|
|
|
`)
|
|
|
|
})
|
2022-10-19 15:29:01 +00:00
|
|
|
})
|
|
|
|
|
2024-06-24 14:39:34 +00:00
|
|
|
async function analyzeSizes (pattern: string | string[], rootDir: string, opts: { excludeNodeModules?: boolean } = {}) {
|
|
|
|
const patterns = Array.isArray(pattern) ? pattern : [pattern]
|
|
|
|
const files: string[] = new fdir({
|
|
|
|
resolveSymlinks: true,
|
|
|
|
exclude: p => opts.excludeNodeModules ? p.includes('node_modules') : false
|
|
|
|
})
|
|
|
|
.withRelativePaths().globWithOptions(patterns, { dot: true }).crawl(rootDir).sync()
|
2022-10-19 15:29:01 +00:00
|
|
|
let totalBytes = 0
|
|
|
|
for (const file of files) {
|
2024-06-24 14:39:34 +00:00
|
|
|
const path = resolve(rootDir, file)
|
2023-01-20 16:17:31 +00:00
|
|
|
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 }
|
|
|
|
}
|
2023-04-04 12:34:29 +00:00
|
|
|
|
|
|
|
function roundToKilobytes (bytes: number) {
|
2023-04-12 08:42:45 +00:00
|
|
|
return (bytes / 1024).toFixed(bytes > (100 * 1024) ? 0 : 1) + 'k'
|
2023-04-04 12:34:29 +00:00
|
|
|
}
|