Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/inject bug #14606

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/shared/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class BaseTemplate {
result[compName] = {
name: newComp?.name,
}
} else {
} else {
result[compName] = newComp
}
}
Expand All @@ -227,7 +227,7 @@ export class BaseTemplate {
: this.dataKeymap('i:item,c:1')
const xs = this.supportXS
? (this.isSupportRecursive
? `xs.a(0, item.${Shortcuts.NodeName})`
? `xs.a(0, item.${Shortcuts.NodeName})`
: `xs.a(0, item.${Shortcuts.NodeName}, '')`)
: "'tmpl_0_' + item.nn"
return `${this.buildXsTemplate()}
Expand Down Expand Up @@ -285,7 +285,7 @@ export class BaseTemplate {
const { isSupportRecursive, supportXS } = this
const isLastRecursiveComp = !isSupportRecursive && level + 1 === this.baseLevel
const isUseXs = !this.isSupportRecursive && this.supportXS

if (isLastRecursiveComp) {
const data = isUseXs
? `${this.dataKeymap('i:item,c:c,l:l')}`
Expand All @@ -309,19 +309,19 @@ export class BaseTemplate {

return supportXS
? `<template is="{{${xs}}}" data="{{${data}}}" />`
: isSupportRecursive
: isSupportRecursive
? `<template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />`
: `<template is="{{'tmpl_' + c + '_' + item.nn}}" data="{{${data}}}" />`
}

}

private getChildren (comp: Component, level: number): string {
const { isSupportRecursive, Adapter } = this
const nextLevel = isSupportRecursive ? 0 : level + 1

let child = this.getChildrenTemplate(nextLevel)

if (isFunction(this.modifyLoopBody)) {
child = this.modifyLoopBody(child, comp.nodeName)
}
Expand Down Expand Up @@ -441,13 +441,17 @@ export class BaseTemplate {
child = this.modifyThirdPartyLoopBody(child, compName)
}

template += `
<template name="tmpl_${level}_${compName}">
<${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
const children = this.voidElements.has(compName)
? ''
: `
<block ${Adapter.for}="{{i.${Shortcuts.Childnodes}}}" ${Adapter.key}="sid">
${child}
</block>
</${compName}>
`

template += `
<template name="tmpl_${level}_${compName}">
<${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">${children}</${compName}>
</template>
`
}
Expand Down Expand Up @@ -671,7 +675,7 @@ export class UnRecursiveTemplate extends BaseTemplate {
const listA = Array.from(isLoopCompsSet).map(item => componentsAlias[item]?._num || item)
const listB = hasMaxComps.map(item => componentsAlias[item]?._num || item)
const containerLevel = this.baseLevel - 1

return `function (l, n, s) {
var a = ${JSON.stringify(listA)}
var b = ${JSON.stringify(listB)}
Expand Down
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
14 changes: 12 additions & 2 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 Down Expand Up @@ -41,7 +42,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 +90,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.