2022-09-16 09:26:28 +00:00
|
|
|
import { relative, resolve } from 'pathe'
|
2021-10-07 10:15:15 +00:00
|
|
|
import consola from 'consola'
|
2021-10-07 13:53:31 +00:00
|
|
|
import { writeTypes } from '../utils/prepare'
|
2021-10-13 10:44:54 +00:00
|
|
|
import { loadKit } from '../utils/kit'
|
2021-10-18 14:22:02 +00:00
|
|
|
import { clearDir } from '../utils/fs'
|
2022-06-12 21:26:36 +00:00
|
|
|
import { overrideEnv } from '../utils/env'
|
2022-09-01 09:34:56 +00:00
|
|
|
import { showVersions } from '../utils/banner'
|
2021-07-26 14:04:35 +00:00
|
|
|
import { defineNuxtCommand } from './index'
|
2021-04-15 19:17:44 +00:00
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'build',
|
2022-09-26 10:36:10 +00:00
|
|
|
usage: 'npx nuxi build [--prerender] [--dotenv] [rootDir]',
|
2021-07-26 14:04:35 +00:00
|
|
|
description: 'Build nuxt for production deployment'
|
|
|
|
},
|
|
|
|
async invoke (args) {
|
2022-06-12 21:26:36 +00:00
|
|
|
overrideEnv('production')
|
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
const rootDir = resolve(args._[0] || '.')
|
2022-09-01 09:34:56 +00:00
|
|
|
showVersions(rootDir)
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2022-09-16 09:26:28 +00:00
|
|
|
const { loadNuxt, buildNuxt, useNitro } = await loadKit(rootDir)
|
2021-07-15 11:24:26 +00:00
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
const nuxt = await loadNuxt({
|
|
|
|
rootDir,
|
2022-09-26 10:36:10 +00:00
|
|
|
dotenv: {
|
|
|
|
cwd: rootDir,
|
|
|
|
fileName: args.dotenv
|
|
|
|
},
|
2022-04-07 11:28:04 +00:00
|
|
|
overrides: {
|
|
|
|
_generate: args.prerender
|
|
|
|
}
|
|
|
|
})
|
2021-07-15 11:24:26 +00:00
|
|
|
|
2022-09-16 17:27:50 +00:00
|
|
|
// Use ? for backward compatibility for Nuxt <= RC.10
|
|
|
|
const nitro = useNitro?.()
|
2022-09-16 09:26:28 +00:00
|
|
|
|
2021-10-18 14:22:02 +00:00
|
|
|
await clearDir(nuxt.options.buildDir)
|
|
|
|
|
2021-10-07 13:53:31 +00:00
|
|
|
await writeTypes(nuxt)
|
|
|
|
|
2021-10-20 18:50:01 +00:00
|
|
|
nuxt.hook('build:error', (err) => {
|
2021-10-07 10:15:15 +00:00
|
|
|
consola.error('Nuxt Build Error:', err)
|
2021-07-26 14:04:35 +00:00
|
|
|
process.exit(1)
|
|
|
|
})
|
2021-04-09 15:52:45 +00:00
|
|
|
|
2021-07-26 14:04:35 +00:00
|
|
|
await buildNuxt(nuxt)
|
2022-09-16 09:26:28 +00:00
|
|
|
|
|
|
|
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!`)
|
|
|
|
}
|
2021-07-26 14:04:35 +00:00
|
|
|
}
|
|
|
|
})
|