Skip to content

Commit

Permalink
[fix]复杂slot依赖场景注入的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderXL committed May 29, 2024
1 parent 4d68a6c commit 573b44d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
23 changes: 13 additions & 10 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const stringifyLoadersAndResource = require('./utils/stringify-loaders-resource'
const emitFile = require('./utils/emit-file')
const { MPX_PROCESSED_FLAG, MPX_DISABLE_EXTRACTOR_CACHE } = require('./utils/const')
const isEmptyObject = require('./utils/is-empty-object')
const getDynamicTemplate = require('./runtime-render/getTemplate')
const genDynamicTemplate = require('./runtime-render/gen-dynamic-template')
const resolveMpxCustomElementPath = require('./utils/resolve-mpx-custom-element-path')
require('./utils/check-core-version-match')

Expand Down Expand Up @@ -970,21 +970,24 @@ class MpxWebpackPlugin {
const runtimeInfoJson = mpx.runtimeInfoJson[packageName] || {}

for (const resourcePath in mpx.runtimeInfoTemplate[packageName]) {
const { dynamicSlotDependencies = {} } = mpx.runtimeInfoTemplate[packageName][resourcePath]
const { dynamicSlotDependencies = [] } = mpx.runtimeInfoTemplate[packageName][resourcePath]
const componentsJsonConfig = runtimeInfoJson[resourcePath]

for (const componentName in dynamicSlotDependencies) {
const { resourcePath, isDynamic } = componentsJsonConfig[componentName] || {}
if (isDynamic) {
dynamicSlotDependencies[componentName].forEach(name => {
const { resourcePath: path, hashName } = componentsJsonConfig[name]
dynamicSlotDependencies.forEach((slotDependencies) => {
let lastNeedInjectNode = slotDependencies[0]
for (let i = 1; i <= slotDependencies.length - 1; i++) {
const componentName = slotDependencies[i]
const { resourcePath, isDynamic } = componentsJsonConfig[componentName] || {}
if (isDynamic) {
const { resourcePath: path, hashName } = componentsJsonConfig[lastNeedInjectNode]
mpx.dynamicSlotDependencies[resourcePath] = mpx.dynamicSlotDependencies[resourcePath] || {}
Object.assign(mpx.dynamicSlotDependencies[resourcePath], {
[hashName]: publicPath + componentsMap[path]
})
})
lastNeedInjectNode = slotDependencies[i]
}
}
}
})
}
}
}
Expand Down Expand Up @@ -1247,7 +1250,7 @@ class MpxWebpackPlugin {
const _dynamicAsset = JSON.parse(dynamicAsset)
if (type === 'template') {
// 动态注入运行时组件的模版内容
extractedInfo.content = getDynamicTemplate(packageName)
extractedInfo.content = genDynamicTemplate(packageName)
dynamicAssets[moduleId][type] = mpx.changeHashNameForAstNode(_dynamicAsset, packageName, resourcePath)
} else if (type === 'styles') {
// 合并多个style标签的样式代码
Expand Down
21 changes: 10 additions & 11 deletions packages/webpack-plugin/lib/runtime-render/base-wxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ function makeAttrsMap (attrKeys = []) {
// 部分节点类型不需要被收集
const RUNTIME_FILTER_NODES = ['import', 'template', 'wxs', 'component', 'slot']

function hasParentCustomComponent (el, isComponentNode, options) {
function collectParentCustomComponent (el, isComponentNode, options) {
const res = []
let parent = el.parent
while (parent) {
if (isComponentNode(parent, options)) {
return parent.tag
if (!res.length) res.push(el.tag)
res.push(parent.tag)
}
parent = parent.parent
}
return false
return res
}

module.exports = function setBaseWxml (el, config, meta) {
Expand All @@ -32,7 +34,7 @@ module.exports = function setBaseWxml (el, config, meta) {
meta.runtimeInfo = {
baseComponents: {},
customComponents: {},
dynamicSlotDependencies: {}
dynamicSlotDependencies: []
}
}

Expand All @@ -55,14 +57,11 @@ module.exports = function setBaseWxml (el, config, meta) {
el.tag = optimizedInfo.nodeType
}
} else {
// 收集运行时组件模版当中运行时组件使用普通组件作为slot的场景,主要因为普通组件被渲染时上下文发生了改变
const parentComponentTag = hasParentCustomComponent(el, isComponentNode, options)
if (parentComponentTag) {
// 收集运行时组件模版当中运行时组件使用 slot 的场景,主要因为运行时组件渲染slot时组件上下文发生了变化
const slotDependencies = collectParentCustomComponent(el, isComponentNode, options)
if (slotDependencies.length) {
const dynamicSlotDependencies = meta.runtimeInfo.dynamicSlotDependencies
if (!dynamicSlotDependencies[parentComponentTag]) {
dynamicSlotDependencies[parentComponentTag] = []
}
dynamicSlotDependencies[parentComponentTag].push(el.tag)
dynamicSlotDependencies.push(slotDependencies)
}
}

Expand Down

0 comments on commit 573b44d

Please sign in to comment.