提交 90275821 编写于 作者: fxy060608's avatar fxy060608

refactor(easycom): add autoscan option

上级 62bd1f92
......@@ -352,7 +352,17 @@ function initAutoImportScanComponents () {
refreshAutoComponentMap()
}
function initAutoImportComponents (usingAutoImportComponents = {}) {
const _toString = Object.prototype.toString
function isPlainObject (obj) {
return _toString.call(obj) === '[object Object]'
}
function initAutoImportComponents (easycom = {}) {
let usingAutoImportComponents = easycom.custom || easycom || {}
if (!isPlainObject(usingAutoImportComponents)) {
usingAutoImportComponents = {}
}
// 目前仅 mp-weixin 内置支持 page-meta 等组件
if (process.env.UNI_PLATFORM !== 'mp-weixin') {
if (!usingAutoImportComponents['^page-meta$']) {
......@@ -415,10 +425,13 @@ function parseUsingAutoImportComponents (usingAutoImportComponents) {
const autoImportComponents = []
if (usingAutoImportComponents) {
Object.keys(usingAutoImportComponents).forEach(pattern => {
const replacement = usingAutoImportComponents[pattern]
if (replacement && typeof replacement === 'string') {
autoImportComponents.push({
pattern: new RegExp(pattern),
replacement: usingAutoImportComponents[pattern]
replacement: replacement
})
}
})
}
return autoImportComponents
......
......@@ -316,8 +316,11 @@ const {
initAutoImportScanComponents
} = require('@dcloudio/uni-cli-shared/lib/pages')
initAutoImportScanComponents()
process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJsonObj.easycom && pagesJsonObj.easycom.autoscan === false)
initAutoImportComponents(pagesJsonObj.easycom)
if (process.UNI_AUTO_SCAN_COMPONENTS) {
initAutoImportScanComponents()
}
runByHBuilderX && console.log(`正在编译中...`)
......
......@@ -13,9 +13,11 @@ class WebpackUniAppPlugin {
compiler.hooks.invalid.tap('webpack-uni-app-invalid', (fileName, changeTime) => {
if (fileName && typeof fileName === 'string') {
if (fileName.indexOf('.vue') !== -1 || fileName.indexOf('.nvue') !== -1) {
if (process.UNI_AUTO_SCAN_COMPONENTS) {
initAutoImportScanComponents()
}
}
}
})
}
}
......
......@@ -56,6 +56,7 @@ module.exports = function (content) {
})
// 组件自动导入配置
process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJson.easycom && pagesJson.easycom.autoscan === false)
initAutoImportComponents(pagesJson.easycom)
// TODO 与 usingComponents 放在一块读取设置
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册