From 5e155fc011f6d816752f1e3255f9ec8781a82344 Mon Sep 17 00:00:00 2001 From: Austin Keener Date: Mon, 12 Jul 2021 12:24:41 -0400 Subject: [PATCH] feat(compiler): Allow 'comments' option to effect comment inclusion in development Close: #3392 Replace: #3395 --- .../compiler-core/__tests__/parse.spec.ts | 43 +++++++++++++------ packages/compiler-core/src/options.ts | 3 +- packages/compiler-core/src/parse.ts | 19 ++++---- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index 1c7c878539a..ff5f121163f 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -377,25 +377,42 @@ describe('compiler: parse', () => { }) test('comments option', () => { - __DEV__ = false - const astDefaultComment = baseParse('') - const astNoComment = baseParse('', { comments: false }) - const astWithComments = baseParse('', { comments: true }) - __DEV__ = true + const astOptionNoComment = baseParse('', { comments: false }) + const astOptionWithComments = baseParse('', { comments: true }) - expect(astDefaultComment.children).toHaveLength(0) - expect(astNoComment.children).toHaveLength(0) - expect(astWithComments.children).toHaveLength(1) + expect(astOptionNoComment.children).toHaveLength(0) + expect(astOptionWithComments.children).toHaveLength(1) }) // #2217 - test('comments in the
 tag should be removed in production mode', () => {
-      __DEV__ = false
+    test('comments in the 
 tag should be removed when comments option requires it', () => {
       const rawText = `

` - const ast = baseParse(`

${rawText}
`) - __DEV__ = true - expect((ast.children[0] as ElementNode).children).toMatchObject([ + const astWithComments = baseParse(`
${rawText}
`, { + comments: true + }) + expect( + (astWithComments.children[0] as ElementNode).children + ).toMatchObject([ + { + type: NodeTypes.ELEMENT, + tag: 'p' + }, + { + type: NodeTypes.COMMENT + }, + { + type: NodeTypes.ELEMENT, + tag: 'p' + } + ]) + + const astWithoutComments = baseParse(`
${rawText}
`, { + comments: false + }) + expect( + (astWithoutComments.children[0] as ElementNode).children + ).toMatchObject([ { type: NodeTypes.ELEMENT, tag: 'p' diff --git a/packages/compiler-core/src/options.ts b/packages/compiler-core/src/options.ts index a387c167875..6dfc157bccc 100644 --- a/packages/compiler-core/src/options.ts +++ b/packages/compiler-core/src/options.ts @@ -61,7 +61,8 @@ export interface ParserOptions */ decodeEntities?: (rawText: string, asAttr: boolean) => string /** - * Keep comments in the templates AST, even in production + * Whether to keep comments in the templates AST. + * This defaults to `true` in development and `false` in production builds. */ comments?: boolean } diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index 9e6ee326096..cef4f999487 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -77,7 +77,7 @@ export const defaultParserOptions: MergedParserOptions = { rawText.replace(decodeRE, (_, p1) => decodeMap[p1]), onError: defaultOnError, onWarn: defaultOnWarn, - comments: false + comments: __DEV__ } export const enum TextModes { @@ -118,9 +118,14 @@ function createParserContext( rawOptions: ParserOptions ): ParserContext { const options = extend({}, defaultParserOptions) - for (const key in rawOptions) { + + let key: keyof ParserOptions + for (key in rawOptions) { // @ts-ignore - options[key] = rawOptions[key] || defaultParserOptions[key] + options[key] = + rawOptions[key] === undefined + ? defaultParserOptions[key] + : rawOptions[key] } return { options, @@ -282,12 +287,8 @@ function parseChildren( node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ') } } - // also remove comment nodes in prod by default - if ( - !__DEV__ && - node.type === NodeTypes.COMMENT && - !context.options.comments - ) { + // Remove comment nodes if desired by configuration. + else if (node.type === NodeTypes.COMMENT && !context.options.comments) { removedWhitespace = true nodes[i] = null as any }