Skip to content

Commit

Permalink
fix(plugin-inject): 修复 Vue2 使用 componentsMap 配置时报错的问题,#13299 #14520 (#…
Browse files Browse the repository at this point in the history
…14607)

* fix(plugin-inject): 修复 Vue2 使用 componentsMap 配置时报错的问题,#13299 #14520

* fix(plugin-inject): 开启 treeshake

---------

Co-authored-by: xuanzebin <38971117+xuanzebin@users.noreply.github.com>
  • Loading branch information
Chen-jj and xuanzebin authored Oct 31, 2023
1 parent b336e82 commit 27aa3a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/taro-plugin-inject/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"dependencies": {
"@tarojs/service": "workspace:*",
"@tarojs/shared": "workspace:*"
"@tarojs/shared": "workspace:*",
"@tarojs/helper": "workspace:*"
},
"devDependencies": {
"rollup": "^2.79.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/taro-plugin-inject/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import { esbuild } from '@tarojs/helper'
import { isArray, isFunction, isObject, isString } from '@tarojs/shared'
import * as path from 'path'

Expand All @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 27aa3a8

Please sign in to comment.