Skip to content

Commit

Permalink
fix: add option to rewatch on path after raw fs event (#4717)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and pi0 committed Jan 9, 2019
1 parent 2e62fbe commit 9c6df49
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
57 changes: 45 additions & 12 deletions packages/builder/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,30 @@ export default class Builder {

// -- Loading indicator --
if (this.options.loadingIndicator.name) {
const indicatorPath1 = path.resolve(
let indicatorPath = path.resolve(
this.template.dir,
'views/loading',
this.options.loadingIndicator.name + '.html'
)
const indicatorPath2 = this.nuxt.resolver.resolveAlias(
this.options.loadingIndicator.name
)
const indicatorPath = fsExtra.existsSync(indicatorPath1)
? indicatorPath1
: fsExtra.existsSync(indicatorPath2) ? indicatorPath2 : null

let customIndicator = false
if (!fsExtra.existsSync(indicatorPath)) {
indicatorPath = this.nuxt.resolver.resolveAlias(
this.options.loadingIndicator.name
)

if (fsExtra.existsSync(indicatorPath)) {
customIndicator = true
} else {
indicatorPath = null
}
}

if (indicatorPath) {
templatesFiles.push({
src: indicatorPath,
dst: 'loading.html',
custom: customIndicator,
options: this.options.loadingIndicator
})
} else {
Expand Down Expand Up @@ -532,11 +541,17 @@ export default class Builder {
interpolate: /<%=([\s\S]+?)%>/g
}

// Add vue-app template dir to watchers
this.options.build.watch.push(this.template.dir)

// Interpret and move template files to .nuxt/
await Promise.all(
templatesFiles.map(async ({ src, dst, options, custom }) => {
// Add template to watchers
this.options.build.watch.push(src)
// Add custom templates to watcher
if (custom) {
this.options.build.watch.push(src)
}

// Render template to dst
const fileContent = await fsExtra.readFile(src, 'utf8')
let content
Expand Down Expand Up @@ -612,9 +627,27 @@ export default class Builder {
...Object.values(omit(this.options.build.styleResources, ['options']))
]).map(upath.normalizeSafe)

this.watchers.custom = chokidar
.watch(customPatterns, options)
.on('change', refreshFiles)
const watchCustom = (refresh) => {
if (refresh) refreshFiles()

this.watchers.custom = chokidar
.watch(customPatterns, options)
.on('change', refreshFiles)

const rewatchOnRawEvents = this.options.watchers.rewatchOnRawEvents
if (rewatchOnRawEvents && Array.isArray(rewatchOnRawEvents)) {
this.watchers.custom.on('raw', (_event, _path, opts) => {
if (rewatchOnRawEvents.includes(_event)) {
this.watchers.custom.close()
this.watchers.custom = null

watchCustom(true)
}
})
}
}

watchCustom()
}

watchRestart() {
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/config/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default () => ({
// Watch
watch: [],
watchers: {
rewatchOnRawEvents: env.linux ? ['rename'] : undefined,
webpack: {},
chokidar: {
ignoreInitial: true
Expand Down

0 comments on commit 9c6df49

Please sign in to comment.