Skip to content

Commit dd0f9d1

Browse files
feat(compiler): allow 'comments' option to affect comment inclusion in dev (#4115)
Close: #3392 Replace: #3395
1 parent d868d5f commit dd0f9d1

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

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

+30-13
Original file line numberDiff line numberDiff line change
@@ -377,25 +377,42 @@ describe('compiler: parse', () => {
377377
})
378378

379379
test('comments option', () => {
380-
__DEV__ = false
381-
const astDefaultComment = baseParse('<!--abc-->')
382-
const astNoComment = baseParse('<!--abc-->', { comments: false })
383-
const astWithComments = baseParse('<!--abc-->', { comments: true })
384-
__DEV__ = true
380+
const astOptionNoComment = baseParse('<!--abc-->', { comments: false })
381+
const astOptionWithComments = baseParse('<!--abc-->', { comments: true })
385382

386-
expect(astDefaultComment.children).toHaveLength(0)
387-
expect(astNoComment.children).toHaveLength(0)
388-
expect(astWithComments.children).toHaveLength(1)
383+
expect(astOptionNoComment.children).toHaveLength(0)
384+
expect(astOptionWithComments.children).toHaveLength(1)
389385
})
390386

391387
// #2217
392-
test('comments in the <pre> tag should be removed in production mode', () => {
393-
__DEV__ = false
388+
test('comments in the <pre> tag should be removed when comments option requires it', () => {
394389
const rawText = `<p/><!-- foo --><p/>`
395-
const ast = baseParse(`<pre>${rawText}</pre>`)
396-
__DEV__ = true
397390

398-
expect((ast.children[0] as ElementNode).children).toMatchObject([
391+
const astWithComments = baseParse(`<pre>${rawText}</pre>`, {
392+
comments: true
393+
})
394+
expect(
395+
(astWithComments.children[0] as ElementNode).children
396+
).toMatchObject([
397+
{
398+
type: NodeTypes.ELEMENT,
399+
tag: 'p'
400+
},
401+
{
402+
type: NodeTypes.COMMENT
403+
},
404+
{
405+
type: NodeTypes.ELEMENT,
406+
tag: 'p'
407+
}
408+
])
409+
410+
const astWithoutComments = baseParse(`<pre>${rawText}</pre>`, {
411+
comments: false
412+
})
413+
expect(
414+
(astWithoutComments.children[0] as ElementNode).children
415+
).toMatchObject([
399416
{
400417
type: NodeTypes.ELEMENT,
401418
tag: 'p'

packages/compiler-core/src/options.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export interface ParserOptions
6161
*/
6262
decodeEntities?: (rawText: string, asAttr: boolean) => string
6363
/**
64-
* Keep comments in the templates AST, even in production
64+
* Whether to keep comments in the templates AST.
65+
* This defaults to `true` in development and `false` in production builds.
6566
*/
6667
comments?: boolean
6768
}

packages/compiler-core/src/parse.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const defaultParserOptions: MergedParserOptions = {
7777
rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
7878
onError: defaultOnError,
7979
onWarn: defaultOnWarn,
80-
comments: false
80+
comments: __DEV__
8181
}
8282

8383
export const enum TextModes {
@@ -118,9 +118,14 @@ function createParserContext(
118118
rawOptions: ParserOptions
119119
): ParserContext {
120120
const options = extend({}, defaultParserOptions)
121-
for (const key in rawOptions) {
121+
122+
let key: keyof ParserOptions
123+
for (key in rawOptions) {
122124
// @ts-ignore
123-
options[key] = rawOptions[key] || defaultParserOptions[key]
125+
options[key] =
126+
rawOptions[key] === undefined
127+
? defaultParserOptions[key]
128+
: rawOptions[key]
124129
}
125130
return {
126131
options,
@@ -282,12 +287,8 @@ function parseChildren(
282287
node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
283288
}
284289
}
285-
// also remove comment nodes in prod by default
286-
if (
287-
!__DEV__ &&
288-
node.type === NodeTypes.COMMENT &&
289-
!context.options.comments
290-
) {
290+
// Remove comment nodes if desired by configuration.
291+
else if (node.type === NodeTypes.COMMENT && !context.options.comments) {
291292
removedWhitespace = true
292293
nodes[i] = null as any
293294
}

0 commit comments

Comments
 (0)