diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index 60d6042713c..7d22a473590 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -246,6 +246,15 @@ function parseChildren( if (first && first.type === NodeTypes.TEXT) { first.content = first.content.replace(/^\r?\n/, '') } + if (!__DEV__ && !context.options.comments) { + for (let i = 0; i < nodes.length; i++) { + const node = nodes[i] + if (node.type === NodeTypes.COMMENT) { + removedWhitespace = true + nodes[i] = null as any + } + } + } } } diff --git a/packages/compiler-core/src/transforms/transformText.ts b/packages/compiler-core/src/transforms/transformText.ts index 7419497fe1a..039a6a70ade 100644 --- a/packages/compiler-core/src/transforms/transformText.ts +++ b/packages/compiler-core/src/transforms/transformText.ts @@ -9,6 +9,7 @@ import { import { isText } from '../utils' import { CREATE_TEXT } from '../runtimeHelpers' import { PatchFlags, PatchFlagNames } from '@vue/shared' +import { getStaticType } from './hoistStatic' // Merge adjacent text nodes and expressions into a single expression // e.g.
abc {{ d }} {{ e }}
should have a single expression node as child. @@ -78,7 +79,7 @@ export const transformText: NodeTransform = (node, context) => { callArgs.push(child) } // mark dynamic text with flag so it gets patched inside a block - if (!context.ssr && child.type !== NodeTypes.TEXT) { + if (!context.ssr && !getStaticType(child)) { callArgs.push( `${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */` ) diff --git a/packages/compiler-dom/__tests__/parse.spec.ts b/packages/compiler-dom/__tests__/parse.spec.ts index 9f227fa2184..6680b517f8b 100644 --- a/packages/compiler-dom/__tests__/parse.spec.ts +++ b/packages/compiler-dom/__tests__/parse.spec.ts @@ -159,6 +159,25 @@ describe('DOM parser', () => { ]) }) + // #2217 + test('comments in the
 tag should be removed in production mode', () => {
+      __DEV__ = false
+      const rawText = `

` + const ast = parse(`

${rawText}
`, parserOptions) + __DEV__ = true + + expect((ast.children[0] as ElementNode).children).toMatchObject([ + { + type: NodeTypes.ELEMENT, + tag: 'p' + }, + { + type: NodeTypes.ELEMENT, + tag: 'p' + } + ]) + }) + // #945 test('  should not be condensed', () => { const nbsp = String.fromCharCode(160)