Skip to content

Commit

Permalink
feat(taro-cli): 增加对 config.ui 配置项的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj authored and luckyadam committed Dec 29, 2018
1 parent 9e28464 commit 1b40313
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 56 deletions.
68 changes: 35 additions & 33 deletions packages/taro-cli/src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const {
BUILD_TYPES,
REG_STYLE,
REG_TYPESCRIPT,
cssImports,
debounce
cssImports
} = require('./util')

const appPath = process.cwd()
Expand All @@ -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'
Expand Down Expand Up @@ -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: /(^|[/\\])\../,
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 0 additions & 23 deletions packages/taro-cli/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 1b40313

Please sign in to comment.