]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-core): add `comments` parser option (#1858)
authorBarthélémy Ledoux <ledouxb@me.com>
Mon, 17 Aug 2020 15:20:28 +0000 (10:20 -0500)
committerGitHub <noreply@github.com>
Mon, 17 Aug 2020 15:20:28 +0000 (11:20 -0400)
packages/compiler-core/__tests__/parse.spec.ts
packages/compiler-core/src/options.ts
packages/compiler-core/src/parse.ts

index 7f12a70099d7af57ea33392e4b698a68a73f39fa..56bea060506b07104753ee14baf958785289352e 100644 (file)
@@ -374,6 +374,16 @@ describe('compiler: parse', () => {
         }
       })
     })
+
+    test('comments option', () => {
+      __DEV__ = false
+      const astNoComment = baseParse('<!--abc-->')
+      const astWithComments = baseParse('<!--abc-->', { comments: true })
+      __DEV__ = true
+
+      expect(astNoComment.children).toHaveLength(0)
+      expect(astWithComments.children).toHaveLength(1)
+    })
   })
 
   describe('Element', () => {
index f0d5258ce635cfb807c023bce328543ac0b93afa..22d8a086b2fe0a84002dc57800388e60f7a3c1bc 100644 (file)
@@ -49,6 +49,10 @@ export interface ParserOptions {
    */
   decodeEntities?: (rawText: string, asAttr: boolean) => string
   onError?: (error: CompilerError) => void
+  /**
+   * Keep comments in the templates AST, even in production
+   */
+  comments?: boolean
 }
 
 export type HoistTransform = (
index e2888e32e6073ae4bdc7f0484be7129e5b3bd23e..60d6042713c42800fee1444a71cc9853f9de52e5 100644 (file)
@@ -50,7 +50,8 @@ export const defaultParserOptions: MergedParserOptions = {
   isCustomElement: NO,
   decodeEntities: (rawText: string): string =>
     rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
-  onError: defaultOnError
+  onError: defaultOnError,
+  comments: false
 }
 
 export const enum TextModes {
@@ -228,8 +229,12 @@ function parseChildren(
           } else {
             node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
           }
-        } else if (!__DEV__ && node.type === NodeTypes.COMMENT) {
-          // remove comment nodes in prod
+        } else if (
+          !__DEV__ &&
+          node.type === NodeTypes.COMMENT &&
+          !context.options.comments
+        ) {
+          // remove comment nodes in prod by default
           removedWhitespace = true
           nodes[i] = null as any
         }