Skip to content

Commit

Permalink
fix(mini-runner): 优化 watch 时文件编译速度
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 26, 2019
1 parent 17c1fd3 commit 161a418
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default class MiniPlugin {
tabBarIcons: Set<string>
isWatch: boolean
errors: any[]
changedFileType: PARSE_AST_TYPE | undefined

constructor (options = {}) {
this.options = defaults(options || {}, {
Expand Down Expand Up @@ -783,7 +784,7 @@ export default class MiniPlugin {
source: () => quickappJSONStr
}
}
if (template && (!this.changedFile || this.changedFile === item)) {
if (template && (!this.changedFile || this.changedFile === item || this.changedFileType === PARSE_AST_TYPE.ENTRY)) {
compilation.assets[templatePath] = {
size: () => template!.length,
source: () => template
Expand Down Expand Up @@ -860,12 +861,52 @@ export default class MiniPlugin {
this.isWatch = true
if (REG_SCRIPTS.test(changedFile)) {
this.changedFile = changedFile
this.components.forEach(component => {
if (component.path === changedFile) {
this.components.delete(component)
const { type, obj } = this.getChangedFileInfo(changedFile)
this.changedFileType = type
if (this.changedFileType === PARSE_AST_TYPE.ENTRY
|| this.changedFileType === PARSE_AST_TYPE.PAGE
|| this.changedFileType === PARSE_AST_TYPE.COMPONENT) {
this.components.forEach(component => {
if (component.path === changedFile) {
this.components.delete(component)
}
})
this.errors = []
if (this.changedFileType === PARSE_AST_TYPE.ENTRY) {
this.run(compiler)
} else {
if (!this.options.isBuildPlugin) {
this.getComponents(compiler, new Set([obj]), this.changedFileType === PARSE_AST_TYPE.PAGE)
} else {
this.getPluginFiles(compiler)
}
this.transferFileContent(compiler)
}
})
this.run(compiler)
}
}
}

getChangedFileInfo (filePath) {
let type
let obj
this.pages.forEach(page => {
if (page.path === filePath) {
type = PARSE_AST_TYPE.PAGE
obj = page
}
})
this.components.forEach(component => {
if (component.path === filePath) {
type = PARSE_AST_TYPE.COMPONENT
obj = component
}
})
if (filePath === this.appEntry) {
type = PARSE_AST_TYPE.ENTRY
}
return {
type,
obj
}
}

Expand Down

0 comments on commit 161a418

Please sign in to comment.