-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathbuild.ts
99 lines (85 loc) · 2.69 KB
/
build.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { Nitro } from 'nitropack'
import process from 'node:process'
import { defineCommand } from 'citty'
import { relative, resolve } from 'pathe'
import { showVersions } from '../utils/banner'
import { overrideEnv } from '../utils/env'
import { clearBuildDir } from '../utils/fs'
import { loadKit } from '../utils/kit'
import { logger } from '../utils/logger'
import { cwdArgs, dotEnvArgs, envNameArgs, legacyRootDirArgs, logLevelArgs } from './_shared'
export default defineCommand({
meta: {
name: 'build',
description: 'Build Nuxt for production deployment',
},
args: {
...cwdArgs,
...logLevelArgs,
prerender: {
type: 'boolean',
description: 'Build Nuxt and prerender static routes',
},
preset: {
type: 'string',
description: 'Nitro server preset',
},
...dotEnvArgs,
...envNameArgs,
...legacyRootDirArgs,
},
async run(ctx) {
overrideEnv('production')
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir)
await showVersions(cwd)
const kit = await loadKit(cwd)
const nuxt = await kit.loadNuxt({
cwd,
dotenv: {
cwd,
fileName: ctx.args.dotenv,
},
envName: ctx.args.envName, // c12 will fall back to NODE_ENV
overrides: {
logLevel: ctx.args.logLevel as 'silent' | 'info' | 'verbose',
// TODO: remove in 3.8
_generate: ctx.args.prerender,
nitro: {
static: ctx.args.prerender,
preset: ctx.args.preset || process.env.NITRO_PRESET || process.env.SERVER_PRESET,
},
...ctx.data?.overrides,
},
})
let nitro: Nitro | undefined
// In Bridge, if Nitro is not enabled, useNitro will throw an error
try {
// Use ? for backward compatibility for Nuxt <= RC.10
nitro = kit.useNitro?.()
logger.info(`Building for Nitro preset: \`${nitro.options.preset}\``)
}
catch {
//
}
await clearBuildDir(nuxt.options.buildDir)
await kit.writeTypes(nuxt)
nuxt.hook('build:error', (err) => {
logger.error('Nuxt Build Error:', err)
process.exit(1)
})
await kit.buildNuxt(nuxt)
if (ctx.args.prerender) {
if (!nuxt.options.ssr) {
logger.warn(
'HTML content not prerendered because `ssr: false` was set. You can read more in `https://nuxt.com/docs/getting-started/deployment#static-hosting`.',
)
}
// TODO: revisit later if/when nuxt build --prerender will output hybrid
const dir = nitro?.options.output.publicDir
const publicDir = dir ? relative(process.cwd(), dir) : '.output/public'
logger.success(
`You can now deploy \`${publicDir}\` to any static hosting!`,
)
}
},
})