Nuxt/lib/build.js

553 lines
18 KiB
JavaScript
Raw Normal View History

2016-11-07 01:34:58 +00:00
'use strict'
2017-01-11 19:14:59 +00:00
import _ from 'lodash'
import chokidar from 'chokidar'
import fs from 'fs-extra'
import hash from 'hash-sum'
import pify from 'pify'
import webpack from 'webpack'
import PostCompilePlugin from 'post-compile-webpack-plugin'
2017-01-13 20:30:31 +00:00
import serialize from 'serialize-javascript'
2017-01-11 19:14:59 +00:00
import { createBundleRenderer } from 'vue-server-renderer'
2017-06-19 06:02:10 +00:00
import { join, resolve, basename, dirname, relative } from 'path'
import { isUrl, r, wp } from './utils'
2017-01-11 19:14:59 +00:00
import clientWebpackConfig from './webpack/client.config.js'
import serverWebpackConfig from './webpack/server.config.js'
2017-03-20 16:52:35 +00:00
const debug = require('debug')('nuxt:build')
2016-11-11 14:30:11 +00:00
const remove = pify(fs.remove)
const readFile = pify(fs.readFile)
2017-03-25 02:17:26 +00:00
const utimes = pify(fs.utimes)
2016-11-11 14:30:11 +00:00
const writeFile = pify(fs.writeFile)
const mkdirp = pify(fs.mkdirp)
2016-11-24 00:47:11 +00:00
const glob = pify(require('glob'))
2017-05-18 08:45:15 +00:00
2017-02-28 16:32:03 +00:00
let webpackStats = 'none'
2017-05-18 08:45:15 +00:00
debug.color = 2 // force green color
2016-11-07 01:34:58 +00:00
const defaults = {
2017-01-23 16:55:39 +00:00
analyze: false,
2017-05-05 15:53:21 +00:00
extractCSS: false,
2017-02-13 12:34:54 +00:00
publicPath: '/_nuxt/',
filenames: {
css: 'common.[chunkhash].css',
2017-03-25 23:52:39 +00:00
manifest: 'manifest.[hash].js',
2017-05-19 07:33:33 +00:00
vendor: 'vendor.bundle.[chunkhash].js',
app: 'nuxt.bundle.[chunkhash].js'
},
vendor: [],
2016-11-18 09:38:47 +00:00
plugins: [],
2016-11-22 23:47:31 +00:00
babel: {},
postcss: [],
templates: [],
watch: []
}
2017-06-11 19:04:59 +00:00
2016-11-22 23:47:31 +00:00
const defaultsPostcss = [
require('autoprefixer')({
browsers: ['last 3 versions']
})
]
2017-01-11 19:14:59 +00:00
export function options () {
// Defaults build options
2016-11-22 23:47:31 +00:00
let extraDefaults = {}
if (this.options.build && !Array.isArray(this.options.build.postcss)) extraDefaults.postcss = defaultsPostcss
this.options.build = _.defaultsDeep(this.options.build, defaults, extraDefaults)
2017-03-24 17:54:54 +00:00
/* istanbul ignore if */
2017-03-16 17:52:38 +00:00
if (this.dev && isUrl(this.options.build.publicPath)) {
this.options.build.publicPath = defaults.publicPath
2017-02-16 17:16:00 +00:00
}
}
export function production () {
2016-12-07 18:01:46 +00:00
// Production, create server-renderer
webpackStats = {
chunks: false,
children: false,
modules: false,
colors: true
}
const serverConfig = getWebpackServerConfig.call(this)
const bundlePath = join(serverConfig.output.path, 'server-bundle.json')
const manifestPath = join(serverConfig.output.path, 'client-manifest.json')
if (fs.existsSync(bundlePath) && fs.existsSync(manifestPath)) {
const bundle = fs.readFileSync(bundlePath, 'utf8')
const manifest = fs.readFileSync(manifestPath, 'utf8')
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
addAppTemplate.call(this)
}
2016-12-07 18:01:46 +00:00
}
export async function build () {
// Avoid calling this method multiple times
if (this._buildDone) {
return this
}
// If building
if (this._building) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.build())
}, 300)
})
}
this._building = true
// Wait for Nuxt.js to be ready
await this.ready()
// Check if pages dir exists and warn if not
this._nuxtPages = typeof this.createRoutes !== 'function'
2017-05-21 17:18:48 +00:00
if (this._nuxtPages) {
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
console.error('> No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?') // eslint-disable-line no-console
} else {
console.error('> Couldn\'t find a `pages` directory. Please create one under the project root') // eslint-disable-line no-console
}
process.exit(1)
}
}
2016-12-08 06:45:40 +00:00
debug(`App root: ${this.srcDir}`)
debug(`Generating ${this.buildDir} files...`)
2016-12-21 19:51:09 +00:00
// Create .nuxt/, .nuxt/components and .nuxt/dist folders
2017-05-23 23:52:48 +00:00
await remove(r(this.buildDir))
await mkdirp(r(this.buildDir, 'components'))
if (!this.dev) {
2017-05-23 23:52:48 +00:00
await mkdirp(r(this.buildDir, 'dist'))
2016-11-07 01:34:58 +00:00
}
// Generate routes and interpret the template files
await generateRoutesAndFiles.call(this)
2016-12-21 19:51:09 +00:00
// Generate .nuxt/dist/ files
await buildFiles.call(this)
// Flag to set that building is done
this._buildDone = true
2016-12-21 19:51:09 +00:00
return this
}
async function buildFiles () {
if (this.dev) {
2017-02-03 14:09:27 +00:00
debug('Adding webpack middleware...')
createWebpackMiddleware.call(this)
webpackWatchAndUpdate.call(this)
2017-05-21 17:18:48 +00:00
watchFiles.call(this)
} else {
debug('Building files...')
await webpackRunClient.call(this)
await webpackRunServer.call(this)
2017-02-21 18:22:57 +00:00
addAppTemplate.call(this)
}
}
function addAppTemplate () {
2017-06-20 08:07:04 +00:00
let templatePath = resolve(this.buildDir, 'dist', 'index.html')
2017-02-21 18:22:57 +00:00
if (fs.existsSync(templatePath)) {
this.appTemplate = _.template(fs.readFileSync(templatePath, 'utf8'), {
interpolate: /{{([\s\S]+?)}}/g
})
}
}
async function generateRoutesAndFiles () {
2017-05-18 08:45:15 +00:00
debug('Generating files...')
// -- Templates --
2016-11-07 01:34:58 +00:00
let templatesFiles = [
'App.vue',
'client.js',
'index.js',
2017-02-03 14:09:27 +00:00
'middleware.js',
2016-11-07 01:34:58 +00:00
'router.js',
'server.js',
'utils.js',
2017-01-27 22:10:02 +00:00
'components/nuxt-error.vue',
2016-12-16 16:45:47 +00:00
'components/nuxt-loading.vue',
'components/nuxt-child.js',
'components/nuxt-link.js',
'components/nuxt.vue'
2016-11-07 01:34:58 +00:00
]
2017-06-19 06:02:10 +00:00
const srcImportDir = relative(this.buildDir, this.srcDir)
2017-05-18 08:45:15 +00:00
const templateVars = {
options: this.options,
2016-11-20 11:23:48 +00:00
uniqBy: _.uniqBy,
isDev: this.dev,
2016-11-10 16:16:37 +00:00
router: {
2017-02-17 09:24:30 +00:00
mode: this.options.router.mode,
2016-11-10 16:16:37 +00:00
base: this.options.router.base,
2017-02-03 14:09:27 +00:00
middleware: this.options.router.middleware,
2017-01-26 14:56:47 +00:00
linkActiveClass: this.options.router.linkActiveClass,
2017-05-21 17:18:48 +00:00
linkExactActiveClass: this.options.router.linkExactActiveClass,
2017-01-26 14:56:47 +00:00
scrollBehavior: this.options.router.scrollBehavior
2016-11-10 16:16:37 +00:00
},
2016-12-04 18:15:11 +00:00
env: this.options.env,
2016-11-14 22:59:54 +00:00
head: this.options.head,
middleware: fs.existsSync(join(this.srcDir, 'middleware')),
2017-05-18 08:45:15 +00:00
store: this.options.store || fs.existsSync(join(this.srcDir, 'store')),
css: this.options.css,
plugins: this.options.plugins.map((p, i) => {
if (typeof p === 'string') p = { src: p }
p.src = r(this.srcDir, p.src)
return { src: p.src, ssr: (p.ssr !== false), name: `plugin${i}` }
}),
appPath: './App.vue',
2017-05-18 08:45:15 +00:00
layouts: Object.assign({}, this.options.layouts),
2016-12-08 06:45:40 +00:00
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),
transition: this.options.transition,
2016-11-07 01:34:58 +00:00
components: {
2017-06-19 06:02:10 +00:00
ErrorPage: this.options.ErrorPage ? join(srcImportDir, this.options.ErrorPage) : null
2016-11-10 16:16:37 +00:00
}
2016-11-07 01:34:58 +00:00
}
2017-05-18 08:45:15 +00:00
// -- Layouts --
if (fs.existsSync(resolve(this.srcDir, 'layouts'))) {
const layoutsFiles = await glob('layouts/*.vue', {cwd: this.srcDir})
layoutsFiles.forEach((file) => {
let name = file.split('/').slice(-1)[0].replace('.vue', '')
if (name === 'error') return
2017-06-19 06:02:10 +00:00
templateVars.layouts[name] = join(srcImportDir, file)
2017-05-18 08:45:15 +00:00
})
if (layoutsFiles.includes('layouts/error.vue')) {
2017-06-19 06:02:10 +00:00
templateVars.components.ErrorPage = join(srcImportDir, 'layouts/error.vue')
2017-05-18 08:45:15 +00:00
}
}
// If no default layout, create its folder and add the default folder
if (!templateVars.layouts.default) {
2017-05-23 23:52:48 +00:00
await mkdirp(r(this.buildDir, 'layouts'))
2017-05-18 08:45:15 +00:00
templatesFiles.push('layouts/default.vue')
2017-06-19 06:02:10 +00:00
templateVars.layouts.default = join(relative(this.buildDir, __dirname), 'app', 'layouts', 'default.vue')
2017-05-18 08:45:15 +00:00
}
// -- Routes --
debug('Generating routes...')
2017-05-21 17:18:48 +00:00
// If user defined a custom method to create routes
if (this._nuxtPages) {
// Use nuxt.js createRoutes bases on pages/
2017-05-18 08:45:15 +00:00
const files = await glob('pages/**/*.vue', {cwd: this.srcDir})
2017-06-19 06:02:10 +00:00
templateVars.router.routes = createRoutes(files, srcImportDir)
} else {
2017-06-19 06:02:10 +00:00
templateVars.router.routes = this.createRoutes(srcImportDir)
2017-05-18 08:45:15 +00:00
}
2017-05-21 17:18:48 +00:00
// router.extendRoutes method
2017-01-18 16:25:38 +00:00
if (typeof this.options.router.extendRoutes === 'function') {
// let the user extend the routes
2017-05-21 17:18:48 +00:00
this.options.router.extendRoutes.call(this, templateVars.router.routes || [], r)
2017-01-18 16:25:38 +00:00
}
2017-05-18 08:45:15 +00:00
// Routes for generate command
2017-05-21 17:18:48 +00:00
this.routes = flatRoutes(templateVars.router.routes || [])
2017-05-18 08:45:15 +00:00
// -- Store --
2016-12-25 20:16:30 +00:00
// Add store if needed
if (this.options.store) {
templatesFiles.push('store.js')
}
2017-05-18 08:45:15 +00:00
// Resolve template files
2017-05-18 08:45:15 +00:00
const customTemplateFiles = this.options.build.templates.map(t => t.dst || basename(t.src || t))
templatesFiles = templatesFiles.map(file => {
2017-05-18 08:45:15 +00:00
// Skip if custom file was already provided in build.templates[]
2017-05-21 17:18:48 +00:00
if (customTemplateFiles.indexOf(file) !== -1) {
2017-05-18 08:45:15 +00:00
return
}
2017-05-21 17:18:48 +00:00
// Allow override templates using a file with same name in ${srcDir}/app
const customPath = r(this.srcDir, 'app', file)
const customFileExists = fs.existsSync(customPath)
return {
src: customFileExists ? customPath : r(__dirname, 'app', file),
dst: file,
custom: customFileExists
}
2017-05-18 08:45:15 +00:00
}).filter(i => !!i)
// -- Custom templates --
// Add custom template files
templatesFiles = templatesFiles.concat(this.options.build.templates.map(t => {
return Object.assign({
src: r(this.dir, t.src || t),
dst: t.dst || basename(t.src || t),
custom: true
}, t)
}))
// Interpret and move template files to .nuxt/
2017-05-21 17:18:48 +00:00
return Promise.all(templatesFiles.map(async ({ src, dst, options, custom }) => {
// Add template to watchers
this.options.build.watch.push(src)
// Render template to dst
2017-05-18 08:45:15 +00:00
const fileContent = await readFile(src, 'utf8')
const template = _.template(fileContent, {
imports: {
serialize,
hash,
r,
wp
2017-05-18 08:45:15 +00:00
}
2016-11-07 01:34:58 +00:00
})
2017-05-18 08:45:15 +00:00
const content = template(Object.assign({}, templateVars, {
options: options || {},
custom,
src,
dst
}))
2017-05-23 23:52:48 +00:00
const path = r(this.buildDir, dst)
2017-05-18 08:45:15 +00:00
// Ensure parent dir exits
await mkdirp(dirname(path))
// Write file
await writeFile(path, content, 'utf8')
// Fix webpack loop (https://github.com/webpack/watchpack/issues/25#issuecomment-287789288)
const dateFS = Date.now() / 1000 - 30
return utimes(path, dateFS, dateFS)
2017-05-18 08:47:39 +00:00
}))
2016-11-07 01:34:58 +00:00
}
2017-06-19 06:02:10 +00:00
function createRoutes (files, importDir) {
2016-12-11 00:46:04 +00:00
let routes = []
files.forEach((file) => {
let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
2017-06-19 06:02:10 +00:00
let route = { name: '', path: '', component: join(importDir, file) }
2016-12-11 00:46:04 +00:00
let parent = routes
keys.forEach((key, i) => {
2016-12-23 16:31:42 +00:00
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
route.name += (key === '_') ? 'all' : ''
2016-12-11 00:46:04 +00:00
let child = _.find(parent, { name: route.name })
if (child) {
if (!child.children) {
child.children = []
}
parent = child.children
2016-12-23 16:31:42 +00:00
route.path = ''
2016-12-11 00:46:04 +00:00
} else {
2016-12-23 16:31:42 +00:00
if (key === 'index' && (i + 1) === keys.length) {
route.path += (i > 0 ? '' : '/')
} else {
route.path += '/' + (key === '_' ? '*' : key.replace('_', ':'))
if (key !== '_' && key.indexOf('_') !== -1) {
2016-12-23 16:31:42 +00:00
route.path += '?'
}
}
2016-12-11 00:46:04 +00:00
}
})
2016-12-11 15:40:49 +00:00
// Order Routes path
2016-12-23 14:43:01 +00:00
parent.push(route)
parent.sort((a, b) => {
2017-02-09 00:18:29 +00:00
if (!a.path.length || a.path === '/') { return -1 }
if (!b.path.length || b.path === '/') { return 1 }
var res = 0
var _a = a.path.split('/')
var _b = b.path.split('/')
for (var i = 0; i < _a.length; i++) {
if (res !== 0) { break }
var y = (_a[i].indexOf('*') > -1) ? 2 : (_a[i].indexOf(':') > -1 ? 1 : 0)
var z = (_b[i].indexOf('*') > -1) ? 2 : (_b[i].indexOf(':') > -1 ? 1 : 0)
res = y - z
if (i === _b.length - 1 && res === 0) {
res = 1
}
}
return res === 0 ? -1 : res
2016-12-23 14:43:01 +00:00
})
2016-12-11 00:46:04 +00:00
})
return cleanChildrenRoutes(routes)
}
function cleanChildrenRoutes (routes, isChild = false) {
let start = -1
let routesIndex = []
2016-12-11 00:46:04 +00:00
routes.forEach((route) => {
if (/-index$/.test(route.name) || route.name === 'index') {
// Save indexOf 'index' key in name
let res = route.name.split('-')
let s = res.indexOf('index')
start = (start === -1 || s < start) ? s : start
routesIndex.push(res)
2017-01-11 14:03:42 +00:00
}
})
routes.forEach((route) => {
route.path = (isChild) ? route.path.replace('/', '') : route.path
if (route.path.indexOf('?') > -1) {
let names = route.name.split('-')
let paths = route.path.split('/')
if (!isChild) { paths.shift() } // clean first / for parents
routesIndex.forEach((r) => {
let i = r.indexOf('index') - start // children names
if (i < paths.length) {
for (var a = 0; a <= i; a++) {
if (a === i) { paths[a] = paths[a].replace('?', '') }
if (a < i && names[a] !== r[a]) { break }
}
}
})
route.path = (isChild ? '' : '/') + paths.join('/')
2016-12-20 16:30:43 +00:00
}
2016-12-23 16:31:42 +00:00
route.name = route.name.replace(/-index$/, '')
2016-12-11 00:46:04 +00:00
if (route.children) {
2017-01-11 11:52:39 +00:00
if (route.children.find((child) => child.path === '')) {
delete route.name
}
2016-12-11 00:46:04 +00:00
route.children = cleanChildrenRoutes(route.children, true)
}
})
return routes
}
function flatRoutes (router, path = '', routes = []) {
2017-02-21 12:01:16 +00:00
router.forEach((r) => {
if (!r.path.includes(':') && !r.path.includes('*')) {
if (r.children) {
flatRoutes(r.children, path + r.path + '/', routes)
} else {
routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path)
}
2017-02-21 12:01:16 +00:00
}
})
return routes
}
2016-11-07 01:34:58 +00:00
function getWebpackClientConfig () {
2016-12-09 17:54:17 +00:00
return clientWebpackConfig.call(this)
2016-11-07 01:34:58 +00:00
}
function getWebpackServerConfig () {
2016-12-09 17:54:17 +00:00
return serverWebpackConfig.call(this)
2016-11-07 01:34:58 +00:00
}
2017-02-03 14:09:27 +00:00
function createWebpackMiddleware () {
2016-11-07 01:34:58 +00:00
const clientConfig = getWebpackClientConfig.call(this)
const host = process.env.HOST || process.env.npm_package_config_nuxt_host || 'localhost'
2017-03-25 17:59:46 +00:00
const port = process.env.PORT || process.env.npm_package_config_nuxt_port || '3000'
2016-11-07 01:34:58 +00:00
// setup on the fly compilation + hot-reload
2016-12-27 13:53:36 +00:00
clientConfig.entry.app = _.flatten(['webpack-hot-middleware/client?reload=true', clientConfig.entry.app])
2016-11-07 01:34:58 +00:00
clientConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new PostCompilePlugin(stats => {
if (!stats.hasErrors() && !stats.hasWarnings()) {
// We don't use os.host() here because browsers have special behaviour with localhost
// For example chrome allows Geolocation api only to https or localhost origins
2017-06-10 18:42:13 +00:00
let _host = host === '0.0.0.0' ? 'localhost' : host
console.log(`> Open http://${_host}:${port}\n`) // eslint-disable-line no-console
}
})
2016-11-07 01:34:58 +00:00
)
const clientCompiler = webpack(clientConfig)
this.clientCompiler = clientCompiler
2017-02-03 14:09:27 +00:00
// Add the middleware to the instance context
2016-11-07 01:34:58 +00:00
this.webpackDevMiddleware = pify(require('webpack-dev-middleware')(clientCompiler, {
publicPath: clientConfig.output.publicPath,
stats: webpackStats,
quiet: true,
noInfo: true,
watchOptions: this.options.watchers.webpack
2016-11-07 01:34:58 +00:00
}))
this.webpackHotMiddleware = pify(require('webpack-hot-middleware')(clientCompiler, {
log: () => {}
}))
clientCompiler.plugin('done', () => {
const fs = this.webpackDevMiddleware.fileSystem
const filePath = join(clientConfig.output.path, 'index.html')
if (fs.existsSync(filePath)) {
const template = fs.readFileSync(filePath, 'utf-8')
this.appTemplate = _.template(template, {
interpolate: /{{([\s\S]+?)}}/g
})
}
this.watchHandler()
})
2016-11-07 01:34:58 +00:00
}
function webpackWatchAndUpdate () {
2016-11-07 01:34:58 +00:00
const MFS = require('memory-fs') // <- dependencies of webpack
const serverFS = new MFS()
const clientFS = this.clientCompiler.outputFileSystem
2016-11-07 01:34:58 +00:00
const serverConfig = getWebpackServerConfig.call(this)
const serverCompiler = webpack(serverConfig)
const bundlePath = join(serverConfig.output.path, 'server-bundle.json')
const manifestPath = join(serverConfig.output.path, 'client-manifest.json')
serverCompiler.outputFileSystem = serverFS
const watchHandler = (err) => {
2016-11-07 01:34:58 +00:00
if (err) throw err
const bundleExists = serverFS.existsSync(bundlePath)
const manifestExists = clientFS.existsSync(manifestPath)
2017-05-05 14:47:50 +00:00
if (bundleExists && manifestExists) {
const bundle = serverFS.readFileSync(bundlePath, 'utf8')
const manifest = clientFS.readFileSync(manifestPath, 'utf8')
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
}
}
this.watchHandler = watchHandler
this.webpackServerWatcher = serverCompiler.watch(this.options.watchers.webpack, watchHandler)
2016-11-07 01:34:58 +00:00
}
function webpackRunClient () {
return new Promise((resolve, reject) => {
const clientConfig = getWebpackClientConfig.call(this)
const clientCompiler = webpack(clientConfig)
clientCompiler.run((err, stats) => {
2016-11-07 01:34:58 +00:00
if (err) return reject(err)
console.log('[nuxt:build:client]\n', stats.toString(webpackStats)) // eslint-disable-line no-console
2017-03-20 16:52:35 +00:00
if (stats.hasErrors()) return reject(new Error('Webpack build exited with errors'))
2016-11-07 01:34:58 +00:00
resolve()
})
})
}
function webpackRunServer () {
return new Promise((resolve, reject) => {
const serverConfig = getWebpackServerConfig.call(this)
const serverCompiler = webpack(serverConfig)
serverCompiler.run((err, stats) => {
if (err) return reject(err)
console.log('[nuxt:build:server]\n', stats.toString(webpackStats)) // eslint-disable-line no-console
2017-03-20 16:52:35 +00:00
if (stats.hasErrors()) return reject(new Error('Webpack build exited with errors'))
2017-03-22 14:47:34 +00:00
const bundlePath = join(serverConfig.output.path, 'server-bundle.json')
const manifestPath = join(serverConfig.output.path, 'client-manifest.json')
2016-11-11 14:30:11 +00:00
readFile(bundlePath, 'utf8')
2017-05-05 14:47:50 +00:00
.then(bundle => {
readFile(manifestPath, 'utf8')
.then(manifest => {
createRenderer.call(this, JSON.parse(bundle), JSON.parse(manifest))
resolve()
2017-05-05 14:47:50 +00:00
})
})
2016-11-07 01:34:58 +00:00
})
})
}
function createRenderer (bundle, manifest) {
2016-11-07 01:34:58 +00:00
// Create bundle renderer to give a fresh context for every request
this.renderer = createBundleRenderer(bundle, Object.assign({
2017-05-02 06:57:39 +00:00
clientManifest: manifest,
2017-05-05 14:47:50 +00:00
runInNewContext: false,
basedir: this.dir
2017-05-05 14:47:50 +00:00
}, this.options.build.ssr))
2016-11-07 01:34:58 +00:00
this.renderToString = pify(this.renderer.renderToString)
this.renderToStream = this.renderer.renderToStream
}
2017-05-21 17:18:48 +00:00
function watchFiles () {
2016-12-24 13:15:12 +00:00
const patterns = [
r(this.srcDir, 'layouts'),
r(this.srcDir, 'store'),
r(this.srcDir, 'middleware'),
2016-12-24 13:15:12 +00:00
r(this.srcDir, 'layouts/*.vue'),
r(this.srcDir, 'layouts/**/*.vue')
]
2017-05-21 17:18:48 +00:00
if (this._nuxtPages) {
patterns.push(r(this.srcDir, 'pages'))
patterns.push(r(this.srcDir, 'pages/*.vue'))
patterns.push(r(this.srcDir, 'pages/**/*.vue'))
}
const options = Object.assign({}, this.options.watchers.chokidar, {
ignoreInitial: true
})
2016-12-21 19:51:09 +00:00
/* istanbul ignore next */
const refreshFiles = _.debounce(async () => {
await generateRoutesAndFiles.call(this)
}, 200)
// Watch for internals
2017-05-21 17:18:48 +00:00
this.filesWatcher = chokidar.watch(patterns, options)
.on('add', refreshFiles)
.on('unlink', refreshFiles)
// Watch for custom provided files
this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options)
2017-05-21 17:18:48 +00:00
.on('change', refreshFiles)
}