-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathbundle.test.ts
More file actions
60 lines (53 loc) · 2.03 KB
/
bundle.test.ts
File metadata and controls
60 lines (53 loc) · 2.03 KB
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
import { join } from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { promises as fsp } from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises'
import { buildNuxt, loadNuxt } from '@nuxt/kit'
import type { NuxtConfig } from '@nuxt/schema'
import { describe, it, expect } from 'vitest'
import { glob } from 'tinyglobby'
import { isWindows } from 'std-env'
describe.skipIf(process.env.ECOSYSTEM_CI || isWindows)('nuxt image bundle size', () => {
it('should match snapshot', { timeout: 120_000 }, async () => {
const rootDir = fileURLToPath(new URL('../.tmp', import.meta.url))
await fsp.rm(rootDir, { recursive: true, force: true })
const withoutImage = await build(join(rootDir, 'without'))
const withImage = await build(join(rootDir, 'with'), {
modules: ['@nuxt/image'],
image: { provider: 'ipx' },
})
expect(roundToKilobytes(withImage.totalBytes - withoutImage.totalBytes)).toMatchInlineSnapshot(`"12.8k"`)
})
})
async function build(rootDir: string, config: NuxtConfig = {}) {
await mkdir(rootDir, { recursive: true })
await writeFile(join(rootDir, 'app.vue'), `<template><NuxtImg src="/test.jpg" /><NuxtPicture src="/test.jpg" /></template>`)
const nuxt = await loadNuxt({
cwd: rootDir,
ready: true,
overrides: {
ssr: false,
...config,
},
})
await buildNuxt(nuxt)
await nuxt.close()
return await analyzeSizes(['**/*.js'], join(rootDir, '.output/public'))
}
async function analyzeSizes(pattern: string[], rootDir: string) {
const files: string[] = await glob(pattern, { cwd: rootDir })
let totalBytes = 0
for (const file of files) {
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
}
}
return { files, totalBytes }
}
function roundToKilobytes(bytes: number) {
return (bytes / 1024).toFixed(bytes > (100 * 1024) ? 0 : 1) + 'k'
}