Skip to content
Merged
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
1 change: 1 addition & 0 deletions .oxfmtrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 8 additions & 4 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

// <previous-line-node></previous-line-node>
// <target-node></target-node>
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isBundled,
isServe,
page,
readFile,
serverLogs,
untilBrowserLogAfter,
viteServer,
Expand Down Expand Up @@ -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')
}
})
}
})
2 changes: 2 additions & 0 deletions playground/html/tag-removal/module-script.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script type="module" src="/main.js"></script><script type="module" src="/nested/nested.js"></script>
<h1>keep</h1>
2 changes: 2 additions & 0 deletions playground/html/tag-removal/stylesheet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script type="module" src="/main.js"></script><link rel="stylesheet" href="/main.css">
<h1>keep</h1>
5 changes: 5 additions & 0 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 = /<!doctype html>/i
if (doctypeRE.test(html)) return

Expand Down
Loading