fix(module-runner): initialize CSS module cache for non-client enviro…#21675
Open
rexxars wants to merge 1 commit intovitejs:mainfrom
Open
fix(module-runner): initialize CSS module cache for non-client enviro…#21675rexxars wants to merge 1 commit intovitejs:mainfrom
rexxars wants to merge 1 commit intovitejs:mainfrom
Conversation
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
Importing CSS files via
createServerModuleRunnerin dev mode throws aTypeErrorbecause the CSS module cache is never initialized for non-client environments..module.css: TypeError: Cannot read properties of undefined (reading 'set').css: TypeError: Cannot read properties of undefined (reading 'get')Root cause
Both
cssPluginandcssPostPlugindepend on amoduleCache/cssModulesCachethat is only initialized inside thebuildStarthook. However, in the dev server's plugin container,buildStartis conditionally called per-plugin - it only fires for plugins that haveperEnvironmentStartEndDuringDev: trueor when the environment is "client". Since the CSS plugins don't set that flag, theirbuildStartcallback is skipped for the SSR environment, leaving the cache undefined.When using
middlewareMode: true, this is masked becauseinitServer()explicitly callsbuildStarton the client environment, and the shared plugin closure variable gets initialized. But without middleware mode (the commoncreateServerModuleRunnerpattern), nobuildStartfires before the first CSS transform, causing the crash.You can work around this by running
await server.pluginContainer.buildStart({})first, but it seems a little hacky/non-intuitive.Fix
configureServerhook tocssPluginthat eagerly initializesmoduleCacheandcssModulesCacheif not already set. This hook always runs duringcreateServer(), regardless of middleware mode or environment.cssPostPluginto optional chaining (?.), consistent with howcssAnalysisPluginalready accesses the same cache.Testing
Added a test that creates a server without
middlewareModeand imports both.cssand.module.cssfiles throughcreateServerModuleRunner. This test fails before the fix and passes after.