@@ -87,10 +87,16 @@ function resolveAssetPlaceholders(graph: ModuleGraph, code: string): string {
8787 * themselves are not stable between builds.
8888 *
8989 * CSS modules are a special case: Vite extracts their content during the
90- * prerender build, leaving `code` as an empty string. For those modules, the
91- * source file is read from disk so that CSS edits invalidate the hash.
90+ * prerender build, leaving `code` as an empty string. For those modules the
91+ * compiled CSS captured during `transform` is used instead, which reflects
92+ * the full output of any CSS preprocessor (Sass partials, Less imports,
93+ * `additionalData`, etc.) rather than just the entry file on disk (#17974).
9294 */
93- function hashModules ( graph : ModuleGraph , sortedIds : string [ ] ) : string {
95+ function hashModules (
96+ graph : ModuleGraph ,
97+ sortedIds : string [ ] ,
98+ compiledCss : Map < string , string > ,
99+ ) : string {
94100 const hasher = crypto . createHash ( 'sha256' ) ;
95101 for ( const id of sortedIds ) {
96102 hasher . update ( id ) ;
@@ -99,14 +105,21 @@ function hashModules(graph: ModuleGraph, sortedIds: string[]): string {
99105 if ( code != null && code . length > 0 ) {
100106 hasher . update ( resolveAssetPlaceholders ( graph , code ) ) ;
101107 } else if ( CSS_LANGS_RE . test ( id ) ) {
102- // Vite extracts CSS into separate assets, so the module's `code` in the
103- // prerender bundle is empty. Read the source file so that stylesheet
104- // changes are reflected in the dependency hash (#17704).
105- try {
106- hasher . update ( nodeFs . readFileSync ( removeQueryString ( id ) , 'utf-8' ) ) ;
107- } catch {
108- // Virtual CSS or unreadable file — skip. The worst case is a
109- // cache miss (re-render), never a stale hit.
108+ // Vite extracts CSS into separate assets, so the module's `code` in
109+ // the prerender bundle is empty. Use the compiled CSS captured during
110+ // transform, which includes resolved preprocessor imports (#17974).
111+ // Fall back to the raw source file for modules the transform hook
112+ // did not see (e.g. virtual CSS modules).
113+ const compiled = compiledCss . get ( id ) ;
114+ if ( compiled != null ) {
115+ hasher . update ( compiled ) ;
116+ } else {
117+ try {
118+ hasher . update ( nodeFs . readFileSync ( removeQueryString ( id ) , 'utf-8' ) ) ;
119+ } catch {
120+ // Virtual CSS or unreadable file — skip. The worst case is a
121+ // cache miss (re-render), never a stale hit.
122+ }
110123 }
111124 }
112125 hasher . update ( '\n' ) ;
@@ -119,7 +132,10 @@ function hashModules(graph: ModuleGraph, sortedIds: string[]): string {
119132 * Strongly connected components collapse cycles into a DAG, whose hashes can be
120133 * folded into every importer without walking shared dependencies again for each root.
121134 */
122- function createTransitiveGraphCache ( graph : HashableModuleGraph ) : TransitiveGraphCache {
135+ function createTransitiveGraphCache (
136+ graph : HashableModuleGraph ,
137+ compiledCss : Map < string , string > = new Map < string , string > ( ) ,
138+ ) : TransitiveGraphCache {
123139 const modules = new Map < string , HashableModuleInfo | null > ( ) ;
124140 const dependencies = new Map < string , string [ ] > ( ) ;
125141 const excludedModules = new Set < string > ( ) ;
@@ -213,7 +229,7 @@ function createTransitiveGraphCache(graph: HashableModuleGraph): TransitiveGraph
213229 const ready = unresolvedDependencies . flatMap ( ( count , index ) => ( count === 0 ? [ index ] : [ ] ) ) ;
214230 for ( const componentIndex of ready ) {
215231 const hasher = crypto . createHash ( 'sha256' ) ;
216- hasher . update ( hashModules ( graph , components [ componentIndex ] ) ) ;
232+ hasher . update ( hashModules ( graph , components [ componentIndex ] , compiledCss ) ) ;
217233 const dependencyHashes = [ ...componentDependencies [ componentIndex ] ]
218234 . map ( ( dependencyIndex ) => componentHashes . get ( dependencyIndex ) ! )
219235 . sort ( ) ;
@@ -357,6 +373,12 @@ function collectContentEntryHashes(
357373 * render modules sit behind `content-data`-pruned bridges.
358374 */
359375export function pluginIncremental ( internals : BuildInternals , root : URL ) : VitePlugin {
376+ // Compiled CSS captured during transform, keyed by module id. Preprocessor
377+ // output (Sass, Less, etc.) includes resolved partials and additionalData,
378+ // so hashing it instead of the raw source detects changes in transitive
379+ // preprocessor dependencies that are invisible to the Vite module graph.
380+ const compiledCss = new Map < string , string > ( ) ;
381+
360382 return {
361383 name : '@astro/plugin-incremental' ,
362384 applyToEnvironment ( environment ) {
@@ -365,13 +387,21 @@ export function pluginIncremental(internals: BuildInternals, root: URL): VitePlu
365387 environment . name === ASTRO_VITE_ENVIRONMENT_NAMES . client
366388 ) ;
367389 } ,
390+ transform ( code , id ) {
391+ if (
392+ this . environment ?. name === ASTRO_VITE_ENVIRONMENT_NAMES . prerender &&
393+ CSS_LANGS_RE . test ( id )
394+ ) {
395+ compiledCss . set ( id , code ) ;
396+ }
397+ } ,
368398 generateBundle ( ) {
369399 if ( this . environment ?. name === ASTRO_VITE_ENVIRONMENT_NAMES . client ) {
370400 foldClientDependencies ( this , internals ) ;
371401 return ;
372402 }
373403
374- const transitiveGraph = createTransitiveGraphCache ( this ) ;
404+ const transitiveGraph = createTransitiveGraphCache ( this , compiledCss ) ;
375405 const hashes = new Map < string , string > ( ) ;
376406 const serverIslandComponents = new Set < string > ( ) ;
377407 for ( const id of this . getModuleIds ( ) ) {
0 commit comments