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

feat(cli): refresh auto components when pages.json changed

上级 6e9848f5
......@@ -328,6 +328,15 @@ function addPageUsingComponents (pagePath, usingComponents) {
// 存储自动组件
const autoComponentMap = {}
/**
* UNI_AUTO_COMPONENTS 被更新,重新刷新 map
*/
function refreshAutoComponentMap () {
Object.keys(autoComponentMap).forEach(name => {
addAutoComponent(name)
})
}
function addAutoComponent (name) {
const options = process.UNI_AUTO_COMPONENTS
const opt = options.find(opt => opt.pattern.test(name))
......@@ -355,6 +364,18 @@ function getAutoComponents (autoComponents) {
return components
}
function parseUsingAutoImportComponents (usingAutoImportComponents) {
const autoImportComponents = []
if (usingAutoImportComponents) {
Object.keys(usingAutoImportComponents).forEach(pattern => {
autoImportComponents.push({
pattern: new RegExp(pattern),
replacement: usingAutoImportComponents[pattern]
})
})
}
return autoImportComponents
}
module.exports = {
getMainEntry,
getNVueMainEntry,
......@@ -363,10 +384,12 @@ module.exports = {
getPagesJson,
parsePagesJson,
pagesJsonJsFileName,
getAutoComponents,
getAutoComponents,
refreshAutoComponentMap,
addPageUsingComponents,
getUsingComponentsCode,
generateUsingComponentsCode,
getGlobalUsingComponentsCode,
parseUsingAutoImportComponents,
generateGlobalUsingComponentsCode
}
......@@ -290,18 +290,6 @@ if (runByHBuilderX) {
}
}
}
// 组件自动导入配置
process.UNI_AUTO_COMPONENTS = []
const usingAutoImportComponents = pagesJsonObj.usingAutoImportComponents
if (usingAutoImportComponents) {
Object.keys(usingAutoImportComponents).forEach(pattern => {
process.UNI_AUTO_COMPONENTS.push({
pattern: new RegExp(pattern),
replacement: usingAutoImportComponents[pattern]
})
})
}
if (
process.env.UNI_USING_CACHE &&
......
......@@ -17,7 +17,9 @@ const {
} = require('@dcloudio/uni-cli-shared/lib/cache')
const {
pagesJsonJsFileName
pagesJsonJsFileName,
refreshAutoComponentMap,
parseUsingAutoImportComponents
} = require('@dcloudio/uni-cli-shared/lib/pages')
const parseStyle = require('./util').parseStyle
......@@ -31,6 +33,17 @@ function renameUsingComponents (jsonObj) {
return jsonObj
}
let lastUsingAutoImportComponentsJson = ''
function initAutoImportComponents (usingAutoImportComponents = {}) {
const newUsingAutoImportComponentsJson = JSON.stringify(usingAutoImportComponents)
if (newUsingAutoImportComponentsJson !== lastUsingAutoImportComponentsJson) {
lastUsingAutoImportComponentsJson = newUsingAutoImportComponentsJson
process.UNI_AUTO_COMPONENTS = parseUsingAutoImportComponents(usingAutoImportComponents)
refreshAutoComponentMap()
}
}
module.exports = function (content) {
this.cacheable && this.cacheable()
......@@ -53,6 +66,10 @@ module.exports = function (content) {
this.addDependency(file)
}
})
// 组件自动导入配置
initAutoImportComponents(pagesJson.usingAutoImportComponents)
// TODO 与 usingComponents 放在一块读取设置
if (manifestJson.transformPx === false) {
process.UNI_TRANSFORM_PX = false
......@@ -83,8 +100,8 @@ module.exports = function (content) {
if (jsonFile) {
if (jsonFile.name === 'define-pages.js') {
appConfigContent = jsonFile.content
} else {
// app-view 不需要生成 app-config-service.js,manifest.json
} else {
// app-view 不需要生成 app-config-service.js,manifest.json
!isAppView && this.emitFile(jsonFile.name, jsonFile.content)
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册