diff --git a/.oxfmtrc.json b/.oxfmtrc.json index 58260ed5c2daa7..b8c88eeaba4ea7 100644 --- a/.oxfmtrc.json +++ b/.oxfmtrc.json @@ -37,6 +37,7 @@ "playground/html/invalidClick.html", "playground/html/invalidEscape.html", "playground/html/valid.html", + "playground/html/tag-removal/*.html", "playground/external/public/slash@3.0.0.js", "playground/ssr-html/public/slash@3.0.0.js", "playground/worker/classic-worker.js", diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 238a7d8fc27a43..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,7 +507,7 @@ 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, diff --git a/playground/html/__tests__/html.spec.ts b/playground/html/__tests__/html.spec.ts index 19e015a5136f70..0caa60c7363909 100644 --- a/playground/html/__tests__/html.spec.ts +++ b/playground/html/__tests__/html.spec.ts @@ -7,6 +7,7 @@ import { isBundled, isServe, page, + readFile, serverLogs, untilBrowserLogAfter, viteServer, @@ -570,3 +571,19 @@ test.runIf(isServe)( ) }, ) + +describe.runIf(isBuild)('HTML tag removal', () => { + for (const kind of ['module-script', 'stylesheet']) { + test(`removes adjacent ${kind} tags (#23819)`, async () => { + const path = `/tag-removal/${kind}.html` + const html = readFile(`dist${path}`) + expect(html).not.toMatch(/(?:main\.js|nested\.js|main\.css)/) + + await page.goto(viteTestUrl + path) + expect(await page.textContent('h1')).toBe('keep') + if (kind === 'stylesheet') { + expect(await getColor('h1')).toBe('blue') + } + }) + } +}) diff --git a/playground/html/tag-removal/module-script.html b/playground/html/tag-removal/module-script.html new file mode 100644 index 00000000000000..ec15b8d3d4fa8f --- /dev/null +++ b/playground/html/tag-removal/module-script.html @@ -0,0 +1,2 @@ + +

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