Skip to content

Commit a98a157

Browse files
committed
fix: 修复definePageConfig宏函数正则匹配注释代码问题。
1 parent b3127a8 commit a98a157

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/taro-helper/src/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,24 @@ function genProps(props: any[]) {
582582
}, {})
583583
}
584584

585+
// 移除JavaScript/TypeScript代码中的注释
586+
export function removeComments(code: string): string {
587+
// 移除单行注释
588+
code = code.replace(/\/\/.*$/gm, '')
589+
// 移除多行注释
590+
code = code.replace(/\/\*[\s\S]*?\*\//g, '')
591+
return code
592+
}
593+
585594
// read page config from a sfc file instead of the regular config file
586595
function readSFCPageConfig(configPath: string) {
587596
if (!fs.existsSync(configPath)) return {}
588597

589598
const sfcSource = fs.readFileSync(configPath, 'utf8')
599+
// 先移除注释,再应用正则表达式
600+
const codeWithoutComments = removeComments(sfcSource)
590601
const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g
591-
const matches = sfcSource.match(dpcReg)
602+
const matches = codeWithoutComments.match(dpcReg)
592603

593604
let result: any = {}
594605

0 commit comments

Comments
 (0)