feat(compiler): allow 'comments' option to affect comment inclusion in dev (#4115)
Close: #3392 Replace: #3395
This commit is contained in:
		
							parent
							
								
									d868d5ffa7
								
							
						
					
					
						commit
						dd0f9d1ce6
					
				@ -377,25 +377,42 @@ describe('compiler: parse', () => {
 | 
				
			|||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    test('comments option', () => {
 | 
					    test('comments option', () => {
 | 
				
			||||||
      __DEV__ = false
 | 
					      const astOptionNoComment = baseParse('<!--abc-->', { comments: false })
 | 
				
			||||||
      const astDefaultComment = baseParse('<!--abc-->')
 | 
					      const astOptionWithComments = baseParse('<!--abc-->', { comments: true })
 | 
				
			||||||
      const astNoComment = baseParse('<!--abc-->', { comments: false })
 | 
					 | 
				
			||||||
      const astWithComments = baseParse('<!--abc-->', { comments: true })
 | 
					 | 
				
			||||||
      __DEV__ = true
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      expect(astDefaultComment.children).toHaveLength(0)
 | 
					      expect(astOptionNoComment.children).toHaveLength(0)
 | 
				
			||||||
      expect(astNoComment.children).toHaveLength(0)
 | 
					      expect(astOptionWithComments.children).toHaveLength(1)
 | 
				
			||||||
      expect(astWithComments.children).toHaveLength(1)
 | 
					 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // #2217
 | 
					    // #2217
 | 
				
			||||||
    test('comments in the <pre> tag should be removed in production mode', () => {
 | 
					    test('comments in the <pre> tag should be removed when comments option requires it', () => {
 | 
				
			||||||
      __DEV__ = false
 | 
					 | 
				
			||||||
      const rawText = `<p/><!-- foo --><p/>`
 | 
					      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,
 | 
					          type: NodeTypes.ELEMENT,
 | 
				
			||||||
          tag: 'p'
 | 
					          tag: 'p'
 | 
				
			||||||
 | 
				
			|||||||
@ -61,7 +61,8 @@ export interface ParserOptions
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  decodeEntities?: (rawText: string, asAttr: boolean) => string
 | 
					  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
 | 
					  comments?: boolean
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -77,7 +77,7 @@ export const defaultParserOptions: MergedParserOptions = {
 | 
				
			|||||||
    rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
 | 
					    rawText.replace(decodeRE, (_, p1) => decodeMap[p1]),
 | 
				
			||||||
  onError: defaultOnError,
 | 
					  onError: defaultOnError,
 | 
				
			||||||
  onWarn: defaultOnWarn,
 | 
					  onWarn: defaultOnWarn,
 | 
				
			||||||
  comments: false
 | 
					  comments: __DEV__
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const enum TextModes {
 | 
					export const enum TextModes {
 | 
				
			||||||
@ -118,9 +118,14 @@ function createParserContext(
 | 
				
			|||||||
  rawOptions: ParserOptions
 | 
					  rawOptions: ParserOptions
 | 
				
			||||||
): ParserContext {
 | 
					): ParserContext {
 | 
				
			||||||
  const options = extend({}, defaultParserOptions)
 | 
					  const options = extend({}, defaultParserOptions)
 | 
				
			||||||
  for (const key in rawOptions) {
 | 
					
 | 
				
			||||||
 | 
					  let key: keyof ParserOptions
 | 
				
			||||||
 | 
					  for (key in rawOptions) {
 | 
				
			||||||
    // @ts-ignore
 | 
					    // @ts-ignore
 | 
				
			||||||
    options[key] = rawOptions[key] || defaultParserOptions[key]
 | 
					    options[key] =
 | 
				
			||||||
 | 
					      rawOptions[key] === undefined
 | 
				
			||||||
 | 
					        ? defaultParserOptions[key]
 | 
				
			||||||
 | 
					        : rawOptions[key]
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return {
 | 
					  return {
 | 
				
			||||||
    options,
 | 
					    options,
 | 
				
			||||||
@ -282,12 +287,8 @@ function parseChildren(
 | 
				
			|||||||
          node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
 | 
					          node.content = node.content.replace(/[\t\r\n\f ]+/g, ' ')
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      // also remove comment nodes in prod by default
 | 
					      // Remove comment nodes if desired by configuration.
 | 
				
			||||||
      if (
 | 
					      else if (node.type === NodeTypes.COMMENT && !context.options.comments) {
 | 
				
			||||||
        !__DEV__ &&
 | 
					 | 
				
			||||||
        node.type === NodeTypes.COMMENT &&
 | 
					 | 
				
			||||||
        !context.options.comments
 | 
					 | 
				
			||||||
      ) {
 | 
					 | 
				
			||||||
        removedWhitespace = true
 | 
					        removedWhitespace = true
 | 
				
			||||||
        nodes[i] = null as any
 | 
					        nodes[i] = null as any
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user