2020-05-07 19:08:01 +00:00
import { TARGETS } from '@nuxt/utils'
2020-10-15 16:12:06 +00:00
import consola from 'consola'
2019-03-03 08:12:46 +00:00
import { common , locking } from '../options'
import { normalizeArg , createLock } from '../utils'
2020-07-16 15:10:54 +00:00
import { ensureBuild , generate } from '../utils/generate'
2018-10-17 21:28:25 +00:00
2018-10-29 22:16:16 +00:00
export default {
name : 'generate' ,
description : 'Generate a static web application (server-rendered)' ,
usage : 'generate <dir>' ,
options : {
... common ,
2019-03-03 08:12:46 +00:00
... locking ,
2018-10-29 22:16:16 +00:00
build : {
type : 'boolean' ,
default : true ,
2020-06-04 18:41:09 +00:00
description : 'Only generate pages for dynamic routes, used for incremental builds. Generate has to be run once without this option before using it'
2018-11-07 23:37:06 +00:00
} ,
2018-11-17 23:05:51 +00:00
devtools : {
type : 'boolean' ,
default : false ,
description : 'Enable Vue devtools' ,
2019-07-10 10:45:49 +00:00
prepare ( cmd , options , argv ) {
2018-11-17 23:05:51 +00:00
options . vue = options . vue || { }
options . vue . config = options . vue . config || { }
if ( argv . devtools ) {
options . vue . config . devtools = true
}
}
} ,
2019-03-29 14:08:08 +00:00
quiet : {
alias : 'q' ,
type : 'boolean' ,
description : 'Disable output except for errors' ,
2019-07-10 10:45:49 +00:00
prepare ( cmd , options , argv ) {
2019-03-29 14:08:08 +00:00
// Silence output when using --quiet
options . build = options . build || { }
if ( argv . quiet ) {
options . build . quiet = true
}
}
} ,
2018-11-07 23:37:06 +00:00
modern : {
... common . modern ,
description : 'Generate app in modern build (modern mode can be only client)' ,
2019-07-10 10:45:49 +00:00
prepare ( cmd , options , argv ) {
2018-11-22 15:48:26 +00:00
if ( normalizeArg ( argv . modern ) ) {
2018-11-07 23:37:06 +00:00
options . modern = 'client'
}
}
2019-03-11 19:55:54 +00:00
} ,
2020-07-16 17:32:09 +00:00
'force-build' : {
type : 'boolean' ,
default : false ,
description : 'Force to build the application with webpack'
} ,
2019-03-11 19:55:54 +00:00
'fail-on-error' : {
type : 'boolean' ,
default : false ,
description : 'Exit with non-zero status code if there are errors when generating pages'
2018-10-29 22:16:16 +00:00
}
} ,
2019-07-10 10:45:49 +00:00
async run ( cmd ) {
2020-07-16 15:10:54 +00:00
const config = await cmd . getNuxtConfig ( { dev : false } )
// Disable analyze if set by the nuxt config
config . build = config . build || { }
config . build . analyze = false
2019-02-07 15:00:41 +00:00
2020-07-16 15:10:54 +00:00
// Full static
2020-05-07 19:08:01 +00:00
if ( config . target === TARGETS . static ) {
2020-07-16 15:10:54 +00:00
await ensureBuild ( cmd )
await generate ( cmd )
return
2019-02-07 15:00:41 +00:00
}
2020-05-07 19:08:01 +00:00
// Forcing static target anyway
config . target = TARGETS . static
2020-10-15 16:12:06 +00:00
consola . warn ( ` For using \` nuxt generate \` , your have to set \` target: 'static' \` in your \` nuxt.config \` \n 👉 Learn more about it on https://go.nuxtjs.dev/static-target ` )
2020-05-07 19:08:01 +00:00
// Set flag to keep the prerendering behaviour
config . _legacyGenerate = true
2018-12-20 11:15:48 +00:00
const nuxt = await cmd . getNuxt ( config )
2019-03-03 08:12:46 +00:00
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
} ) )
} )
}
2018-12-20 11:15:48 +00:00
const generator = await cmd . getGenerator ( nuxt )
2020-06-25 14:48:04 +00:00
await nuxt . server . listen ( 0 )
2018-10-17 21:28:25 +00:00
2019-03-11 19:55:54 +00:00
const { errors } = await generator . generate ( {
2018-10-29 22:16:16 +00:00
init : true ,
2018-12-20 11:15:48 +00:00
build : cmd . argv . build
} )
2019-03-11 19:55:54 +00:00
2020-05-07 19:08:01 +00:00
await nuxt . close ( )
2019-03-11 19:55:54 +00:00
if ( cmd . argv [ 'fail-on-error' ] && errors . length > 0 ) {
throw new Error ( 'Error generating pages, exiting with non-zero code' )
}
2018-10-29 22:16:16 +00:00
}
2018-10-17 21:28:25 +00:00
}