Skip to content

Commit

Permalink
fix(builder): only listen for file changes for supported extensions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and pi0 committed May 27, 2019
1 parent 7354d95 commit 1f2bf1c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 46 deletions.
27 changes: 13 additions & 14 deletions packages/builder/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ export default class Builder {
}
}

globPathWithExtensions(path) {
return `${path}/**/*.{${this.supportedExtensions.join(',')}}`
}

async generateRoutesAndFiles() {
consola.debug('Generating nuxt files')

Expand All @@ -229,7 +233,7 @@ export default class Builder {
await this.resolveLoadingIndicator(templateContext)

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

await this.compileTemplates(templateContext)

Expand Down Expand Up @@ -274,7 +278,7 @@ export default class Builder {
}

async resolveFiles(dir, cwd = this.options.srcDir) {
return this.ignore.filter(await glob(`${dir}/**/*.{${this.supportedExtensions.join(',')}}`, {
return this.ignore.filter(await glob(this.globPathWithExtensions(dir), {
cwd,
ignore: this.options.ignore
}))
Expand Down Expand Up @@ -568,6 +572,7 @@ export default class Builder {
watcher.on(event, listener)
}

// TODO: due to fixes in chokidar this isnt used anymore and could be removed in Nuxt v3
const { rewatchOnRawEvents } = this.options.watchers
if (rewatchOnRawEvents && Array.isArray(rewatchOnRawEvents)) {
watcher.on('raw', (_event) => {
Expand All @@ -592,26 +597,20 @@ export default class Builder {
}

watchClient() {
const src = this.options.srcDir
const rGlob = dir => ['*', '**/*'].map(glob => r(src, `${dir}/${glob}.{${this.supportedExtensions.join(',')}}`))

let patterns = [
r(src, this.options.dir.layouts),
r(src, this.options.dir.middleware),
...rGlob(this.options.dir.layouts)
r(this.options.srcDir, this.options.dir.layouts),
r(this.options.srcDir, this.options.dir.middleware)
]

if (this.options.store) {
patterns.push(r(src, this.options.dir.store))
patterns.push(r(this.options.srcDir, this.options.dir.store))
}

if (this._nuxtPages && !this._defaultPage) {
patterns.push(
r(src, this.options.dir.pages),
...rGlob(this.options.dir.pages)
)
patterns.push(r(this.options.srcDir, this.options.dir.pages))
}

patterns = patterns.map(upath.normalizeSafe)
patterns = patterns.map((path, ...args) => upath.normalizeSafe(this.globPathWithExtensions(path), ...args))

const refreshFiles = debounce(() => this.generateRoutesAndFiles(), 200)

Expand Down
2 changes: 1 addition & 1 deletion packages/builder/test/builder.generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('builder: builder generate', () => {
])
expect(builder.resolveCustomTemplates).toBeCalledTimes(1)
expect(builder.resolveLoadingIndicator).toBeCalledTimes(1)
expect(builder.options.build.watch).toEqual(['/var/nuxt/src/template'])
expect(builder.options.build.watch).toEqual(['/var/nuxt/src/template/**/*.{vue,js,ts,tsx}'])
expect(builder.compileTemplates).toBeCalledTimes(1)
expect(consola.success).toBeCalledTimes(1)
expect(consola.success).toBeCalledWith('Nuxt files generated')
Expand Down
42 changes: 21 additions & 21 deletions packages/builder/test/builder.watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ describe('builder: builder watch', () => {

const patterns = [
'/var/nuxt/src/layouts',
'/var/nuxt/src/middleware',
'/var/nuxt/src/layouts/*.{vue,js,ts,tsx}',
'/var/nuxt/src/layouts/**/*.{vue,js,ts,tsx}'
'/var/nuxt/src/middleware'
]

expect(r).toBeCalledTimes(4)
expect(r).nthCalledWith(1, '/var/nuxt/src', '/var/nuxt/src/layouts')
expect(r).nthCalledWith(2, '/var/nuxt/src', '/var/nuxt/src/middleware')
expect(r).nthCalledWith(3, '/var/nuxt/src', '/var/nuxt/src/layouts/*.{vue,js,ts,tsx}')
expect(r).nthCalledWith(4, '/var/nuxt/src', '/var/nuxt/src/layouts/**/*.{vue,js,ts,tsx}')
const globbedPatterns = [
'/var/nuxt/src/layouts/**/*.{vue,js,ts,tsx}',
'/var/nuxt/src/middleware/**/*.{vue,js,ts,tsx}'
]

expect(r).toBeCalledTimes(2)
expect(r).nthCalledWith(1, '/var/nuxt/src', patterns[0])
expect(r).nthCalledWith(2, '/var/nuxt/src', patterns[1])

expect(upath.normalizeSafe).toBeCalledTimes(4)
expect(upath.normalizeSafe).nthCalledWith(1, '/var/nuxt/src/layouts', 0, patterns)
expect(upath.normalizeSafe).nthCalledWith(2, '/var/nuxt/src/middleware', 1, patterns)
expect(upath.normalizeSafe).nthCalledWith(3, '/var/nuxt/src/layouts/*.{vue,js,ts,tsx}', 2, patterns)
expect(upath.normalizeSafe).nthCalledWith(4, '/var/nuxt/src/layouts/**/*.{vue,js,ts,tsx}', 3, patterns)
expect(upath.normalizeSafe).toBeCalledTimes(2)
expect(upath.normalizeSafe).nthCalledWith(1, globbedPatterns[0], 0, patterns)
expect(upath.normalizeSafe).nthCalledWith(2, globbedPatterns[1], 1, patterns)

expect(builder.createFileWatcher).toBeCalledTimes(1)
expect(builder.createFileWatcher).toBeCalledWith(patterns, ['add', 'unlink'], expect.any(Function), expect.any(Function))
expect(builder.createFileWatcher).toBeCalledWith(globbedPatterns, ['add', 'unlink'], expect.any(Function), expect.any(Function))
expect(builder.assignWatcher).toBeCalledTimes(1)
})

Expand All @@ -83,8 +82,8 @@ describe('builder: builder watch', () => {

builder.watchClient()

expect(r).toBeCalledTimes(5)
expect(r).nthCalledWith(5, '/var/nuxt/src', '/var/nuxt/src/store')
expect(r).toBeCalledTimes(3)
expect(r).nthCalledWith(3, '/var/nuxt/src', '/var/nuxt/src/store')
})

test('should NOT watch pages files on client if _defaultPage=true', () => {
Expand All @@ -108,7 +107,7 @@ describe('builder: builder watch', () => {

builder.watchClient()

expect(r).toBeCalledTimes(4)
expect(r).toBeCalledTimes(2)
})
test('should watch pages files', () => {
const nuxt = createNuxt()
Expand All @@ -130,10 +129,11 @@ describe('builder: builder watch', () => {

builder.watchClient()

expect(r).toBeCalledTimes(7)
expect(r).nthCalledWith(5, '/var/nuxt/src', '/var/nuxt/src/pages')
expect(r).nthCalledWith(6, '/var/nuxt/src', '/var/nuxt/src/pages/*.{vue,js,ts,tsx}')
expect(r).nthCalledWith(7, '/var/nuxt/src', '/var/nuxt/src/pages/**/*.{vue,js,ts,tsx}')
expect(r).toBeCalledTimes(3)
expect(r).nthCalledWith(3, '/var/nuxt/src', '/var/nuxt/src/pages')

expect(upath.normalizeSafe).toBeCalledTimes(3)
expect(upath.normalizeSafe).nthCalledWith(3, '/var/nuxt/src/pages/**/*.{vue,js,ts,tsx}', 2, expect.any(Array))
})

test('should invoke generateRoutesAndFiles on file refresh', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/config/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default () => ({
// Watch
watch: [],
watchers: {
rewatchOnRawEvents: env.linux ? ['rename'] : undefined,
rewatchOnRawEvents: undefined,
webpack: {},
chokidar: {
ignoreInitial: true
Expand Down
4 changes: 1 addition & 3 deletions packages/config/test/__snapshots__/options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,7 @@ Object {
"chokidar": Object {
"ignoreInitial": true,
},
"rewatchOnRawEvents": Array [
"rename",
],
"rewatchOnRawEvents": undefined,
"webpack": Object {},
},
}
Expand Down
8 changes: 2 additions & 6 deletions packages/config/test/config/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ Object {
"chokidar": Object {
"ignoreInitial": true,
},
"rewatchOnRawEvents": Array [
"rename",
],
"rewatchOnRawEvents": undefined,
"webpack": Object {},
},
}
Expand Down Expand Up @@ -662,9 +660,7 @@ Object {
"chokidar": Object {
"ignoreInitial": true,
},
"rewatchOnRawEvents": Array [
"rename",
],
"rewatchOnRawEvents": undefined,
"webpack": Object {},
},
}
Expand Down

0 comments on commit 1f2bf1c

Please sign in to comment.