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 = - / +

keep

diff --git a/playground/html/tag-removal/stylesheet.html b/playground/html/tag-removal/stylesheet.html new file mode 100644 index 00000000000000..6254c56881bc62 --- /dev/null +++ b/playground/html/tag-removal/stylesheet.html @@ -0,0 +1,2 @@ + +

keep

diff --git a/playground/html/vite.config.js b/playground/html/vite.config.js index 4cbf0c145a41c6..78afc2a9426c4b 100644 --- a/playground/html/vite.config.js +++ b/playground/html/vite.config.js @@ -35,6 +35,8 @@ const input = { write: resolve(dirname, 'write.html'), 'transform-inline-js': resolve(dirname, 'transform-inline-js.html'), malformedUrl: resolve(dirname, 'malformed-url.html'), + tagRemovalModuleScript: resolve(dirname, 'tag-removal/module-script.html'), + tagRemovalStylesheet: resolve(dirname, 'tag-removal/stylesheet.html'), // resolved from `process.cwd()` by Rolldown (resolved from `root` by vite's resolver first) relativeInput: relative( process.cwd(), @@ -75,6 +77,9 @@ export default defineConfig({ throw new Error('pre transform applied at wrong time!') } + // Keep adjacent tags on the first line for the tag removal regression. + if (filename.includes('/tag-removal/')) return + const doctypeRE = //i if (doctypeRE.test(html)) return From e8d3ad210c7c5bdb74608e6b8f047d84d9809f4f Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Thu, 5 Nov 2026 13:56:51 +0900 Subject: [PATCH 4/4] refactor: tweak --- packages/vite/src/node/plugins/html.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 3cec6c3dc01fbe..b361bb63d111cf 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -486,9 +486,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (startOffset === 0) return 0 // Gets the offset for the start of the line including the - // newline trailing the previous node - const lineStartOffset = - startOffset - node.sourceCodeLocation!.startCol + // newline trailing the previous node. + // Columns are one-based and offsets are zero-based, so their difference + // is -1 on the first line. Clamp to 0 because there is no preceding newline. + const lineStartOffset = Math.max( + 0, + startOffset - node.sourceCodeLocation!.startCol, + ) // // @@ -503,14 +507,14 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // previous newline, we cannot strip it out without risking content deletion. let isLineEmpty = false try { - const line = s.slice(Math.max(0, lineStartOffset), startOffset) + const line = s.slice(lineStartOffset, startOffset) isLineEmpty = !line.trim() } catch { // magic-string may throw if there's some content removed in the sliced string, // which we ignore and assume the line is not empty } - return isLineEmpty ? Math.max(0, lineStartOffset) : startOffset + return isLineEmpty ? lineStartOffset : startOffset } // pre-transform