Skip to content

Commit

Permalink
fix: 修复不能使用原生自定义组件的问题,fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Dec 15, 2021
1 parent 67cf90c commit 81ba91d
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,51 @@ export default (ctx: IPluginContext) => {
name: 'dd',
useConfigName: 'mini',
async fn ({ config }) {
modifyPageTemplate(ctx)
const program = new DD(ctx, config)
await program.start()
}
})
}

// 支付宝小程序中,如果某个页面依赖了原生小程序组件,
// 那么这个页面不能使用公共模板 base.axml,
// 而需要把公共模板的内容在此页面的模板中复制一份, 。
function modifyPageTemplate (ctx: IPluginContext) {
ctx.modifyBuildAssets(({ assets, miniPlugin }) => {
const pages: string[] = []

// 筛选出使用了自定义组件的页面
miniPlugin.pages.forEach(page => {
const config = miniPlugin.filesConfig[miniPlugin.getConfigFilePath(page.name)].content
if (!page.isNative && config?.hasOwnProperty('usingComponents') && Object.keys(config.usingComponents).length) {
pages.push(page.name)
}
})

if (!pages.length) return

const baseXml = assets['base.axml'].source()

pages.forEach(page => {
const templateName = `${page}.axml`
const assetsItem = assets[templateName]
const src = assetsItem._value ? assetsItem._value.toString() : assetsItem.source()
let relativePath
const templateCaller = src.replace(/<import src="(.*)base\.axml"\/>/, function (_, $1) {
relativePath = $1
return ''
})
const main = baseXml.replace(/<import-sjs name="xs" from="(.*)utils.sjs" \/>/, function () {
return `<import-sjs name="xs" from="${relativePath}utils.sjs" />`
})

const res = `${templateCaller}
${main}`
assets[templateName] = {
size: () => res.length,
source: () => res
}
})
})
}

0 comments on commit 81ba91d

Please sign in to comment.