Skip to content

fix(html): remove adjacent module scripts correctly on first line (fix #23819) - #23824

Closed
szupzj18 wants to merge 1 commit into
vitejs:mainfrom
szupzj18:chris/fix/html-adjacent-module-scripts
Closed

szupzj18 wants to merge 1 commit into
vitejs:mainfrom
szupzj18:chris/fix/html-adjacent-module-scripts

Conversation

@szupzj18

Copy link
Copy Markdown

Description

Closes #23819

When the first line of index.html contains two adjacent module scripts with nothing between them, vite build bundled both modules but removed only the first original <script> tag. The second tag was left in dist/index.html (requesting a /b.js that does not exist → 404), and when it ended the file its final > was cut off:

<script type="module" crossorigin src="/assets/index-*.js"></script>
<script type="module" src="/b.js"></script

Root cause

In nodeStartWithLeadingWhitespace (packages/vite/src/node/plugins/html.ts), parse5's startCol is 1-based, so for a node on line 1:

lineStartOffset = startOffset - startCol // -1 for the second script

The first script on that line has already been removed from the MagicString, so the sliced prefix is empty and the line is considered empty. The helper then returned lineStartOffset without clamping it (the slice two lines above is already wrapped in Math.max(0, ...)). magic-string interprets negative indices from the end of the string, so s.remove(-1, endOffset) deleted [length - 1, endOffset) — the final > when the tag ends the file, and nothing visible when a newline follows.

Change

Clamp the returned offset to 0, matching the clamping already used for the slice:

return isLineEmpty ? Math.max(0, lineStartOffset) : startOffset

Tests

  • Added a build unit test with a one-line index.html holding two adjacent module scripts (no trailing newline). It fails on main because the /b.js tag survives, and passes with the fix; it also asserts both modules are still bundled.
  • Verified locally: the new spec, the plugins (272) and build (104) unit suites, and the html playground integration tests in both build (54) and serve (65) modes all pass.

When two module scripts sit adjacent on the first line of index.html,
parse5's 1-based startCol made lineStartOffset -1 for the second
script. Since the first script was already removed, the empty prefix
was treated as an empty line and the unclamped -1 was handed to
magic-string's remove(), which interprets negatives from the string
end and left the second original tag behind (cutting off its final
> when the tag ended the file). Clamp the returned offset to 0.
@btea

btea commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #23823

@btea btea marked this as a duplicate of #23823 Oct 18, 2026
@szupzj18

Copy link
Copy Markdown
Author

Thanks @btea, closing as a duplicate of #23823. Same root cause (parse5's 1-based columns making the line-start offset -1) and the same one-line clamp to 0.

@szupzj18 szupzj18 closed this Oct 18, 2026
@szupzj18
szupzj18 deleted the chris/fix/html-adjacent-module-scripts branch October 18, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build keeps the second of two adjacent module scripts on the first line of index.html (and cuts its final > at EOF)

2 participants