Skip to content

Commit 62b9d02

Browse files
authored
feat(compiler-core): add comments parser option (#1858)
1 parent 823a2bc commit 62b9d02

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,16 @@ describe('compiler: parse', () => {
374374
}
375375
})
376376
})
377+
378+
test('comments option', () => {
379+
__DEV__ = false
380+
const astNoComment = baseParse('<!--abc-->')
381+
const astWithComments = baseParse('<!--abc-->', { comments: true })
382+
__DEV__ = true
383+
384+
expect(astNoComment.children).toHaveLength(0)
385+
expect(astWithComments.children).toHaveLength(1)
386+
})
377387
})
378388

379389
describe('Element', () => {

packages/compiler-core/src/options.ts

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export interface ParserOptions {
4949
*/
5050
decodeEntities?: (rawText: string, asAttr: boolean) => string
5151
onError?: (error: CompilerError) => void
52+
/**
53+
* Keep comments in the templates AST, even in production
54+
*/
55+
comments?: boolean
5256
}
5357

5458
export type HoistTransform = (

packages/compiler-core/src/parse.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export const defaultParserOptions: MergedParserOptions = {
5050
isCustomElement: NO,
5151
decodeEntities: (rawText: string): string =>
5252
rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
53-
onError: defaultOnError
53+
onError: defaultOnError,
54+
comments: false
5455
}
5556

5657
export const enum TextModes {
@@ -228,8 +229,12 @@ function parseChildren(
228229
} else {
229230
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
230231
}
231-
} else if (!__DEV__ && node.type === NodeTypes.COMMENT) {
232-
// remove comment nodes in prod
232+
} else if (
233+
!__DEV__ &&
234+
node.type === NodeTypes.COMMENT &&
235+
!context.options.comments
236+
) {
237+
// remove comment nodes in prod by default
233238
removedWhitespace = true
234239
nodes[i] = null as any
235240
}

0 commit comments

Comments
 (0)