mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
chore: improve watcher and change ignorePrefix
This commit is contained in:
parent
c63091b68d
commit
e892f5f019
@ -1,11 +1,18 @@
|
|||||||
import { join } from 'path'
|
import { join, relative } from 'path'
|
||||||
import fsExtra from 'fs-extra'
|
import fsExtra from 'fs-extra'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
import { debounce } from 'lodash'
|
||||||
import { BundleBuilder } from 'src/webpack'
|
import { BundleBuilder } from 'src/webpack'
|
||||||
import { Nuxt } from '../core'
|
import { Nuxt } from '../core'
|
||||||
import { compileTemplates, scanTemplates, NuxtTemplate } from './template'
|
import {
|
||||||
|
templateData,
|
||||||
|
compileTemplates,
|
||||||
|
scanTemplates,
|
||||||
|
NuxtTemplate
|
||||||
|
} from './template'
|
||||||
import { createWatcher } from './watch'
|
import { createWatcher } from './watch'
|
||||||
import { resolveApp, NuxtApp } from './app'
|
import { resolveApp, NuxtApp } from './app'
|
||||||
|
import Ignore from './ignore'
|
||||||
|
|
||||||
export class Builder {
|
export class Builder {
|
||||||
nuxt: Nuxt
|
nuxt: Nuxt
|
||||||
@ -36,26 +43,34 @@ async function build (builder: Builder) {
|
|||||||
|
|
||||||
function watch (builder: Builder) {
|
function watch (builder: Builder) {
|
||||||
const { nuxt } = builder
|
const { nuxt } = builder
|
||||||
|
const ignore = new Ignore({
|
||||||
|
rootDir: nuxt.options.srcDir,
|
||||||
|
ignoreArray: nuxt.options.ignore.concat(
|
||||||
|
relative(nuxt.options.rootDir, nuxt.options.buildDir)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
// Watch internal templates
|
// Watch internal templates
|
||||||
const nuxtAppWatcher = createWatcher(nuxt.options.appDir)
|
const options = nuxt.options.watchers.chokidar
|
||||||
// nuxtAppWatcher.debug()
|
const nuxtAppWatcher = createWatcher(nuxt.options.appDir, options, ignore)
|
||||||
nuxtAppWatcher.watchAll(async () => {
|
nuxtAppWatcher.watchAll(async () => {
|
||||||
consola.log('Re-generate templates')
|
consola.log('Re-generate templates')
|
||||||
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Watch user app
|
// Watch user app
|
||||||
const appWatcher = createWatcher(builder.app.srcDir, {
|
const appWatcher = createWatcher(builder.app.srcDir, options, ignore)
|
||||||
ignored: [
|
// Watch for App.vue creation
|
||||||
nuxt.options.buildDir
|
// appWatcher.debug('srcDir')
|
||||||
]
|
appWatcher.watch(
|
||||||
})
|
/^(A|a)pp\.[a-z]{2,3}/,
|
||||||
// appWatcher.debug()
|
debounce(({ event }) => {
|
||||||
appWatcher.watch(/(A|a)pp\.[a-z]{2,3}/, async () => {
|
if (['add', 'unlink'].includes(event)) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 200))
|
generate(builder)
|
||||||
await generate(builder)
|
}
|
||||||
})
|
}, 50)
|
||||||
|
)
|
||||||
|
// Watch for page changes
|
||||||
appWatcher.watch('pages/', async () => {
|
appWatcher.watch('pages/', async () => {
|
||||||
consola.log('Re-generate routes')
|
consola.log('Re-generate routes')
|
||||||
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
||||||
@ -69,13 +84,9 @@ export async function generate (builder: Builder) {
|
|||||||
builder.app = resolveApp(nuxt, nuxt.options.srcDir)
|
builder.app = resolveApp(nuxt, nuxt.options.srcDir)
|
||||||
|
|
||||||
const templatesDir = join(builder.nuxt.options.appDir, '_templates')
|
const templatesDir = join(builder.nuxt.options.appDir, '_templates')
|
||||||
const appTemplates = await scanTemplates(templatesDir, {
|
const appTemplates = await scanTemplates(templatesDir, templateData(builder))
|
||||||
app: builder.app
|
|
||||||
})
|
|
||||||
|
|
||||||
builder.templates = [
|
builder.templates = [...appTemplates]
|
||||||
...appTemplates
|
|
||||||
]
|
|
||||||
|
|
||||||
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
await compileTemplates(builder.templates, nuxt.options.buildDir)
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,10 @@ export default class Ignore {
|
|||||||
return paths
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ignores (pathname: string) {
|
||||||
|
return this.ignore && this.ignore.ignores(pathname)
|
||||||
|
}
|
||||||
|
|
||||||
reload () {
|
reload () {
|
||||||
delete this.ignore
|
delete this.ignore
|
||||||
delete this.ignoreFile
|
delete this.ignoreFile
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { join, relative, dirname } from 'path'
|
import { join, relative, dirname } from 'path'
|
||||||
import fsExtra from 'fs-extra'
|
import fsExtra from 'fs-extra'
|
||||||
import globby from 'globby'
|
import globby from 'globby'
|
||||||
import consola from 'consola'
|
|
||||||
import lodashTemplate from 'lodash/template'
|
import lodashTemplate from 'lodash/template'
|
||||||
|
|
||||||
export interface NuxtTemplate {
|
export interface NuxtTemplate {
|
||||||
@ -10,11 +9,17 @@ export interface NuxtTemplate {
|
|||||||
data?: any
|
data?: any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function templateData (builder) {
|
||||||
|
return {
|
||||||
|
app: builder.app
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function compileTemplate ({ src, path, data }: NuxtTemplate, destDir: string) {
|
async function compileTemplate ({ src, path, data }: NuxtTemplate, destDir: string) {
|
||||||
const srcContents = await fsExtra.readFile(src, 'utf-8')
|
const srcContents = await fsExtra.readFile(src, 'utf-8')
|
||||||
const compiledSrc = lodashTemplate(srcContents, {})(data)
|
const compiledSrc = lodashTemplate(srcContents, {})(data)
|
||||||
const dest = join(destDir, path)
|
const dest = join(destDir, path)
|
||||||
consola.log('Compile template', dest)
|
// consola.log('Compile template', dest)
|
||||||
await fsExtra.mkdirp(dirname(dest))
|
await fsExtra.mkdirp(dirname(dest))
|
||||||
await fsExtra.writeFile(dest, compiledSrc)
|
await fsExtra.writeFile(dest, compiledSrc)
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,23 @@
|
|||||||
import { relative } from 'path'
|
|
||||||
import chokidar, { WatchOptions } from 'chokidar'
|
import chokidar, { WatchOptions } from 'chokidar'
|
||||||
|
import defu from 'defu'
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
|
import Ignore from './ignore'
|
||||||
|
|
||||||
export function createWatcher (dir: string, options?: WatchOptions) {
|
export function createWatcher (
|
||||||
const watcher = chokidar.watch(dir, {
|
dir: string,
|
||||||
|
options?: WatchOptions,
|
||||||
|
ignore?: Ignore
|
||||||
|
) {
|
||||||
|
const opts = defu({ cwd: dir }, options, {
|
||||||
ignored: [],
|
ignored: [],
|
||||||
ignoreInitial: true,
|
ignoreInitial: true
|
||||||
...options
|
|
||||||
})
|
})
|
||||||
|
const watcher = chokidar.watch(dir, opts)
|
||||||
const watchAll = (cb: Function, filter?: Function) => {
|
const watchAll = (cb: Function, filter?: Function) => {
|
||||||
watcher.on('raw', (event, path: string, _details) => {
|
watcher.on('all', (event, path: string) => {
|
||||||
if (options.ignored.find(ignore => path.match(ignore))) {
|
if (ignore && ignore.ignores(path)) {
|
||||||
return // 🖕 chokidar ignored option
|
return
|
||||||
}
|
}
|
||||||
path = relative(dir, path)
|
|
||||||
const _event = { event, path }
|
const _event = { event, path }
|
||||||
if (!filter || filter(_event)) {
|
if (!filter || filter(_event)) {
|
||||||
cb(_event)
|
cb(_event)
|
||||||
@ -22,7 +25,8 @@ export function createWatcher (dir: string, options?: WatchOptions) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const watch = (pattern: string| RegExp, cb: Function) => watchAll(cb, e => e.path.match(pattern))
|
const watch = (pattern: string | RegExp, cb: Function) =>
|
||||||
|
watchAll(cb, e => e.path.match(pattern))
|
||||||
|
|
||||||
const debug = (tag: string = '[Watcher]') => {
|
const debug = (tag: string = '[Watcher]') => {
|
||||||
consola.log(tag, 'Watching ', dir)
|
consola.log(tag, 'Watching ', dir)
|
||||||
|
@ -122,7 +122,6 @@ interface CommonConfiguration {
|
|||||||
styleExtensions: string[]
|
styleExtensions: string[]
|
||||||
watch: string[]
|
watch: string[]
|
||||||
watchers: {
|
watchers: {
|
||||||
rewatchOnRawEvents?: boolean
|
|
||||||
webpack: WebpackConfiguration['watchOptions']
|
webpack: WebpackConfiguration['watchOptions']
|
||||||
chokidar: ChokidarWatchOptions
|
chokidar: ChokidarWatchOptions
|
||||||
}
|
}
|
||||||
@ -189,7 +188,7 @@ export default (): CommonConfiguration => ({
|
|||||||
|
|
||||||
// Ignores
|
// Ignores
|
||||||
ignoreOptions: undefined,
|
ignoreOptions: undefined,
|
||||||
ignorePrefix: '-',
|
ignorePrefix: '_',
|
||||||
ignore: [
|
ignore: [
|
||||||
'**/*.test.*',
|
'**/*.test.*',
|
||||||
'**/*.spec.*'
|
'**/*.spec.*'
|
||||||
@ -198,7 +197,6 @@ export default (): CommonConfiguration => ({
|
|||||||
// Watch
|
// Watch
|
||||||
watch: [],
|
watch: [],
|
||||||
watchers: {
|
watchers: {
|
||||||
rewatchOnRawEvents: undefined,
|
|
||||||
webpack: {
|
webpack: {
|
||||||
aggregateTimeout: 1000
|
aggregateTimeout: 1000
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user