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'
|
2022-10-15 18:42:57 +00:00
|
|
|
import { createApp, eventHandler, lazyEventHandler, toNodeListener } 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',
|
2023-03-07 12:18:47 +00:00
|
|
|
usage: 'npx nuxi analyze [--log-level] [rootDir]',
|
2021-10-21 19:51:44 +00:00
|
|
|
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,
|
2023-03-07 12:18:47 +00:00
|
|
|
overrides: {
|
|
|
|
build: { analyze: true },
|
|
|
|
logLevel: args['log-level']
|
2021-10-21 19:51:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await clearDir(nuxt.options.buildDir)
|
|
|
|
await writeTypes(nuxt)
|
|
|
|
await buildNuxt(nuxt)
|
|
|
|
|
|
|
|
const app = createApp()
|
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
const serveFile = (filePath: string) => lazyEventHandler(async () => {
|
2021-10-21 19:51:44 +00:00
|
|
|
const contents = await fsp.readFile(filePath, 'utf-8')
|
2022-11-15 14:33:43 +00:00
|
|
|
return eventHandler((event) => { event.node.res.end(contents) })
|
2021-10-21 19:51:44 +00:00
|
|
|
})
|
|
|
|
|
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')))
|
2022-10-19 15:29:20 +00:00
|
|
|
app.use(eventHandler(() => `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>Nuxt Bundle Stats (experimental)</title>
|
|
|
|
</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>
|
|
|
|
`))
|
2021-10-21 19:51:44 +00:00
|
|
|
|
2022-10-15 18:42:57 +00:00
|
|
|
await listen(toNodeListener(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
|
|
|
}
|
|
|
|
})
|