UNPKG

1.93 kBPlain TextView Raw
1#!/usr/bin/env node
2
3// Show logs
4process.env.DEBUG = 'nuxt:*'
5
6var _ = require('lodash')
7var debug = require('debug')('nuxt:build')
8debug.color = 2 // force green color
9var fs = require('fs')
10var Nuxt = require('../')
11var chokidar = require('chokidar')
12var resolve = require('path').resolve
13
14var rootDir = resolve(process.argv.slice(2)[0] || '.')
15var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
16
17var options = {}
18if (fs.existsSync(nuxtConfigFile)) {
19 options = require(nuxtConfigFile)
20}
21if (typeof options.rootDir !== 'string') {
22 options.rootDir = rootDir
23}
24options.dev = true // Add hot reloading and watching changes
25
26var nuxt = new Nuxt(options)
27nuxt.build()
28.then(() => {
29 var server = new nuxt.Server(nuxt)
30 .listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
31 listenOnConfigChanges(nuxt, server)
32})
33.catch((err) => {
34 console.error(err) // eslint-disable-line no-console
35 process.exit(1)
36})
37
38function listenOnConfigChanges (nuxt, server) {
39 // Listen on nuxt.config.js changes
40 var build = _.debounce(() => {
41 debug('[nuxt.config.js] changed')
42 delete require.cache[nuxtConfigFile]
43 var options = {}
44 if (fs.existsSync(nuxtConfigFile)) {
45 try {
46 options = require(nuxtConfigFile)
47 } catch (e) {
48 return console.error(e) // eslint-disable-line no-console
49 }
50 }
51 options.rootDir = rootDir
52 nuxt.close()
53 .then(() => {
54 nuxt.renderer = null
55 debug('Rebuilding the app...')
56 return new Nuxt(options).build()
57 })
58 .then((nuxt) => {
59 server.nuxt = nuxt
60 })
61 .catch((error) => {
62 console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
63 process.exit(1)
64 })
65 }, 200)
66 var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
67 chokidar.watch(nuxtConfigFile, { ignoreInitial: true })
68 .on('all', build)
69}