Skip to content

Commit

Permalink
fix(mini-runner): 按需生成 CustomWrapper 产物,fix #11745
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Sep 21, 2022
1 parent c9a3d74 commit d223734
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
50 changes: 31 additions & 19 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ export default class TaroMiniPlugin {
const baseTemplateName = this.getIsBuildPluginPath('base', isBuildPlugin)
const baseCompName = 'comp'
const customWrapperName = 'custom-wrapper'
const isUsingCustomWrapper = componentConfig.thirdPartyComponents.has('custom-wrapper')
if (typeof modifyMiniConfigs === 'function') {
await modifyMiniConfigs(this.filesConfig)
}
Expand All @@ -913,30 +914,39 @@ export default class TaroMiniPlugin {
if (!template.isSupportRecursive) {
// 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
this.generateTemplateFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), template.buildBaseComponentTemplate, this.options.fileType.templ)
this.generateConfigFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), {
const baseCompConfig = {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
}
})
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
[baseCompName]: `./${baseCompName}`
}
})
}
if (isUsingCustomWrapper) {
baseCompConfig[customWrapperName] = `./${customWrapperName}`
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
}
})
}
this.generateConfigFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), baseCompConfig)
} else {
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[customWrapperName]: `./${customWrapperName}`
}
})
if (isUsingCustomWrapper) {
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[customWrapperName]: `./${customWrapperName}`
}
})
}
}
this.generateTemplateFile(compilation, baseTemplateName, template.buildTemplate, componentConfig)
this.generateTemplateFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), template.buildCustomComponentTemplate, this.options.fileType.templ)
if (isUsingCustomWrapper) {
this.generateTemplateFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), template.buildCustomComponentTemplate, this.options.fileType.templ)
} else {
delete compilation.assets['custom-wrapper.js']
}
this.generateXSFile(compilation, 'utils', isBuildPlugin)
// 为独立分包生成 base/comp/custom-wrapper
this.independentPackages.forEach((_pages, name) => {
Expand Down Expand Up @@ -991,9 +1001,11 @@ export default class TaroMiniPlugin {
importCustomWrapperPath = promoteRelativePath(path.relative(page.path, path.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))))
}
config.content.usingComponents = {
[customWrapperName]: importCustomWrapperPath,
...config.content.usingComponents
}
if (isUsingCustomWrapper) {
config.content.usingComponents[customWrapperName] = importCustomWrapperPath
}
if (!template.isSupportRecursive && !page.isNative) {
config.content.usingComponents[baseCompName] = importBaseCompPath
}
Expand Down
49 changes: 30 additions & 19 deletions packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ export default class TaroMiniPlugin {
const baseTemplateName = this.getIsBuildPluginPath('base', isBuildPlugin)
const baseCompName = 'comp'
const customWrapperName = 'custom-wrapper'
const isUsingCustomWrapper = componentConfig.thirdPartyComponents.has('custom-wrapper')

/**
* 与原生小程序混写时解析模板与样式
Expand All @@ -886,6 +887,9 @@ export default class TaroMiniPlugin {
compilation.assets[newAssetPath] = assetObj
delete compilation.assets[assetPath]
}
if (!isUsingCustomWrapper && assetPath === 'custom-wrapper.js') {
delete compilation.assets[assetPath]
}
})

if (typeof modifyMiniConfigs === 'function') {
Expand All @@ -899,30 +903,35 @@ export default class TaroMiniPlugin {
if (!template.isSupportRecursive) {
// 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归
this.generateTemplateFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), template.buildBaseComponentTemplate, this.options.fileType.templ)
this.generateConfigFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
}
})
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
const baseCompConfig = {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
[baseCompName]: `./${baseCompName}`
}
})
}
if (isUsingCustomWrapper) {
baseCompConfig[customWrapperName] = `./${customWrapperName}`
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[baseCompName]: `./${baseCompName}`,
[customWrapperName]: `./${customWrapperName}`
}
})
}
this.generateConfigFile(compilation, this.getIsBuildPluginPath(baseCompName, isBuildPlugin), baseCompConfig)
} else {
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[customWrapperName]: `./${customWrapperName}`
}
})
if (isUsingCustomWrapper) {
this.generateConfigFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), {
component: true,
usingComponents: {
[customWrapperName]: `./${customWrapperName}`
}
})
}
}
this.generateTemplateFile(compilation, baseTemplateName, template.buildTemplate, componentConfig)
this.generateTemplateFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), template.buildCustomComponentTemplate, this.options.fileType.templ)
isUsingCustomWrapper && this.generateTemplateFile(compilation, this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), template.buildCustomComponentTemplate, this.options.fileType.templ)
this.generateXSFile(compilation, 'utils', isBuildPlugin)
// 为独立分包生成 base/comp/custom-wrapper
this.independentPackages.forEach((_pages, name) => {
Expand Down Expand Up @@ -977,9 +986,11 @@ export default class TaroMiniPlugin {
importCustomWrapperPath = promoteRelativePath(path.relative(page.path, path.join(sourceDir, independentName, this.getTargetFilePath(this.getIsBuildPluginPath(customWrapperName, isBuildPlugin), ''))))
}
config.content.usingComponents = {
[customWrapperName]: importCustomWrapperPath,
...config.content.usingComponents
}
if(isUsingCustomWrapper) {
config.content.usingComponents[customWrapperName] = importCustomWrapperPath
}
if (!template.isSupportRecursive && !page.isNative) {
config.content.usingComponents[baseCompName] = importBaseCompPath
}
Expand Down

0 comments on commit d223734

Please sign in to comment.