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

fix: do not remove comment when there is an entry option #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-html",
"version": "3.2.0",
"version": "3.2.0-o.3",
"description": "A plugin for vite to Minimize index.html and use lodash.template template syntax in index.html",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -52,7 +52,7 @@
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.1",
"html-minifier-terser": "^6.1.0",
"node-html-parser": "^5.3.3",
"node-html-parser": "^6.1.4",
"pathe": "^0.2.0"
},
"peerDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ describe('function test.', () => {
<title>env-title</title>
</head>
</html>
`)
})

test('renderHtml must keep comment with entry.', async () => {
const content = await renderHtml(`
<!DOCTYPE html>
<html>
<!--comment-->
</html>
`, {
injectOptions: {},
viteConfig: {} as any,
env: {},
entry: 'fake'
},)
expect(content).toEqual(`
<!DOCTYPE html>
<html>
<!--comment-->
</html>
`)
})
})
4 changes: 2 additions & 2 deletions packages/core/src/htmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ export function removeEntryScript(html: string, verbose = false) {
if (!html) {
return html
}

const root = parse(html)
const root = parse(html, { comment: true })
const scriptNodes = root.querySelectorAll('script[type=module]') || []
const removedNode: string[] = []
scriptNodes.forEach((item) => {
Expand All @@ -268,6 +267,7 @@ export function removeEntryScript(html: string, verbose = false) {
removedNode.toString(),
)} is deleted. You may also delete it from the index.html.
`)
consola.warn(root.toString())
return root.toString()
}

Expand Down
Loading