Skip to content

Commit eab1dc2

Browse files
Singminsapphi-red
andauthored
fix(html): remove adjacent module tags on the first line (#23823)
Co-authored-by: 翠 <[email protected]>
1 parent a270391 commit eab1dc2

6 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎.oxfmtrc.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"playground/html/invalidClick.html",
3838
"playground/html/invalidEscape.html",
3939
"playground/html/valid.html",
40+
"playground/html/tag-removal/*.html",
4041
"playground/external/public/[email protected]",
4142
"playground/ssr-html/public/[email protected]",
4243
"playground/worker/classic-worker.js",

‎packages/vite/src/node/plugins/html.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
486486
if (startOffset === 0) return 0
487487

488488
// Gets the offset for the start of the line including the
489-
// newline trailing the previous node
490-
const lineStartOffset =
491-
startOffset - node.sourceCodeLocation!.startCol
489+
// newline trailing the previous node.
490+
// Columns are one-based and offsets are zero-based, so their difference
491+
// is -1 on the first line. Clamp to 0 because there is no preceding newline.
492+
const lineStartOffset = Math.max(
493+
0,
494+
startOffset - node.sourceCodeLocation!.startCol,
495+
)
492496

493497
// <previous-line-node></previous-line-node>
494498
// <target-node></target-node>
@@ -503,7 +507,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
503507
// previous newline, we cannot strip it out without risking content deletion.
504508
let isLineEmpty = false
505509
try {
506-
const line = s.slice(Math.max(0, lineStartOffset), startOffset)
510+
const line = s.slice(lineStartOffset, startOffset)
507511
isLineEmpty = !line.trim()
508512
} catch {
509513
// magic-string may throw if there's some content removed in the sliced string,

‎playground/html/__tests__/html.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isBundled,
88
isServe,
99
page,
10+
readFile,
1011
serverLogs,
1112
untilBrowserLogAfter,
1213
viteServer,
@@ -570,3 +571,19 @@ test.runIf(isServe)(
570571
)
571572
},
572573
)
574+
575+
describe.runIf(isBuild)('HTML tag removal', () => {
576+
for (const kind of ['module-script', 'stylesheet']) {
577+
test(`removes adjacent ${kind} tags (#23819)`, async () => {
578+
const path = `/tag-removal/${kind}.html`
579+
const html = readFile(`dist${path}`)
580+
expect(html).not.toMatch(/(?:main\.js|nested\.js|main\.css)/)
581+
582+
await page.goto(viteTestUrl + path)
583+
expect(await page.textContent('h1')).toBe('keep')
584+
if (kind === 'stylesheet') {
585+
expect(await getColor('h1')).toBe('blue')
586+
}
587+
})
588+
}
589+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script type="module" src="/main.js"></script><script type="module" src="/nested/nested.js"></script>
2+
<h1>keep</h1>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script type="module" src="/main.js"></script><link rel="stylesheet" href="/main.css">
2+
<h1>keep</h1>

‎playground/html/vite.config.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const input = {
3535
write: resolve(dirname, 'write.html'),
3636
'transform-inline-js': resolve(dirname, 'transform-inline-js.html'),
3737
malformedUrl: resolve(dirname, 'malformed-url.html'),
38+
tagRemovalModuleScript: resolve(dirname, 'tag-removal/module-script.html'),
39+
tagRemovalStylesheet: resolve(dirname, 'tag-removal/stylesheet.html'),
3840
// resolved from `process.cwd()` by Rolldown (resolved from `root` by vite's resolver first)
3941
relativeInput: relative(
4042
process.cwd(),
@@ -75,6 +77,9 @@ export default defineConfig({
7577
throw new Error('pre transform applied at wrong time!')
7678
}
7779

80+
// Keep adjacent tags on the first line for the tag removal regression.
81+
if (filename.includes('/tag-removal/')) return
82+
7883
const doctypeRE = /<!doctype html>/i
7984
if (doctypeRE.test(html)) return
8085

0 commit comments

Comments
 (0)