From 769ded0ef4e756cf35661638ea9006dbff6cb959 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Mon, 23 Dec 2019 21:11:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(mini-runner):=20=E4=BF=AE=E5=A4=8D=20watch?= =?UTF-8?q?=20=E6=97=B6=E5=A2=9E=E5=8A=A0=E7=BB=84=E4=BB=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=20bug=EF=BC=8Cclose=20#5140?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/MiniPlugin.ts | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 3d4d4776217c..b8264d235e70 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -149,6 +149,7 @@ export default class MiniPlugin { isWatch: boolean errors: any[] changedFileType: PARSE_AST_TYPE | undefined + addedComponents: Set constructor (options = {}) { this.options = defaults(options || {}, { @@ -171,6 +172,7 @@ export default class MiniPlugin { this.quickappStyleFiles = new Set() this.isWatch = false this.errors = [] + this.addedComponents = new Set() } tryAsync = fn => async (arg, callback) => { @@ -214,6 +216,7 @@ export default class MiniPlugin { this.tryAsync(async compilation => { compilation.errors = compilation.errors.concat(this.errors) await this.generateMiniFiles(compilation) + this.addedComponents.clear() }) ) @@ -234,8 +237,8 @@ export default class MiniPlugin { } getChangedFiles (compiler) { - const { watchFileSystem } = compiler; - const watcher = watchFileSystem.watcher || watchFileSystem.wfs.watcher; + const { watchFileSystem } = compiler + const watcher = watchFileSystem.watcher || watchFileSystem.wfs.watcher return Object.keys(watcher.mtimes) } @@ -700,7 +703,6 @@ export default class MiniPlugin { } })) } - if (depComponents && depComponents.length) { depComponents.forEach(item => { const componentPath = resolveScriptPath(path.resolve(path.dirname(file.path), item.path)) @@ -710,6 +712,7 @@ export default class MiniPlugin { const isNative = this.isNativePageOrComponent(componentTempPath, fs.readFileSync(componentPath).toString()) const componentObj = { name: componentName, path: componentPath, isNative } this.components.add(componentObj) + this.addedComponents.add(componentObj) this.getComponents(compiler, new Set([componentObj]), false) } }) @@ -803,7 +806,11 @@ export default class MiniPlugin { source: () => quickappJSONStr } } - if (template && (!this.changedFile || this.changedFile === item || this.changedFileType === PARSE_AST_TYPE.ENTRY)) { + if (template && ( + !this.changedFile + || this.changedFile === item + || this.changedFileType === PARSE_AST_TYPE.ENTRY + || Array.from(this.addedComponents).some(component => component.path === item))) { compilation.assets[templatePath] = { size: () => template!.length, source: () => template @@ -919,6 +926,15 @@ export default class MiniPlugin { } else { if (!this.options.isBuildPlugin) { this.getComponents(compiler, new Set([obj]), this.changedFileType === PARSE_AST_TYPE.PAGE) + if (this.addedComponents.size) { + this.addedComponents.forEach(item => { + if (item.isNative) { + this.addEntry(compiler, item.path, item.name, PARSE_AST_TYPE.NORMAL) + } else { + this.addEntry(compiler, item.path, item.name, PARSE_AST_TYPE.COMPONENT) + } + }) + } } else { this.getPluginFiles(compiler) }