Repository navigation
Conversation
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.
Contributor
|
Duplicate of #23823 |
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #23819
When the first line of
index.htmlcontains two adjacent module scripts with nothing between them,vite buildbundled both modules but removed only the first original<script>tag. The second tag was left indist/index.html(requesting a/b.jsthat does not exist → 404), and when it ended the file its final>was cut off:Root cause
In
nodeStartWithLeadingWhitespace(packages/vite/src/node/plugins/html.ts), parse5'sstartColis 1-based, so for a node on line 1: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 returnedlineStartOffsetwithout clamping it (theslicetwo lines above is already wrapped inMath.max(0, ...)).magic-stringinterprets negative indices from the end of the string, sos.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:
Tests
index.htmlholding two adjacent module scripts (no trailing newline). It fails onmainbecause the/b.jstag survives, and passes with the fix; it also asserts both modules are still bundled.plugins(272) andbuild(104) unit suites, and thehtmlplayground integration tests in both build (54) and serve (65) modes all pass.