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

refactor(easycom): add autoscan option

上级 62bd1f92
......@@ -334,7 +334,7 @@ let uniAutoImportComponents = []
let uniAutoImportScanComponents = []
function initAutoImportScanComponents () {
function initAutoImportScanComponents () {
const componentsPath = path.resolve(process.env.UNI_INPUT_DIR, 'components')
const components = {}
try {
......@@ -347,12 +347,22 @@ function initAutoImportScanComponents () {
})
} catch (e) {}
uniAutoImportScanComponents = parseUsingAutoImportComponents(components)
uniAutoImportScanComponents = parseUsingAutoImportComponents(components)
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 => {
autoImportComponents.push({
pattern: new RegExp(pattern),
replacement: usingAutoImportComponents[pattern]
})
const replacement = usingAutoImportComponents[pattern]
if (replacement && typeof replacement === 'string') {
autoImportComponents.push({
pattern: new RegExp(pattern),
replacement: replacement
})
}
})
}
return autoImportComponents
......@@ -432,7 +445,7 @@ module.exports = {
parsePagesJson,
pagesJsonJsFileName,
getAutoComponents,
initAutoImportComponents,
initAutoImportComponents,
initAutoImportScanComponents,
addPageUsingComponents,
getUsingComponentsCode,
......
......@@ -312,12 +312,15 @@ if (
}
const {
initAutoImportComponents,
initAutoImportComponents,
initAutoImportScanComponents
} = require('@dcloudio/uni-cli-shared/lib/pages')
initAutoImportScanComponents()
initAutoImportComponents(pagesJsonObj.easycom)
} = require('@dcloudio/uni-cli-shared/lib/pages')
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,7 +13,9 @@ 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) {
initAutoImportScanComponents()
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 放在一块读取设置
......@@ -128,4 +129,4 @@ module.exports = function (content) {
}
return ''
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册