Skip to content

Commit b295400

Browse files
committed
fix: preserve html comments during dev
fix #1420
1 parent b58c860 commit b295400

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

packages/playground/html/__tests__/html.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ function testPage(isNested: boolean) {
6363

6464
describe('main', () => {
6565
testPage(false)
66+
67+
test('preserve comments', async () => {
68+
const html = await page.innerHTML('body')
69+
expect(html).toMatch(`<!-- comment one -->`)
70+
expect(html).toMatch(`<!-- comment two -->`)
71+
})
6672
})
6773

6874
describe('nested', () => {

packages/playground/html/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<link rel="stylesheet" href="./main.css">
22

3+
<!-- comment one -->
34
<h1>Hello</h1>
5+
<!-- comment two -->
46

57
<script type="module" src="./main.js"></script>

packages/vite/src/node/server/middlewares/indexHtml.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ import { CLIENT_PUBLIC_PATH, FS_PREFIX } from '../../constants'
1515

1616
const devHtmlHook: IndexHtmlTransformHook = (html, { path }) => {
1717
let index = -1
18+
const comments: string[] = []
19+
1820
html = html
19-
.replace(htmlCommentRE, '')
21+
.replace(htmlCommentRE, (m) => {
22+
comments.push(m)
23+
return `<!--VITE_COMMENT_${comments.length - 1}-->`
24+
})
2025
.replace(scriptRE, (_match, _openTag, script) => {
2126
index++
2227
if (script) {
@@ -25,6 +30,7 @@ const devHtmlHook: IndexHtmlTransformHook = (html, { path }) => {
2530
}
2631
return _match
2732
})
33+
.replace(/<!--VITE_COMMENT_(\d+)-->/g, (_, i) => comments[i])
2834

2935
return {
3036
html,

0 commit comments

Comments
 (0)