chore: improve import-analysis error message for inline <script> HTML termination #21150 #21151
+20
−7
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.
What is this PR solving?
-This PR improves the error message produced by the vite:import-analysis plugin when an inline <script> in an HTML file is prematurely terminated because the literal sequence </script> appears inside a JavaScript string or template literal.
The updated message clearly explains why the script was truncated and how to fix it (escape as </script> or move the code to an external file), making the error much easier to understand for users.
-Fixes #21150.
-Leaving the existing generic error message as-is.
This was rejected because it provides no guidance for a very common and confusing HTML parsing pitfall.
Always showing the </script> hint.
This would introduce noise for non-HTML importers such as .js, .ts, or .vue.
The chosen approach — showing the note only for .html/.htm importers — keeps the message relevant and minimal.
-This is a messaging-only change; no logic or behavior is modified.
The only added check is isHtmlImporter, which ensures the hint appears only for HTML files.
Reviewers may want to confirm that this condition aligns with Vite's existing conventions for detecting HTML importers.
-Previously Vite produced an unclear message:
([vite] (client) Pre-transform error: Failed to parse source for import analysis because the content contains invalid JS syntax.
You may need to install appropriate plugins to handle the .js file format...)
This was especially confusing when triggered by the following valid HTML:
<!doctype html>
<script type="module"> // problematic line: HTML parser will see </script> unless escaped const s = `</script>`; </script>