mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 01:17:16 +00:00
fix(cli): restart issues (#299)
This commit is contained in:
parent
fb08be37e2
commit
e70978d1c0
@ -4,57 +4,42 @@ import debounce from 'debounce-promise'
|
|||||||
import { createServer, createLoadingHandler } from '../utils/server'
|
import { createServer, createLoadingHandler } from '../utils/server'
|
||||||
import { showBanner } from '../utils/banner'
|
import { showBanner } from '../utils/banner'
|
||||||
import { requireModule } from '../utils/cjs'
|
import { requireModule } from '../utils/cjs'
|
||||||
import { error, info } from '../utils/log'
|
import { error } from '../utils/log'
|
||||||
import { diff, printDiff } from '../utils/diff'
|
|
||||||
|
|
||||||
export async function invoke (args) {
|
export async function invoke (args) {
|
||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development'
|
||||||
const server = createServer()
|
const server = createServer()
|
||||||
const listener = await server.listen({ clipboard: args.clipboard, open: args.open || args.o })
|
const listener = await server.listen({
|
||||||
|
clipboard: args.clipboard,
|
||||||
|
open: args.open || args.o
|
||||||
|
})
|
||||||
|
|
||||||
const rootDir = resolve(args._[0] || '.')
|
const rootDir = resolve(args._[0] || '.')
|
||||||
|
|
||||||
const { loadNuxt, buildNuxt } = requireModule('@nuxt/kit', rootDir)
|
const { loadNuxt, buildNuxt } = requireModule('@nuxt/kit', rootDir)
|
||||||
|
|
||||||
let currentNuxt
|
let currentNuxt
|
||||||
const load = async () => {
|
const load = async (isRestart) => {
|
||||||
try {
|
try {
|
||||||
showBanner(true)
|
const message = `${isRestart ? 'Restarting' : 'Starting'} nuxt...`
|
||||||
listener.showURL()
|
server.setApp(createLoadingHandler(message, 1))
|
||||||
|
if (isRestart) {
|
||||||
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
|
console.log(message)
|
||||||
|
}
|
||||||
if (process.env.DEBUG) {
|
|
||||||
let configChanges
|
|
||||||
if (currentNuxt) {
|
if (currentNuxt) {
|
||||||
configChanges = diff(currentNuxt.options, newNuxt.options, [
|
|
||||||
'generate.staticAssets.version',
|
|
||||||
'env.NITRO_PRESET'
|
|
||||||
])
|
|
||||||
server.setApp(createLoadingHandler('Restarting...', 1))
|
|
||||||
await currentNuxt.close()
|
await currentNuxt.close()
|
||||||
|
}
|
||||||
|
const newNuxt = await loadNuxt({ rootDir, dev: true, ready: false })
|
||||||
currentNuxt = newNuxt
|
currentNuxt = newNuxt
|
||||||
} else {
|
|
||||||
currentNuxt = newNuxt
|
|
||||||
}
|
|
||||||
|
|
||||||
if (configChanges) {
|
|
||||||
if (configChanges.length) {
|
|
||||||
info('Nuxt config updated:')
|
|
||||||
printDiff(configChanges)
|
|
||||||
} else {
|
|
||||||
info('Restarted nuxt due to config changes')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentNuxt = newNuxt
|
|
||||||
}
|
|
||||||
|
|
||||||
await currentNuxt.ready()
|
await currentNuxt.ready()
|
||||||
await buildNuxt(currentNuxt)
|
await buildNuxt(currentNuxt)
|
||||||
server.setApp(currentNuxt.server.app)
|
server.setApp(currentNuxt.server.app)
|
||||||
|
if (isRestart && args.clear !== false) {
|
||||||
|
showBanner()
|
||||||
|
listener.showURL()
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
error('Cannot load nuxt.', err)
|
error(`Cannot ${isRestart ? 'restart' : 'start'} nuxt: `, err)
|
||||||
server.setApp(createLoadingHandler(
|
server.setApp(createLoadingHandler(
|
||||||
'Error while loading nuxt. Please check console and fix errors.'
|
'Error while loading nuxt. Please check console and fix errors.'
|
||||||
))
|
))
|
||||||
@ -67,11 +52,11 @@ export async function invoke (args) {
|
|||||||
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
|
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
|
||||||
watcher.on('all', (_event, file) => {
|
watcher.on('all', (_event, file) => {
|
||||||
if (file.includes('nuxt.config') || file.includes('modules')) {
|
if (file.includes('nuxt.config') || file.includes('modules')) {
|
||||||
dLoad()
|
dLoad(true)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await load()
|
await load(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
|
@ -8,11 +8,15 @@ import { error } from './utils/log'
|
|||||||
|
|
||||||
async function _main () {
|
async function _main () {
|
||||||
const _argv = process.argv.slice(2)
|
const _argv = process.argv.slice(2)
|
||||||
const args = mri(_argv)
|
const args = mri(_argv, {
|
||||||
|
boolean: [
|
||||||
|
'no-clear'
|
||||||
|
]
|
||||||
|
})
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let command = args._.shift() || 'usage'
|
let command = args._.shift() || 'usage'
|
||||||
|
|
||||||
showBanner(command === 'dev')
|
showBanner(command === 'dev' && args.clear !== false)
|
||||||
|
|
||||||
if (!(command in commands)) {
|
if (!(command in commands)) {
|
||||||
console.log('\n' + red('Invalid command ' + command))
|
console.log('\n' + red('Invalid command ' + command))
|
||||||
|
@ -2,7 +2,7 @@ import clear from 'clear'
|
|||||||
import { green } from 'colorette'
|
import { green } from 'colorette'
|
||||||
import { version } from '../../package.json'
|
import { version } from '../../package.json'
|
||||||
|
|
||||||
export function showBanner (_clear: boolean) {
|
export function showBanner (_clear?: boolean) {
|
||||||
if (_clear) { clear() }
|
if (_clear) { clear() }
|
||||||
console.log(green(`Nuxt CLI v${version}`))
|
console.log(green(`Nuxt CLI v${version}`))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user