diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index fc7f0cb1d9d3..8d66426ebe1c 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -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) } @@ -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) => { @@ -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 } diff --git a/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts b/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts index bac6697c76c0..eecfa2728a5d 100644 --- a/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts @@ -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') /** * 与原生小程序混写时解析模板与样式 @@ -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') { @@ -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) => { @@ -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 }