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

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

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