fix(plugin-container): add defensive check for undefined transform handler #21180
+3
−0
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
This PR fixes an intermittent crash that causes the error
Cannot read properties of undefined (reading 'call')during HMR and server restarts.Fixes #21162
Problem
This error occurs intermittently and repeatedly, primarily when:
package.jsonThe error appears in both the browser and terminal, effectively stopping the HMR process and requiring a manual server restart. This is particularly common with Astro + Tailwind + React setups.
Root Cause
vite-plugin-reactintentionally removes itstransformhook during theconfigResolvedphase for performance optimization (source)package.jsonchanges), thepluginContainermay iterate over cached plugin references where thetransformhook has been dynamically deletedgetHookHandler(plugin.transform)returnsundefined, and attempting to call it throws the errorCredit to @sapphi-red for identifying the root cause.
Solution
Added a defensive check in
pluginContainer.tsbefore calling the transform handler: