Nuxt/packages/nuxi/src/commands/build.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

import { relative, resolve } from 'pathe'
import consola from 'consola'
import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { clearDir } from '../utils/fs'
import { overrideEnv } from '../utils/env'
import { showVersions } from '../utils/banner'
import { defineNuxtCommand } from './index'
2021-04-15 19:17:44 +00:00
export default defineNuxtCommand({
meta: {
name: 'build',
usage: 'npx nuxi build [--prerender] [--dotenv] [rootDir]',
description: 'Build nuxt for production deployment'
},
async invoke (args) {
overrideEnv('production')
const rootDir = resolve(args._[0] || '.')
showVersions(rootDir)
2021-04-09 15:52:45 +00:00
const { loadNuxt, buildNuxt, useNitro } = await loadKit(rootDir)
const nuxt = await loadNuxt({
rootDir,
dotenv: {
cwd: rootDir,
fileName: args.dotenv
},
defaults: {
experimental: {
payloadExtraction: args.prerender ? true : undefined
}
},
overrides: {
_generate: args.prerender
}
})
// Use ? for backward compatibility for Nuxt <= RC.10
const nitro = useNitro?.()
await clearDir(nuxt.options.buildDir)
await writeTypes(nuxt)
nuxt.hook('build:error', (err) => {
consola.error('Nuxt Build Error:', err)
process.exit(1)
})
2021-04-09 15:52:45 +00:00
await buildNuxt(nuxt)
if (args.prerender) {
// 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'
consola.success(`You can now deploy \`${publicDir}\` to any static hosting!`)
}
}
})