]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler): allow 'comments' option to affect comment inclusion in dev (#4115)
authorAustin Keener <keeneraustin@yahoo.com>
Mon, 19 Jul 2021 15:40:37 +0000 (11:40 -0400)
committerGitHub <noreply@github.com>
Mon, 19 Jul 2021 15:40:37 +0000 (11:40 -0400)
Close: #3392
Replace: #3395

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

index d677f75dd747b4770e15a96f15ba1eab83fa4978..84c3d538bbe9f104bda4430754768378fe23449b 100644 (file)
@@ -377,25 +377,42 @@ describe('compiler: parse', () => {
     })
 
     test('comments option', () => {
-      __DEV__ = false
-      const astDefaultComment = baseParse('<!--abc-->')
-      const astNoComment = baseParse('<!--abc-->', { comments: false })
-      const astWithComments = baseParse('<!--abc-->', { comments: true })
-      __DEV__ = true
+      const astOptionNoComment = baseParse('<!--abc-->', { comments: false })
+      const astOptionWithComments = baseParse('<!--abc-->', { 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 <pre> tag should be removed in production mode', () => {
-      __DEV__ = false
+    test('comments in the <pre> tag should be removed when comments option requires it', () => {
       const rawText = `<p/><!-- foo --><p/>`
-      const ast = baseParse(`<pre>${rawText}</pre>`)
-      __DEV__ = true
 
-      expect((ast.children[0] as ElementNode).children).toMatchObject([
+      const astWithComments = baseParse(`<pre>${rawText}</pre>`, {
+        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(`<pre>${rawText}</pre>`, {
+        comments: false
+      })
+      expect(
+        (astWithoutComments.children[0] as ElementNode).children
+      ).toMatchObject([
         {
           type: NodeTypes.ELEMENT,
           tag: 'p'
index 22988b150a53418bf57980b62b1c0ec0b6fba2c0..29b8c68774c72bd2fbc5678fb8185d2ff1f2f75d 100644 (file)
@@ -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
 }
index 6c1863f210b9f098d4a7865102b41d1f2a123baf..96aec726686534c82a90493da3a729222a662279 100644 (file)
@@ -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
       }