-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
generate.js
110 lines (98 loc) · 2.92 KB
/
generate.js
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
100
101
102
103
104
105
106
107
108
109
110
import { TARGETS } from '@nuxt/utils'
import { common, locking } from '../options'
import { normalizeArg, createLock } from '../utils'
export default {
name: 'generate',
description: 'Generate a static web application (server-rendered)',
usage: 'generate <dir>',
options: {
...common,
...locking,
build: {
type: 'boolean',
default: true,
description: 'Only generate pages for dynamic routes, used for incremental builds. Generate has to be run once without this option before using it'
},
devtools: {
type: 'boolean',
default: false,
description: 'Enable Vue devtools',
prepare (cmd, options, argv) {
options.vue = options.vue || {}
options.vue.config = options.vue.config || {}
if (argv.devtools) {
options.vue.config.devtools = true
}
}
},
quiet: {
alias: 'q',
type: 'boolean',
description: 'Disable output except for errors',
prepare (cmd, options, argv) {
// Silence output when using --quiet
options.build = options.build || {}
if (argv.quiet) {
options.build.quiet = true
}
}
},
modern: {
...common.modern,
description: 'Generate app in modern build (modern mode can be only client)',
prepare (cmd, options, argv) {
if (normalizeArg(argv.modern)) {
options.modern = 'client'
}
}
},
'fail-on-error': {
type: 'boolean',
default: false,
description: 'Exit with non-zero status code if there are errors when generating pages'
}
},
async run (cmd) {
const config = await cmd.getNuxtConfig({
dev: false,
_build: cmd.argv.build,
_generate: true
})
if (config.target === TARGETS.static) {
throw new Error("Please use `nuxt export` when using `target: 'static'`")
}
// Forcing static target anyway
config.target = TARGETS.static
// Disable analyze if set by the nuxt config
config.build = config.build || {}
config.build.analyze = false
// Set flag to keep the prerendering behaviour
config._legacyGenerate = true
const nuxt = await cmd.getNuxt(config)
if (cmd.argv.lock) {
await cmd.setLock(await createLock({
id: 'build',
dir: nuxt.options.buildDir,
root: config.rootDir
}))
nuxt.hook('build:done', async () => {
await cmd.releaseLock()
await cmd.setLock(await createLock({
id: 'generate',
dir: nuxt.options.generate.dir,
root: config.rootDir
}))
})
}
const generator = await cmd.getGenerator(nuxt)
await nuxt.server.listen(0)
const { errors } = await generator.generate({
init: true,
build: cmd.argv.build
})
await nuxt.close()
if (cmd.argv['fail-on-error'] && errors.length > 0) {
throw new Error('Error generating pages, exiting with non-zero code')
}
}
}