2022-04-15 15:19:05 +00:00
|
|
|
import { promises as fsp } from 'node:fs'
|
2021-10-21 19:51:44 +00:00
|
|
|
import { join, resolve } from 'pathe'
|
|
|
|
import { createApp, lazyHandle } from 'h3'
|
2022-04-07 11:28:04 +00:00
|
|
|
import { listen } from 'listhen'
|
2021-10-21 19:51:44 +00:00
|
|
|
import { writeTypes } from '../utils/prepare'
|
|
|
|
import { loadKit } from '../utils/kit'
|
|
|
|
import { clearDir } from '../utils/fs'
|
2022-06-12 21:26:36 +00:00
|
|
|
import { overrideEnv } from '../utils/env'
|
2021-10-21 19:51:44 +00:00
|
|
|
import { defineNuxtCommand } from './index'
|
|
|
|
|
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'analyze',
|
|
|
|
usage: 'npx nuxi analyze [rootDir]',
|
|
|
|
description: 'Build nuxt and analyze production bundle (experimental)'
|
|
|
|
},
|
|
|
|
async invoke (args) {
|
2022-06-12 21:26:36 +00:00
|
|
|
overrideEnv('production')
|
2021-10-21 19:51:44 +00:00
|
|
|
|
|
|
|
const rootDir = resolve(args._[0] || '.')
|
|
|
|
const statsDir = join(rootDir, '.nuxt/stats')
|
|
|
|
|
|
|
|
const { loadNuxt, buildNuxt } = await loadKit(rootDir)
|
|
|
|
|
|
|
|
const nuxt = await loadNuxt({
|
|
|
|
rootDir,
|
|
|
|
config: {
|
|
|
|
build: {
|
2022-02-21 11:25:31 +00:00
|
|
|
analyze: true
|
2021-10-21 19:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await clearDir(nuxt.options.buildDir)
|
|
|
|
await writeTypes(nuxt)
|
|
|
|
await buildNuxt(nuxt)
|
|
|
|
|
|
|
|
const app = createApp()
|
|
|
|
|
|
|
|
const serveFile = (filePath: string) => lazyHandle(async () => {
|
|
|
|
const contents = await fsp.readFile(filePath, 'utf-8')
|
|
|
|
return (_req, res) => { res.end(contents) }
|
|
|
|
})
|
|
|
|
|
2022-02-01 15:34:06 +00:00
|
|
|
console.warn('Do not deploy analyze results! Use `nuxi build` before deploying.')
|
2021-10-21 19:51:44 +00:00
|
|
|
|
|
|
|
console.info('Starting stats server...')
|
|
|
|
|
|
|
|
app.use('/client', serveFile(join(statsDir, 'client.html')))
|
|
|
|
app.use('/nitro', serveFile(join(statsDir, 'nitro.html')))
|
|
|
|
app.use(() => `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
2021-10-21 20:38:17 +00:00
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Nuxt Bundle Stats (experimental)</title>
|
2021-10-21 19:51:44 +00:00
|
|
|
</head>
|
|
|
|
<h1>Nuxt Bundle Stats (experimental)</h1>
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<a href="/nitro">Nitro server bundle stats</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="/client">Client bundle stats</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</html>`)
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
await listen(app)
|
2022-04-20 14:47:43 +00:00
|
|
|
|
2022-04-29 09:38:22 +00:00
|
|
|
return 'wait' as const
|
2021-10-21 19:51:44 +00:00
|
|
|
}
|
|
|
|
})
|