-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
fix(compiler-sfc): custom lang import usage detection for non-inline templates #13858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -448,7 +448,10 @@ export function hmrShouldReload( | |||||||||||||||||||||||||||||
for (const key in prevImports) { | ||||||||||||||||||||||||||||||
// if an import was previous unused, but now is used, we need to force | ||||||||||||||||||||||||||||||
// reload so that the script now includes that import. | ||||||||||||||||||||||||||||||
if (!prevImports[key].isUsedInTemplate && isImportUsed(key, next)) { | ||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||
!prevImports[key].isUsedInTemplate && | ||||||||||||||||||||||||||||||
isImportUsed(key, next.template!.content!, next.template!.ast!) | ||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||
Comment on lines
+451
to
+454
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Potential runtime crash when template/AST is absent next.template or next.template.ast can be null (e.g., external src templates). The non-null assertions will throw at runtime during HMR checks. Guard before calling isImportUsed. Apply this diff: - if (
- !prevImports[key].isUsedInTemplate &&
- isImportUsed(key, next.template!.content!, next.template!.ast!)
- ) {
+ // skip when there is no in-file template or no parsed AST (e.g. src=)
+ if (!next.template || !next.template.ast) {
+ continue
+ }
+ if (
+ !prevImports[key].isUsedInTemplate &&
+ isImportUsed(key, next.template.content, next.template.ast)
+ ) {
return true
} Optionally short-circuit earlier: ) {
return false
}
+ if (!next.template || !next.template.ast) {
+ return false
+ } 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
return true | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: using preprocessed AST with raw (un-preprocessed) content can mis-detect template imports
When lang is set (e.g., pug), compileSFCTemplate returns an AST for the preprocessed template, but content passed to isImportUsed is the raw SFC template (pug). This mismatch can lead to false negatives/positives. Also, ast may be undefined on preprocessing failure; defaulting to “used” is safer in dev/non-inline to avoid runtime breakage.
Apply the following:
📝 Committable suggestion
🤖 Prompt for AI Agents