File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
586600function readSFCPageConfig ( configPath : string ) {
587601 if ( ! fs . existsSync ( configPath ) ) return { }
588602
589603 const sfcSource = fs . readFileSync ( configPath , 'utf8' )
590- const dpcReg = / d e f i n e P a g e C o n f i g \( \{ [ \w \W ] + ?\} \) / g
591- const matches = sfcSource . match ( dpcReg )
604+ // 先移除注释,再应用正则表达式,避免注释中的代码被错误匹配
605+ const codeWithoutComments = removeComments ( sfcSource )
606+ // 容忍空白与换行、保证单词边界
607+ const dpcReg = / \b d e f i n e P a g e C o n f i g \s * \( \s * \{ [ \s \S ] * ?\} \s * \) / g
608+ const matches = codeWithoutComments . match ( dpcReg )
592609
593610 let result : any = { }
594611
You can’t perform that action at this time.
0 commit comments