Skip to content

Commit

Permalink
fix(mini-runner): 修复 watch bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 07134d7 commit c84b683
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 15 additions & 6 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import traverse from 'babel-traverse'
import { Config as IConfig, PageConfig } from '@tarojs/taro'
import * as _ from 'lodash'

import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework } from '../utils/constants'
import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework, REG_SCRIPTS } from '../utils/constants'
import { IComponentObj } from '../utils/types'
import { resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject, promoteRelativePath } from '../utils'
import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency'
Expand Down Expand Up @@ -136,6 +136,7 @@ export default class MiniPlugin {
context: string
appConfig: IConfig
pageConfigs: Map<string, PageConfig>
changedFile: string

constructor (options = {}) {
this.options = defaults(options || {}, {
Expand Down Expand Up @@ -176,8 +177,12 @@ export default class MiniPlugin {
compiler.hooks.watchRun.tapAsync(
PLUGIN_NAME,
this.tryAsync(async (compiler: webpack.Compiler) => {
const changedFile = this.getChangedFiles(compiler)
await this.watchRun(compiler, changedFile)
const changedFiles = this.getChangedFiles(compiler)
if (!changedFiles.length) {
await this.run(compiler)
} else {
await this.watchRun(compiler, changedFiles)
}
})
)

Expand Down Expand Up @@ -637,7 +642,7 @@ export default class MiniPlugin {
source: () => quickappJSONStr
}
}
if (template) {
if (template && (!this.changedFile || this.changedFile === item)) {
compilation.assets[templatePath] = {
size: () => template!.length,
source: () => template
Expand Down Expand Up @@ -681,8 +686,12 @@ export default class MiniPlugin {
this.transferFileContent(compiler)
}

watchRun (compiler: webpack.Compiler, changedFile: string[]) {

watchRun (compiler: webpack.Compiler, changedFiles: string[]) {
const changedFile = changedFiles[0]
if (REG_SCRIPTS.test(changedFile)) {
this.changedFile = changedFile
this.run(compiler)
}
}

getTargetFilePath (filePath, targetExtname) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path'
import VirtualStats from './virtualStats'
import webpack from 'webpack'

export default class VirtualModulePlugin {
options: {
Expand All @@ -11,7 +12,7 @@ export default class VirtualModulePlugin {
this.options = options
}

apply(compiler) {
apply (compiler: webpack.Compiler) {
const moduleName = this.options.moduleName
const ctime = VirtualModulePlugin.statsDate()
let modulePath = this.options.path
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class VirtualModulePlugin {
})
}

static populateFilesystem(options) {
static populateFilesystem (options) {
const fs = options.fs
const modulePath = options.modulePath
const contents = options.contents
Expand Down

0 comments on commit c84b683

Please sign in to comment.