Skip to content

Commit dee08e6

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

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

packages/taro-helper/src/utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,30 @@ function genProps(props: any[]) {
582582
}, {})
583583
}
584584

585+
// 移除JavaScript/TypeScript代码中的注释
586+
export function removeComments(code: string): string {
587+
// 移除单行注释(避免误删字符串中的协议前缀,如 `http://、https://、ws://、ftp://、file://)`
588+
code = code.replace(/\/\/.*$/gm, (match: string, ...args: any[]) => {
589+
const offset = args[args.length - 2] as number
590+
const src = args[args.length - 1] as string
591+
if (offset > 0 && src[offset - 1] === ':') return match
592+
return ''
593+
})
594+
// 移除多行注释
595+
code = code.replace(/\/\*[\s\S]*?\*\//g, '')
596+
return code
597+
}
598+
585599
// read page config from a sfc file instead of the regular config file
586600
function readSFCPageConfig(configPath: string) {
587601
if (!fs.existsSync(configPath)) return {}
588602

589603
const sfcSource = fs.readFileSync(configPath, 'utf8')
590-
const dpcReg = /definePageConfig\(\{[\w\W]+?\}\)/g
591-
const matches = sfcSource.match(dpcReg)
604+
// 先移除注释,再应用正则表达式,避免注释中的代码被错误匹配
605+
const codeWithoutComments = removeComments(sfcSource)
606+
// 容忍空白与换行、保证单词边界
607+
const dpcReg = /\bdefinePageConfig\s*\(\s*\{[\s\S]*?\}\s*\)/g
608+
const matches = codeWithoutComments.match(dpcReg)
592609

593610
let result: any = {}
594611

0 commit comments

Comments
 (0)