From 27aa3a82d4a99fb2f42d8a75c54499e656f20050 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Tue, 31 Oct 2023 14:55:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugin-inject):=20=E4=BF=AE=E5=A4=8D=20Vue2?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8=20componentsMap=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?#13299=20#14520=20(#14607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(plugin-inject): 修复 Vue2 使用 componentsMap 配置时报错的问题,#13299 #14520 * fix(plugin-inject): 开启 treeshake --------- Co-authored-by: xuanzebin <38971117+xuanzebin@users.noreply.github.com> --- packages/taro-plugin-inject/package.json | 3 ++- packages/taro-plugin-inject/src/index.ts | 20 ++++++++++++-------- pnpm-lock.yaml | 2 ++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/taro-plugin-inject/package.json b/packages/taro-plugin-inject/package.json index bc5bb676a5a..e0d88dffd39 100644 --- a/packages/taro-plugin-inject/package.json +++ b/packages/taro-plugin-inject/package.json @@ -26,7 +26,8 @@ }, "dependencies": { "@tarojs/service": "workspace:*", - "@tarojs/shared": "workspace:*" + "@tarojs/shared": "workspace:*", + "@tarojs/helper": "workspace:*" }, "devDependencies": { "rollup": "^2.79.0", diff --git a/packages/taro-plugin-inject/src/index.ts b/packages/taro-plugin-inject/src/index.ts index b0ec7747340..ac53c4030d1 100644 --- a/packages/taro-plugin-inject/src/index.ts +++ b/packages/taro-plugin-inject/src/index.ts @@ -1,4 +1,5 @@ +import { esbuild } from '@tarojs/helper' import { isArray, isFunction, isObject, isString } from '@tarojs/shared' import * as path from 'path' @@ -20,12 +21,6 @@ export interface IOptions { export default (ctx: IPluginContext, options: IOptions) => { const fs = ctx.helper.fs - ctx.modifyWebpackChain(({ chain }) => { - if(options.componentsMap){ - chain.optimization.providedExports(false) - } - }) - ctx.registerMethod({ name: 'onSetupClose', fn (platform: TaroPlatformBase) { @@ -41,7 +36,7 @@ export default (ctx: IPluginContext, options: IOptions) => { const template = platform.template if(!template) return - + if (isArray(voidComponents)) { voidComponents.forEach(el => template.voidElements.add(el)) } else if (isFunction(voidComponents)) { @@ -89,10 +84,19 @@ function injectRuntimePath (platform: TaroPlatformBase) { } function injectComponentsReact (fs, taroComponentsPath, componentsMap) { - fs.writeFileSync(path.resolve(__dirname, '../dist/components-react.js'), ` + const filePath = path.resolve(__dirname, '../dist/components-react.js') + fs.writeFileSync(filePath, ` export * from '${taroComponentsPath}' ${Object.keys(componentsMap).map((key) => `export const ${key} = '${componentsMap[key]}'`).join('\n')} `) + // 提前使用 esbuild 进行 bundle,避免 Webpack 分析过程中的错误,#13299 #14520 + const result = esbuild.buildSync({ + entryPoints: [filePath], + bundle: true, + write: false, + format: 'esm', + }) + fs.writeFileSync(filePath, result.outputFiles[0].text) } function injectComponents (fs, components) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index efd3b7c5865..2a2cae1affe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1392,12 +1392,14 @@ importers: packages/taro-plugin-inject: specifiers: + '@tarojs/helper': workspace:* '@tarojs/service': workspace:* '@tarojs/shared': workspace:* rollup: ^2.79.0 rollup-plugin-ts: ^3.0.2 typescript: ^4.7.4 dependencies: + '@tarojs/helper': link:../taro-helper '@tarojs/service': link:../taro-service '@tarojs/shared': link:../shared devDependencies: