diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index a93b5e0404c..63f97ca9f4c 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -377,23 +377,17 @@ describe('compiler: parse', () => { }) test('comments option', () => { - __DEV__ = false - const astDefaultComment = baseParse('') - const astNoComment = baseParse('', { comments: false }) const astWithComments = baseParse('', { comments: true }) - __DEV__ = true + const astNoComment = baseParse('', { comments: false }) - expect(astDefaultComment.children).toHaveLength(0) - expect(astNoComment.children).toHaveLength(0) expect(astWithComments.children).toHaveLength(1) + expect(astNoComment.children).toHaveLength(0) }) // #2217 test('comments in the
 tag should be removed in production mode', () => {
-      __DEV__ = false
       const rawText = `

` - const ast = baseParse(`

${rawText}
`) - __DEV__ = true + const ast = baseParse(`
${rawText}
`, { comments: false }) expect((ast.children[0] as ElementNode).children).toMatchObject([ { diff --git a/packages/compiler-core/src/options.ts b/packages/compiler-core/src/options.ts index d8d2573a6e8..2b9f87ca133 100644 --- a/packages/compiler-core/src/options.ts +++ b/packages/compiler-core/src/options.ts @@ -50,7 +50,7 @@ export interface ParserOptions { decodeEntities?: (rawText: string, asAttr: boolean) => string onError?: (error: CompilerError) => void /** - * Keep comments in the templates AST, even in production + * Whether to keep comments in the templates AST */ comments?: boolean } diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index d57ca77a03e..4bcf6c47198 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -59,7 +59,8 @@ export const defaultParserOptions: MergedParserOptions = { decodeEntities: (rawText: string): string => rawText.replace(decodeRE, (_, p1) => decodeMap[p1]), onError: defaultOnError, - comments: false + // only keep comment nodes in DEV by default + comments: __DEV__ } export const enum TextModes { @@ -99,9 +100,13 @@ 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, @@ -236,13 +241,8 @@ function parseChildren( } else { 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 - ) { + } else if (node.type === NodeTypes.COMMENT && !context.options.comments) { + // comment nodes handling removedWhitespace = true nodes[i] = null as any }