Skip to content

Commit

Permalink
fix(mini-runner): 修复 watch 时增加组件文件的 bug,close #5140
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 9267765 commit 769ded0
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default class MiniPlugin {
isWatch: boolean
errors: any[]
changedFileType: PARSE_AST_TYPE | undefined
addedComponents: Set<IComponent>

constructor (options = {}) {
this.options = defaults(options || {}, {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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()
})
)

Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 769ded0

Please sign in to comment.