mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
86 lines
2.5 KiB
JavaScript
Executable File
86 lines
2.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
// Show logs
|
|
process.env.DEBUG = 'nuxt:*'
|
|
|
|
var _ = require('lodash')
|
|
var debug = require('debug')('nuxt:build')
|
|
debug.color = 2 // force green color
|
|
var fs = require('fs')
|
|
var Nuxt = require('../')
|
|
var chokidar = require('chokidar')
|
|
var resolve = require('path').resolve
|
|
var without = require('lodash').without
|
|
|
|
var nuxtConfigFileName = 'nuxt.config.js'
|
|
|
|
// --config-file option
|
|
var indexOfConfig = false
|
|
if (process.argv.indexOf('--config-file') !== -1) {
|
|
indexOfConfig = process.argv.indexOf('--config-file')
|
|
} else if (process.argv.indexOf('-c') !== -1) {
|
|
indexOfConfig = process.argv.indexOf('-c')
|
|
}
|
|
|
|
if (indexOfConfig !== false) {
|
|
nuxtConfigFileName = process.argv.slice(indexOfConfig)[1]
|
|
process.argv = without(process.argv, '--config-file', '-c', nuxtConfigFileName)
|
|
}
|
|
|
|
var rootDir = resolve(process.argv.slice(2)[0] || '.')
|
|
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
|
|
|
var options = {}
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
options = require(nuxtConfigFile)
|
|
}
|
|
if (typeof options.rootDir !== 'string') {
|
|
options.rootDir = rootDir
|
|
}
|
|
options.dev = true // Add hot reloading and watching changes
|
|
|
|
new Nuxt(options).then(nuxt => {
|
|
var server = new nuxt.Server(nuxt)
|
|
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
|
|
listenOnConfigChanges(nuxt, server)
|
|
|
|
nuxt.build()
|
|
.catch((err) => {
|
|
console.error(err) // eslint-disable-line no-console
|
|
process.exit(1)
|
|
})
|
|
})
|
|
|
|
function listenOnConfigChanges (nuxt, server) {
|
|
// Listen on nuxt.config.js changes
|
|
var build = _.debounce(() => {
|
|
debug('[nuxt.config.js] changed')
|
|
delete require.cache[nuxtConfigFile]
|
|
var options = {}
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
try {
|
|
options = require(nuxtConfigFile)
|
|
} catch (e) {
|
|
return console.error(e) // eslint-disable-line no-console
|
|
}
|
|
}
|
|
options.rootDir = rootDir
|
|
nuxt.close()
|
|
.then(() => {
|
|
nuxt.renderer = null
|
|
debug('Rebuilding the app...')
|
|
return (new Nuxt(options)).then(nuxt => nuxt.build())
|
|
})
|
|
.then((nuxt) => {
|
|
server.nuxt = nuxt
|
|
})
|
|
.catch((error) => {
|
|
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
|
process.exit(1)
|
|
})
|
|
}, 200)
|
|
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
|
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
|
|
.on('all', build)
|
|
}
|