[modules] Watch for template changes

`build.watch` option was added
This commit is contained in:
Pooya Parsa 2017-05-12 12:45:37 +04:30
parent 2c37811032
commit 12727d1165
2 changed files with 10 additions and 4 deletions

View File

@ -58,7 +58,8 @@ const defaults = {
plugins: [], plugins: [],
babel: {}, babel: {},
postcss: [], postcss: [],
templates: [] templates: [],
watch: []
} }
const defaultsLoaders = [ const defaultsLoaders = [
{ {
@ -512,7 +513,7 @@ function watchPages () {
r(this.srcDir, 'pages/**/*.vue'), r(this.srcDir, 'pages/**/*.vue'),
r(this.srcDir, 'layouts/*.vue'), r(this.srcDir, 'layouts/*.vue'),
r(this.srcDir, 'layouts/**/*.vue') r(this.srcDir, 'layouts/**/*.vue')
] ].concat(this.options.build.watch)
const options = Object.assign({}, this.options.watchers.chokidar, { const options = Object.assign({}, this.options.watchers.chokidar, {
ignoreInitial: true ignoreInitial: true
}) })
@ -520,7 +521,7 @@ function watchPages () {
const refreshFiles = _.debounce(() => { const refreshFiles = _.debounce(() => {
co(generateRoutesAndFiles.bind(this)) co(generateRoutesAndFiles.bind(this))
}, 200) }, 200)
this.pagesFilesWatcher = chokidar.watch(patterns, options) this.pagesFilesWatcher = chokidar.watch(_.uniq(patterns), options)
.on('add', refreshFiles) .on('add', refreshFiles)
.on('unlink', refreshFiles) .on('unlink', refreshFiles)
} }

View File

@ -34,7 +34,6 @@ class Module {
// Generate unique and human readable dst filename // Generate unique and human readable dst filename
const dst = template.fileName || const dst = template.fileName ||
(path.basename(srcPath.dir) + '.' + srcPath.name + '.' + hash(src) + '.' + srcPath.ext) (path.basename(srcPath.dir) + '.' + srcPath.name + '.' + hash(src) + '.' + srcPath.ext)
// Add to templates list // Add to templates list
const templateObj = { const templateObj = {
src, src,
@ -42,9 +41,15 @@ class Module {
options: template.options options: template.options
} }
this.options.build.templates.push(templateObj) this.options.build.templates.push(templateObj)
// Watch template for changes
this.addWatch(src)
return templateObj return templateObj
} }
addWatch (pattern) {
this.options.build.watch.push(pattern)
}
addPlugin (template) { addPlugin (template) {
const {dst} = this.addTemplate(template) const {dst} = this.addTemplate(template)
// Add to nuxt plugins // Add to nuxt plugins