Skip to content

Commit

Permalink
feat(plugin-compiler-alipay): 完善支付宝转微信 sjs 中 export default function …
Browse files Browse the repository at this point in the history
…xxx(){} 的转换支持
  • Loading branch information
lyfeyaj committed Jun 13, 2023
1 parent 1982ed6 commit f907bb2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/plugin-compiler-alipay/src/plugins/SjsParserPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ export default class AlipayCompilerSjsParserPlugin implements Plugin {
)
}

/**
* 处理 export default function name() {} 的情况
* 原因: 微信不支持 exports.default = name 这种语法,会报错
* 这里转换为 module.exports = function name() {}
*/
if (ts.isFunctionDeclaration(node) && node.modifiers?.length) {
const exportKeyword = node.modifiers[0]
const exportDefaultKeyword = node.modifiers[1]
if (
ts.isToken(exportKeyword) &&
exportKeyword.kind === ts.SyntaxKind.ExportKeyword &&
ts.isToken(exportDefaultKeyword) &&
exportDefaultKeyword.kind === ts.SyntaxKind.DefaultKeyword
) {
return factory.createExpressionStatement(
factory.createBinaryExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('module'),
factory.createIdentifier('exports')
),
factory.createToken(ts.SyntaxKind.EqualsToken),
factory.createFunctionExpression(
undefined,
node.asteriskToken,
node.name,
node.typeParameters,
node.parameters,
node.type,
node.body
)
)
)
}
}

return node
})
)
Expand Down

0 comments on commit f907bb2

Please sign in to comment.