Skip to content

Commit 2e08252

Browse files
committed
fix: do not remove comment when there is an entry option
1 parent 841d4ef commit 2e08252

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/core/src/__tests__/html.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ describe('function test.', () => {
5656
<title>env-title</title>
5757
</head>
5858
</html>
59+
`)
60+
})
61+
62+
test('renderHtml must keep comment with entry.', async () => {
63+
const content = await renderHtml(`
64+
<!DOCTYPE html>
65+
<html>
66+
<!--comment-->
67+
</html>
68+
`, {
69+
injectOptions: {},
70+
viteConfig: {} as any,
71+
env: {},
72+
entry: 'fake'
73+
},)
74+
expect(content).toEqual(`
75+
<!DOCTYPE html>
76+
<html>
77+
<!--comment-->
78+
</html>
5979
`)
6080
})
6181
})

packages/core/src/htmlPlugin.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { InjectOptions, PageOption, Pages, UserOptions } from './typing'
33
import { render } from 'ejs'
44
import { isDirEmpty, loadEnv } from './utils'
55
import { normalizePath } from 'vite'
6-
import { parse } from 'node-html-parser'
6+
import parse from 'node-html-parser'
77
import fs from 'fs-extra'
88
import path from 'pathe'
99
import fg from 'fast-glob'
@@ -255,7 +255,7 @@ export function removeEntryScript(html: string, verbose = false) {
255255
return html
256256
}
257257

258-
const root = parse(html)
258+
const root = parse(html, { comment: true })
259259
const scriptNodes = root.querySelectorAll('script[type=module]') || []
260260
const removedNode: string[] = []
261261
scriptNodes.forEach((item) => {
@@ -268,6 +268,7 @@ export function removeEntryScript(html: string, verbose = false) {
268268
removedNode.toString(),
269269
)} is deleted. You may also delete it from the index.html.
270270
`)
271+
consola.warn(root.toString())
271272
return root.toString()
272273
}
273274

0 commit comments

Comments
 (0)