From 29f46b63430a6d0cd65f5f8eafdf0de675fd3569 Mon Sep 17 00:00:00 2001 From: zhangshengming <1464963064@qq.com> Date: Sun, 18 Oct 2026 21:43:19 +0800 Subject: [PATCH 1/4] fix(html): remove adjacent module tags on the first line (fix #23819) --- packages/vite/src/node/__tests__/html.spec.ts | 80 ++++++++++++++++++- packages/vite/src/node/plugins/html.ts | 2 +- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/__tests__/html.spec.ts b/packages/vite/src/node/__tests__/html.spec.ts index 0eff4742628c20..b67e54376d68c6 100644 --- a/packages/vite/src/node/__tests__/html.spec.ts +++ b/packages/vite/src/node/__tests__/html.spec.ts @@ -1,7 +1,85 @@ -import type { OutputBundle, OutputChunk } from 'rolldown' +import { resolve } from 'node:path' +import type { OutputBundle, OutputChunk, RolldownOutput } from 'rolldown' import { describe, expect, test } from 'vitest' +import { build } from '../build' import { getCssFilesForChunk } from '../plugins/html' +describe('HTML tag removal', () => { + describe.each([ + ['module script', ''], + ['stylesheet', ''], + ])('module script followed by %s', (kind, tag) => { + test.each([ + ['', ''], + ['', '\n'], + ['', '\r\n'], + ['\n ', ''], + ['
keep
', ''], + ])( + 'removes tags with prefix %j and trailing %j (#23819)', + async (prefix, trailing) => { + const result = (await build({ + root: resolve(import.meta.dirname, 'packages/build-project'), + configFile: false, + logLevel: 'silent', + build: { + write: false, + minify: false, + modulePreload: false, + }, + plugins: [ + { + name: 'test-html-tag-removal', + transformIndexHtml: { + order: 'pre', + handler: () => + prefix + + '' + + tag + + trailing, + }, + resolveId(id) { + if (['entry.js', 'other.js', 'style.css'].includes(id)) { + return '\0' + id + } + }, + load(id) { + if (id === '\0entry.js') return 'console.log("module a")' + if (id === '\0other.js') return 'console.log("module b")' + if (id === '\0style.css') return 'body { color: red }' + }, + }, + ], + })) as RolldownOutput + + const html = result.output.find( + (output) => + output.type === 'asset' && output.fileName === 'index.html', + )! + const htmlSource = html.type === 'asset' && html.source + expect(htmlSource).not.toMatch(/(?:entry\.js|other\.js|style\.css)/) + const scriptTag = + /'], - ['stylesheet', ''], - ])('module script followed by %s', (kind, tag) => { - test.each([ - ['', ''], - ['', '\n'], - ['', '\r\n'], - ['\n ', ''], - ['keep
', ''], - ])( - 'removes tags with prefix %j and trailing %j (#23819)', - async (prefix, trailing) => { - const result = (await build({ - root: resolve(import.meta.dirname, 'packages/build-project'), - configFile: false, - logLevel: 'silent', - build: { - write: false, - minify: false, - modulePreload: false, - }, - plugins: [ - { - name: 'test-html-tag-removal', - transformIndexHtml: { - order: 'pre', - handler: () => - prefix + - '' + - tag + - trailing, - }, - resolveId(id) { - if (['entry.js', 'other.js', 'style.css'].includes(id)) { - return '\0' + id - } - }, - load(id) { - if (id === '\0entry.js') return 'console.log("module a")' - if (id === '\0other.js') return 'console.log("module b")' - if (id === '\0style.css') return 'body { color: red }' - }, - }, - ], - })) as RolldownOutput - - const html = result.output.find( - (output) => - output.type === 'asset' && output.fileName === 'index.html', - )! - const htmlSource = html.type === 'asset' && html.source - expect(htmlSource).not.toMatch(/(?:entry\.js|other\.js|style\.css)/) - const scriptTag = - / +