Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-core): comments parser option #1858

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/compiler-core/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
11 changes: 8 additions & 3 deletions packages/compiler-core/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down