fix: improve watching experience for generated files (#6257)

This commit is contained in:
Pooya Parsa 2019-08-21 23:34:04 +04:30 committed by GitHub
parent 6ac5544428
commit 81b92b6395
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 101 additions and 77 deletions

View File

@ -216,6 +216,10 @@ export default class Builder {
async generateRoutesAndFiles () { async generateRoutesAndFiles () {
consola.debug('Generating nuxt files') consola.debug('Generating nuxt files')
if (this.bundleBuilder) {
this.bundleBuilder.pauseWatch()
}
// Plugins // Plugins
this.plugins = Array.from(this.normalizePlugins()) this.plugins = Array.from(this.normalizePlugins())
@ -237,6 +241,10 @@ export default class Builder {
await this.compileTemplates(templateContext) await this.compileTemplates(templateContext)
if (this.bundleBuilder) {
this.bundleBuilder.resumeWatch()
}
consola.success('Nuxt files generated') consola.success('Nuxt files generated')
} }

View File

@ -3,6 +3,7 @@ import consola from 'consola'
import fsExtra from 'fs-extra' import fsExtra from 'fs-extra'
import semver from 'semver' import semver from 'semver'
import { r, waitFor } from '@nuxt/utils' import { r, waitFor } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
import Builder from '../src/builder' import Builder from '../src/builder'
import { createNuxt } from './__utils__' import { createNuxt } from './__utils__'
@ -12,6 +13,7 @@ jest.mock('semver')
jest.mock('hash-sum', () => src => `hash(${src})`) jest.mock('hash-sum', () => src => `hash(${src})`)
jest.mock('@nuxt/utils') jest.mock('@nuxt/utils')
jest.mock('../src/ignore') jest.mock('../src/ignore')
jest.mock('@nuxt/webpack')
describe('builder: builder build', () => { describe('builder: builder build', () => {
beforeAll(() => { beforeAll(() => {
@ -77,7 +79,7 @@ describe('builder: builder build', () => {
test('should prevent duplicate build in dev mode', async () => { test('should prevent duplicate build in dev mode', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.dev = true nuxt.options.dev = true
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._buildStatus = 3 builder._buildStatus = 3
waitFor.mockImplementationOnce(() => { waitFor.mockImplementationOnce(() => {
@ -96,7 +98,7 @@ describe('builder: builder build', () => {
test('should wait 1000ms and retry if building is in progress', async () => { test('should wait 1000ms and retry if building is in progress', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.dev = true nuxt.options.dev = true
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._buildStatus = 2 builder._buildStatus = 2
const buildReturn = await builder.build() const buildReturn = await builder.build()
@ -134,7 +136,7 @@ describe('builder: builder build', () => {
test('should throw error when validateTemplate failed', async () => { test('should throw error when validateTemplate failed', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.validatePages = jest.fn() builder.validatePages = jest.fn()
builder.validateTemplate = jest.fn(() => { builder.validateTemplate = jest.fn(() => {
throw new Error('validate failed') throw new Error('validate failed')
@ -154,7 +156,7 @@ describe('builder: builder build', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.srcDir = '/var/nuxt/src' nuxt.options.srcDir = '/var/nuxt/src'
nuxt.options.dir = { pages: '/var/nuxt/src/pages' } nuxt.options.dir = { pages: '/var/nuxt/src/pages' }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fsExtra.exists.mockReturnValue(false) fsExtra.exists.mockReturnValue(false)
await builder.validatePages() await builder.validatePages()
@ -175,7 +177,7 @@ describe('builder: builder build', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.srcDir = '/var/nuxt/src' nuxt.options.srcDir = '/var/nuxt/src'
nuxt.options.dir = { pages: '/var/nuxt/src/pages' } nuxt.options.dir = { pages: '/var/nuxt/src/pages' }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fsExtra.exists fsExtra.exists
.mockReturnValueOnce(false) .mockReturnValueOnce(false)
.mockReturnValueOnce(true) .mockReturnValueOnce(true)
@ -197,7 +199,7 @@ describe('builder: builder build', () => {
test('should pass validation if createRoutes is function', async () => { test('should pass validation if createRoutes is function', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.build.createRoutes = jest.fn() nuxt.options.build.createRoutes = jest.fn()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
await builder.validatePages() await builder.validatePages()
@ -209,7 +211,7 @@ describe('builder: builder build', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.srcDir = '/var/nuxt/src' nuxt.options.srcDir = '/var/nuxt/src'
nuxt.options.dir = { pages: '/var/nuxt/src/pages' } nuxt.options.dir = { pages: '/var/nuxt/src/pages' }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fsExtra.exists.mockReturnValueOnce(true) fsExtra.exists.mockReturnValueOnce(true)
await builder.validatePages() await builder.validatePages()
@ -230,7 +232,7 @@ describe('builder: builder build', () => {
nuxt: 'edge' nuxt: 'edge'
} }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
semver.satisfies semver.satisfies
.mockReturnValueOnce(true) .mockReturnValueOnce(true)
.mockReturnValueOnce(true) .mockReturnValueOnce(true)
@ -256,7 +258,7 @@ describe('builder: builder build', () => {
nuxt: 'edge' nuxt: 'edge'
} }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
semver.satisfies semver.satisfies
.mockReturnValueOnce(false) .mockReturnValueOnce(false)
nuxt.resolver.requireModule nuxt.resolver.requireModule

View File

@ -4,6 +4,7 @@ import fs from 'fs-extra'
import consola from 'consola' import consola from 'consola'
import template from 'lodash/template' import template from 'lodash/template'
import { r, createRoutes, stripWhitespace } from '@nuxt/utils' import { r, createRoutes, stripWhitespace } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
import Builder from '../src/builder' import Builder from '../src/builder'
import TemplateContext from '../src/context/template' import TemplateContext from '../src/context/template'
import { createNuxt } from './__utils__' import { createNuxt } from './__utils__'
@ -17,6 +18,7 @@ jest.mock('../src/context/template', () => jest.fn())
jest.mock('../src/ignore', () => function () { jest.mock('../src/ignore', () => function () {
this.filter = jest.fn(files => files) this.filter = jest.fn(files => files)
}) })
jest.mock('@nuxt/webpack')
describe('builder: builder generate', () => { describe('builder: builder generate', () => {
beforeAll(() => { beforeAll(() => {
@ -45,7 +47,7 @@ describe('builder: builder generate', () => {
}, },
watch: [] watch: []
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin', src: '/var/somesrc' }]) builder.normalizePlugins = jest.fn(() => [{ name: 'test_plugin', src: '/var/somesrc' }])
builder.resolveLayouts = jest.fn(() => 'resolveLayouts') builder.resolveLayouts = jest.fn(() => 'resolveLayouts')
builder.resolveRoutes = jest.fn(() => 'resolveRoutes') builder.resolveRoutes = jest.fn(() => 'resolveRoutes')
@ -88,7 +90,7 @@ describe('builder: builder generate', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.srcDir = '/var/nuxt/src' nuxt.options.srcDir = '/var/nuxt/src'
nuxt.options.ignore = '/var/nuxt/ignore' nuxt.options.ignore = '/var/nuxt/ignore'
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
Glob.mockReturnValue('matched files') Glob.mockReturnValue('matched files')
const files = await builder.resolveFiles('/var/nuxt/dir') const files = await builder.resolveFiles('/var/nuxt/dir')
@ -105,7 +107,7 @@ describe('builder: builder generate', () => {
test('should resolve relative files', async () => { test('should resolve relative files', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveFiles = jest.fn(dir => [ `${dir}/foo.vue`, `${dir}/bar.vue`, `${dir}/baz.vue` ]) builder.resolveFiles = jest.fn(dir => [ `${dir}/foo.vue`, `${dir}/bar.vue`, `${dir}/baz.vue` ])
const files = await builder.resolveRelative('/var/nuxt/dir') const files = await builder.resolveRelative('/var/nuxt/dir')
@ -125,7 +127,7 @@ describe('builder: builder generate', () => {
nuxt.options.dir = { nuxt.options.dir = {
store: '/var/nuxt/src/store' store: '/var/nuxt/src/store'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveRelative = jest.fn(dir => [ builder.resolveRelative = jest.fn(dir => [
{ src: `${dir}/index.js` }, { src: `${dir}/index.js` },
{ src: `${dir}/bar.js` }, { src: `${dir}/bar.js` },
@ -155,7 +157,7 @@ describe('builder: builder generate', () => {
nuxt.options.dir = { nuxt.options.dir = {
store: '/var/nuxt/src/store' store: '/var/nuxt/src/store'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
const templateVars = {} const templateVars = {}
const templateFiles = [] const templateFiles = []
@ -171,7 +173,7 @@ describe('builder: builder generate', () => {
nuxt.options.dir = { nuxt.options.dir = {
middleware: '/var/nuxt/src/middleware' middleware: '/var/nuxt/src/middleware'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveRelative = jest.fn(dir => [ builder.resolveRelative = jest.fn(dir => [
{ src: `${dir}/midd.js` } { src: `${dir}/midd.js` }
]) ])
@ -193,7 +195,7 @@ describe('builder: builder generate', () => {
{ src: '/var/nuxt/templates/baz.js', dst: 'baz.js' } { src: '/var/nuxt/templates/baz.js', dst: 'baz.js' }
] ]
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fs.exists.mockReturnValueOnce(true) fs.exists.mockReturnValueOnce(true)
const templateContext = { const templateContext = {
@ -226,7 +228,7 @@ describe('builder: builder generate', () => {
nuxt.options.build = { nuxt.options.build = {
template: { dir: '/var/nuxt/templates' } template: { dir: '/var/nuxt/templates' }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fs.exists.mockReturnValueOnce(true) fs.exists.mockReturnValueOnce(true)
const templateFiles = [] const templateFiles = []
@ -252,7 +254,7 @@ describe('builder: builder generate', () => {
nuxt.options.build = { nuxt.options.build = {
template: { dir: '/var/nuxt/templates' } template: { dir: '/var/nuxt/templates' }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fs.exists fs.exists
.mockReturnValueOnce(false) .mockReturnValueOnce(false)
.mockReturnValueOnce(true) .mockReturnValueOnce(true)
@ -281,7 +283,7 @@ describe('builder: builder generate', () => {
nuxt.options.build = { nuxt.options.build = {
template: { dir: '/var/nuxt/templates' } template: { dir: '/var/nuxt/templates' }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
fs.exists fs.exists
.mockReturnValueOnce(false) .mockReturnValueOnce(false)
.mockReturnValueOnce(false) .mockReturnValueOnce(false)
@ -304,7 +306,7 @@ describe('builder: builder generate', () => {
nuxt.options.loadingIndicator = { nuxt.options.loadingIndicator = {
name: false name: false
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
await builder.resolveLoadingIndicator({ templateFiles: [] }) await builder.resolveLoadingIndicator({ templateFiles: [] })
@ -315,7 +317,7 @@ describe('builder: builder generate', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
nuxt.options.build.watch = [] nuxt.options.build.watch = []
nuxt.options.buildDir = '/var/nuxt/build' nuxt.options.buildDir = '/var/nuxt/build'
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.relativeToBuild = jest.fn() builder.relativeToBuild = jest.fn()
const templateFn = jest.fn(() => 'compiled content') const templateFn = jest.fn(() => 'compiled content')
template.mockImplementation(() => templateFn) template.mockImplementation(() => templateFn)
@ -385,7 +387,7 @@ describe('builder: builder generate', () => {
test('should throw error if compile failed', async () => { test('should throw error if compile failed', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.relativeToBuild = jest.fn() builder.relativeToBuild = jest.fn()
template.mockImplementation(() => { template.mockImplementation(() => {
throw new Error('compile failed') throw new Error('compile failed')
@ -412,7 +414,7 @@ describe('builder: builder generate', () => {
nuxt.options.layouts = { nuxt.options.layouts = {
foo: '/var/nuxt/layouts/foo/index.vue' foo: '/var/nuxt/layouts/foo/index.vue'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveFiles = jest.fn(layouts => [ builder.resolveFiles = jest.fn(layouts => [
`${layouts}/foo.vue`, `${layouts}/foo.vue`,
`${layouts}/bar.js`, `${layouts}/bar.js`,
@ -460,7 +462,7 @@ describe('builder: builder generate', () => {
nuxt.options.dir = { nuxt.options.dir = {
layouts: '/var/nuxt/src/layouts' layouts: '/var/nuxt/src/layouts'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveFiles = jest.fn(layouts => [ builder.resolveFiles = jest.fn(layouts => [
`${layouts}/error.vue` `${layouts}/error.vue`
]) ])
@ -487,7 +489,7 @@ describe('builder: builder generate', () => {
nuxt.options.dir = { nuxt.options.dir = {
layouts: '/var/nuxt/src/layouts' layouts: '/var/nuxt/src/layouts'
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.resolveFiles = jest.fn() builder.resolveFiles = jest.fn()
fs.exists.mockReturnValueOnce(false) fs.exists.mockReturnValueOnce(false)
@ -513,7 +515,7 @@ describe('builder: builder generate', () => {
nuxt.options.srcDir = '/var/nuxt/src' nuxt.options.srcDir = '/var/nuxt/src'
nuxt.options.build.createRoutes = jest.fn(() => [ { name: 'default_route' } ]) nuxt.options.build.createRoutes = jest.fn(() => [ { name: 'default_route' } ])
nuxt.options.router.extendRoutes = jest.fn(routes => [ ...routes, { name: 'extend_route' } ]) nuxt.options.router.extendRoutes = jest.fn(routes => [ ...routes, { name: 'extend_route' } ])
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
const templateVars = { const templateVars = {
router: { router: {
@ -550,7 +552,7 @@ describe('builder: builder generate', () => {
} }
nuxt.options.router.routeNameSplitter = '[splitter]' nuxt.options.router.routeNameSplitter = '[splitter]'
createRoutes.mockReturnValueOnce([ { name: 'default_route' } ]) createRoutes.mockReturnValueOnce([ { name: 'default_route' } ])
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._defaultPage = true builder._defaultPage = true
const templateVars = { const templateVars = {
@ -595,7 +597,7 @@ describe('builder: builder generate', () => {
extendRoutes: jest.fn() extendRoutes: jest.fn()
} }
createRoutes.mockImplementationOnce(({ files }) => files.map(file => ({ path: file }))) createRoutes.mockImplementationOnce(({ files }) => files.map(file => ({ path: file })))
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._nuxtPages = true builder._nuxtPages = true
builder.resolveFiles = jest.fn(dir => [ builder.resolveFiles = jest.fn(dir => [
`${dir}/foo.js`, `${dir}/foo.js`,

View File

@ -1,6 +1,7 @@
import Glob from 'glob' import Glob from 'glob'
import consola from 'consola' import consola from 'consola'
import { isIndexFileAndFolder } from '@nuxt/utils' import { isIndexFileAndFolder } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
import Builder from '../src/builder' import Builder from '../src/builder'
import { createNuxt } from './__utils__' import { createNuxt } from './__utils__'
@ -10,6 +11,7 @@ jest.mock('pify', () => fn => fn)
jest.mock('hash-sum', () => src => `hash(${src})`) jest.mock('hash-sum', () => src => `hash(${src})`)
jest.mock('@nuxt/utils') jest.mock('@nuxt/utils')
jest.mock('../src/ignore') jest.mock('../src/ignore')
jest.mock('@nuxt/webpack')
describe('builder: builder plugins', () => { describe('builder: builder plugins', () => {
beforeEach(() => { beforeEach(() => {
@ -24,7 +26,7 @@ describe('builder: builder plugins', () => {
{ src: '/var/nuxt/plugins/test.server', mode: 'server' }, { src: '/var/nuxt/plugins/test.server', mode: 'server' },
{ src: '/var/nuxt/plugins/test.client', ssr: false } { src: '/var/nuxt/plugins/test.client', ssr: false }
] ]
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
const plugins = builder.normalizePlugins() const plugins = builder.normalizePlugins()
@ -57,7 +59,7 @@ describe('builder: builder plugins', () => {
nuxt.options.plugins = [ nuxt.options.plugins = [
{ src: '/var/nuxt/plugins/test', mode: 'abc' } { src: '/var/nuxt/plugins/test', mode: 'abc' }
] ]
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
const plugins = builder.normalizePlugins() const plugins = builder.normalizePlugins()
@ -74,7 +76,7 @@ describe('builder: builder plugins', () => {
test('should resolve plugins', async () => { test('should resolve plugins', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [ builder.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' }, { src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client', mode: 'client' }, { src: '/var/nuxt/plugins/test.client', mode: 'client' },
@ -100,7 +102,7 @@ describe('builder: builder plugins', () => {
test('should throw error if plugin no existed', async () => { test('should throw error if plugin no existed', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [ builder.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' } { src: '/var/nuxt/plugins/test.js', mode: 'all' }
] ]
@ -111,7 +113,7 @@ describe('builder: builder plugins', () => {
test('should warn if there are multiple files and not index', async () => { test('should warn if there are multiple files and not index', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [ builder.plugins = [
{ src: '/var/nuxt/plugins/test', mode: 'all' } { src: '/var/nuxt/plugins/test', mode: 'all' }
] ]
@ -129,7 +131,7 @@ describe('builder: builder plugins', () => {
test('should detect plugin mode for client/server plugins', () => { test('should detect plugin mode for client/server plugins', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.options.plugins = [ builder.options.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' }, { src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client' }, { src: '/var/nuxt/plugins/test.client' },

View File

@ -4,8 +4,10 @@ import upath from 'upath'
import debounce from 'lodash/debounce' import debounce from 'lodash/debounce'
import { r, isString, isPureObject } from '@nuxt/utils' import { r, isString, isPureObject } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
import Builder from '../src/builder' import Builder from '../src/builder'
import { createNuxt } from './__utils__' import { createNuxt } from './__utils__'
jest.mock('@nuxt/webpack')
jest.mock('chokidar', () => ({ jest.mock('chokidar', () => ({
watch: jest.fn().mockReturnThis(), watch: jest.fn().mockReturnThis(),
@ -16,6 +18,7 @@ jest.mock('upath', () => ({ normalizeSafe: jest.fn(src => src) }))
jest.mock('lodash/debounce', () => jest.fn(fn => fn)) jest.mock('lodash/debounce', () => jest.fn(fn => fn))
jest.mock('@nuxt/utils') jest.mock('@nuxt/utils')
jest.mock('../src/ignore') jest.mock('../src/ignore')
jest.mock('@nuxt/webpack')
describe('builder: builder watch', () => { describe('builder: builder watch', () => {
beforeEach(() => { beforeEach(() => {
@ -33,7 +36,7 @@ describe('builder: builder watch', () => {
} }
nuxt.options.build.watch = [] nuxt.options.build.watch = []
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.createFileWatcher = jest.fn() builder.createFileWatcher = jest.fn()
builder.assignWatcher = jest.fn(() => () => {}) builder.assignWatcher = jest.fn(() => () => {})
r.mockImplementation((dir, src) => src) r.mockImplementation((dir, src) => src)
@ -75,7 +78,7 @@ describe('builder: builder watch', () => {
} }
nuxt.options.build.watch = [] nuxt.options.build.watch = []
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.createFileWatcher = jest.fn() builder.createFileWatcher = jest.fn()
builder.assignWatcher = jest.fn(() => () => {}) builder.assignWatcher = jest.fn(() => () => {})
r.mockImplementation((dir, src) => src) r.mockImplementation((dir, src) => src)
@ -100,7 +103,7 @@ describe('builder: builder watch', () => {
chokidar: { test: true } chokidar: { test: true }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._nuxtPages = true builder._nuxtPages = true
builder._defaultPage = true builder._defaultPage = true
r.mockImplementation((dir, src) => src) r.mockImplementation((dir, src) => src)
@ -123,7 +126,7 @@ describe('builder: builder watch', () => {
chokidar: { test: true } chokidar: { test: true }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder._nuxtPages = true builder._nuxtPages = true
r.mockImplementation((dir, src) => src) r.mockImplementation((dir, src) => src)
@ -149,7 +152,7 @@ describe('builder: builder watch', () => {
nuxt.options.watchers = { nuxt.options.watchers = {
chokidar: { test: true } chokidar: { test: true }
} }
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.watchCustom = jest.fn() builder.watchCustom = jest.fn()
r.mockImplementation((dir, src) => src) r.mockImplementation((dir, src) => src)
@ -179,7 +182,7 @@ describe('builder: builder watch', () => {
nuxt.options.build.styleResources = [ nuxt.options.build.styleResources = [
'/var/nuxt/src/style' '/var/nuxt/src/style'
] ]
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.createFileWatcher = jest.fn() builder.createFileWatcher = jest.fn()
builder.assignWatcher = jest.fn(() => () => {}) builder.assignWatcher = jest.fn(() => () => {})
builder.watchClient() builder.watchClient()
@ -212,7 +215,7 @@ describe('builder: builder watch', () => {
const listener = jest.fn() const listener = jest.fn()
const watcherCreatedCallback = jest.fn() const watcherCreatedCallback = jest.fn()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.createFileWatcher(patterns, events, listener, watcherCreatedCallback) builder.createFileWatcher(patterns, events, listener, watcherCreatedCallback)
expect(chokidar.watch).toBeCalledTimes(1) expect(chokidar.watch).toBeCalledTimes(1)
@ -242,7 +245,7 @@ describe('builder: builder watch', () => {
const listener = jest.fn() const listener = jest.fn()
const watcherCreatedCallback = jest.fn() const watcherCreatedCallback = jest.fn()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.createFileWatcher(patterns, events, listener, watcherCreatedCallback) builder.createFileWatcher(patterns, events, listener, watcherCreatedCallback)
expect(chokidar.on).toBeCalledTimes(2) expect(chokidar.on).toBeCalledTimes(2)
@ -277,7 +280,7 @@ describe('builder: builder watch', () => {
{ path: '/test', handler: '/var/nuxt/src/serverMiddleware/test-handler' }, { path: '/test', handler: '/var/nuxt/src/serverMiddleware/test-handler' },
{ obj: 'test' } { obj: 'test' }
] ]
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.ignore.ignoreFile = '/var/nuxt/src/.nuxtignore' builder.ignore.ignoreFile = '/var/nuxt/src/.nuxtignore'
isString.mockImplementation(src => typeof src === 'string') isString.mockImplementation(src => typeof src === 'string')
isPureObject.mockImplementation(obj => typeof obj === 'object') isPureObject.mockImplementation(obj => typeof obj === 'object')
@ -315,7 +318,7 @@ describe('builder: builder watch', () => {
'/var/nuxt/src/watch/test' '/var/nuxt/src/watch/test'
] ]
nuxt.options.serverMiddleware = [] nuxt.options.serverMiddleware = []
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.watchRestart() builder.watchRestart()
@ -350,7 +353,7 @@ describe('builder: builder watch', () => {
'/var/nuxt/src/watch/test' '/var/nuxt/src/watch/test'
] ]
nuxt.options.serverMiddleware = [] nuxt.options.serverMiddleware = []
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.watchRestart() builder.watchRestart()
@ -363,7 +366,7 @@ describe('builder: builder watch', () => {
test('should unwatch every watcher', () => { test('should unwatch every watcher', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
builder.watchers = { builder.watchers = {
files: { close: jest.fn() }, files: { close: jest.fn() },
custom: { close: jest.fn() }, custom: { close: jest.fn() },
@ -379,8 +382,7 @@ describe('builder: builder watch', () => {
test('should close watch and bundle builder', async () => { test('should close watch and bundle builder', async () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const bundleBuilderClose = jest.fn() const builder = new Builder(nuxt, BundleBuilder)
const builder = new Builder(nuxt, { close: bundleBuilderClose })
builder.unwatch = jest.fn() builder.unwatch = jest.fn()
expect(builder.__closed).toBeUndefined() expect(builder.__closed).toBeUndefined()
@ -389,7 +391,7 @@ describe('builder: builder watch', () => {
expect(builder.__closed).toEqual(true) expect(builder.__closed).toEqual(true)
expect(builder.unwatch).toBeCalledTimes(1) expect(builder.unwatch).toBeCalledTimes(1)
expect(bundleBuilderClose).toBeCalledTimes(1) expect(builder.bundleBuilder.close).toBeCalledTimes(1)
}) })
test('should close bundleBuilder only if close api exists', async () => { test('should close bundleBuilder only if close api exists', async () => {
@ -420,7 +422,7 @@ describe('builder: builder watch', () => {
test('should assign watcher with key', () => { test('should assign watcher with key', () => {
const nuxt = createNuxt() const nuxt = createNuxt()
const builder = new Builder(nuxt, {}) const builder = new Builder(nuxt, BundleBuilder)
const key = 'key' const key = 'key'
const watcher = 'watcher' const watcher = 'watcher'

View File

@ -68,7 +68,9 @@ export default () => ({
watch: [], watch: [],
watchers: { watchers: {
rewatchOnRawEvents: undefined, rewatchOnRawEvents: undefined,
webpack: {}, webpack: {
aggregateTimeout: 1000
},
chokidar: { chokidar: {
ignoreInitial: true ignoreInitial: true
} }

View File

@ -357,7 +357,9 @@ Object {
"ignoreInitial": true, "ignoreInitial": true,
}, },
"rewatchOnRawEvents": undefined, "rewatchOnRawEvents": undefined,
"webpack": Object {}, "webpack": Object {
"aggregateTimeout": 1000,
},
}, },
} }
`; `;

View File

@ -322,7 +322,9 @@ Object {
"ignoreInitial": true, "ignoreInitial": true,
}, },
"rewatchOnRawEvents": undefined, "rewatchOnRawEvents": undefined,
"webpack": Object {}, "webpack": Object {
"aggregateTimeout": 1000,
},
}, },
} }
`; `;
@ -649,7 +651,9 @@ Object {
"ignoreInitial": true, "ignoreInitial": true,
}, },
"rewatchOnRawEvents": undefined, "rewatchOnRawEvents": undefined,
"webpack": Object {}, "webpack": Object {
"aggregateTimeout": 1000,
},
}, },
} }
`; `;

View File

@ -168,35 +168,27 @@ export class WebpackBundler {
// Create webpack dev middleware // Create webpack dev middleware
this.devMiddleware[name] = pify( this.devMiddleware[name] = pify(
webpackDevMiddleware( webpackDevMiddleware(
compiler, compiler, {
Object.assign(
{
publicPath: buildOptions.publicPath, publicPath: buildOptions.publicPath,
stats: false, stats: false,
logLevel: 'silent', logLevel: 'silent',
watchOptions: this.buildContext.options.watchers.webpack watchOptions: this.buildContext.options.watchers.webpack,
}, ...buildOptions.devMiddleware
buildOptions.devMiddleware })
)
)
) )
this.devMiddleware[name].close = pify(this.devMiddleware[name].close) this.devMiddleware[name].close = pify(this.devMiddleware[name].close)
this.compilersWatching.push(this.devMiddleware[name].context.watching)
this.hotMiddleware[name] = pify( this.hotMiddleware[name] = pify(
webpackHotMiddleware( webpackHotMiddleware(
compiler, compiler, {
Object.assign(
{
log: false, log: false,
heartbeat: 10000 heartbeat: 10000,
}, path: `/__webpack_hmr/${name}`,
hotMiddlewareOptions, ...hotMiddlewareOptions
{ })
path: `/__webpack_hmr/${name}`
}
)
)
) )
// Register devMiddleware on server // Register devMiddleware on server
@ -221,6 +213,14 @@ export class WebpackBundler {
await Promise.all(this.compilersWatching.map(watching => watching.close())) await Promise.all(this.compilersWatching.map(watching => watching.close()))
} }
pauseWatch () {
this.compilersWatching.forEach(watching => watching.suspend())
}
resumeWatch () {
this.compilersWatching.forEach(watching => watching.resume())
}
async close () { async close () {
if (this.__closed) { if (this.__closed) {
return return