diff --git a/packages/taro-cli/src/ui.js b/packages/taro-cli/src/ui.js index ed25e32785b8..9dfa042f93a5 100644 --- a/packages/taro-cli/src/ui.js +++ b/packages/taro-cli/src/ui.js @@ -21,8 +21,7 @@ const { BUILD_TYPES, REG_STYLE, REG_TYPESCRIPT, - cssImports, - debounce + cssImports } = require('./util') const appPath = process.cwd() @@ -35,8 +34,6 @@ const entryFilePath = resolveScriptPath(path.join(sourceDir, 'index')) const entryFileName = path.basename(entryFilePath) const tempDir = '.temp' const tempPath = path.join(appPath, tempDir) -const lingCompsDir = './node_modules/@ling-components/ling-components/dist/h5' -const lingCompsPath = path.join(appPath, lingCompsDir) const weappOutputName = 'weapp' const h5OutputName = 'h5' @@ -332,7 +329,14 @@ function buildEntry () { function watchFiles () { console.log('\n', chalk.gray('监听文件修改中...'), '\n') - const watchList = process.env.TARO_WATCH_MODE === 'atom' ? [sourceDir, lingCompsPath] : sourceDir + const watchList = [sourceDir] + + const uiConfig = projectConfig.ui || {} + const { extraWatchFiles = [] } = uiConfig + extraWatchFiles.forEach(item => { + watchList.push(path.join(appPath, item.path)) + if (typeof item.handler === 'function') item.callback = item.handler({ buildH5Script }) + }) const watcher = chokidar.watch(watchList, { ignored: /(^|[/\\])\../, @@ -370,38 +374,36 @@ function watchFiles () { } } - const _buildH5Script = debounce(buildH5Script, 1000) + function handleChange (filePath, type, tips) { + const relativePath = path.relative(appPath, filePath) + printLog(type, tips, relativePath) - watcher - .on('add', filePath => { - const relativePath = path.relative(appPath, filePath) - printLog(pocessTypeEnum.CREATE, '添加文件', relativePath) - if (relativePath.indexOf(lingCompsDir.substr(2)) > -1) { - // 玲珑基础库变更 - return _buildH5Script() - } - try { - syncWeappFile(filePath) - syncH5File(filePath) - } catch (err) { - console.log(err) - } - }) - .on('change', filePath => { - const relativePath = path.relative(appPath, filePath) - printLog(pocessTypeEnum.MODIFY, '文件变动', relativePath) - if (relativePath.indexOf(lingCompsDir.substr(2)) > -1) { - // 玲珑基础库变更 - return _buildH5Script() - } - try { - syncWeappFile(filePath) - syncH5File(filePath) - } catch (err) { - console.log(err) + let processed = false + extraWatchFiles.forEach(item => { + if (filePath.indexOf(item.path.substr(2)) < 0) return + if (typeof item.callback === 'function') { + item.callback() + processed = true } }) + if (processed) return + + try { + syncWeappFile(filePath) + syncH5File(filePath) + } catch (err) { + console.log(err) + } + } + + watcher + .on('add', filePath => handleChange(filePath, pocessTypeEnum.CREATE, '添加文件')) + .on('change', filePath => handleChange(filePath, pocessTypeEnum.MODIFY, '文件变动')) .on('unlink', filePath => { + for (const path in extraWatchFiles) { + if (filePath.indexOf(path.substr(2)) > -1) return + } + const relativePath = path.relative(appPath, filePath) printLog(pocessTypeEnum.UNLINK, '删除文件', relativePath) const weappOutputPath = path.join(appPath, outputDirName, weappOutputName) diff --git a/packages/taro-cli/src/util/index.js b/packages/taro-cli/src/util/index.js index 090f9a83270d..4b995e37ee4e 100644 --- a/packages/taro-cli/src/util/index.js +++ b/packages/taro-cli/src/util/index.js @@ -594,26 +594,3 @@ exports.getInstalledNpmPkgVersion = function (pkgName, basedir) { return null } } - -/** - * 防反跳 - * @author Secbone - * - * @param fn [Function] 需要防反跳的函数 - * @param wait [Number] 时间,毫秒 - * @return [Function] - */ -exports.debounce = function debounce (fn, wait) { - let timeout = null - - return function () { - const args = arguments - - if (timeout) clearTimeout(timeout) - - timeout = setTimeout(() => { - timeout = null - fn.apply(this, args) - }, wait) - } -}