@@ -92,6 +92,7 @@ export async function build(
92
92
93
93
// For cache-breaking we rename most assets to include content hashes.
94
94
const aliases = new Map < string , string > ( ) ;
95
+ const plainaliases = new Map < string , string > ( ) ;
95
96
96
97
// Add the search bundle and data, if needed.
97
98
if ( config . search ) {
@@ -106,6 +107,7 @@ export async function build(
106
107
107
108
// Generate the client bundles (JavaScript and styles). TODO Use a content
108
109
// hash, or perhaps the Framework version number for built-in modules.
110
+ const delayedStylesheets = new Set < string > ( ) ;
109
111
if ( addPublic ) {
110
112
for ( const path of globalImports ) {
111
113
if ( path . startsWith ( "/_observablehq/" ) && path . endsWith ( ".js" ) ) {
@@ -136,14 +138,9 @@ export async function build(
136
138
const sourcePath = await populateNpmCache ( root , path ) ; // TODO effects
137
139
await effects . copyFile ( sourcePath , path ) ;
138
140
} else if ( ! / ^ \w + : / . test ( specifier ) ) {
139
- const sourcePath = join ( root , specifier ) ;
140
- effects . output . write ( `${ faint ( "build" ) } ${ sourcePath } ${ faint ( "→" ) } ` ) ;
141
- const contents = await bundleStyles ( { path : sourcePath , minify : true } ) ;
142
- const hash = createHash ( "sha256" ) . update ( contents ) . digest ( "hex" ) . slice ( 0 , 8 ) ;
143
- const ext = extname ( specifier ) ;
144
- const alias = `/${ join ( "_import" , dirname ( specifier ) , `${ basename ( specifier , ext ) } .${ hash } ${ ext } ` ) } ` ;
145
- aliases . set ( resolveStylesheetPath ( root , specifier ) , alias ) ;
146
- await effects . writeFile ( alias , contents ) ;
141
+ // Uses a side effect to register file assets on custom stylesheets
142
+ delayedStylesheets . add ( specifier ) ;
143
+ await bundleStyles ( { path : join ( root , specifier ) , files} ) ;
147
144
}
148
145
}
149
146
}
@@ -170,9 +167,24 @@ export async function build(
170
167
const ext = extname ( file ) ;
171
168
const alias = `/${ join ( "_file" , dirname ( file ) , `${ basename ( file , ext ) } .${ hash } ${ ext } ` ) } ` ;
172
169
aliases . set ( loaders . resolveFilePath ( file ) , alias ) ;
170
+ plainaliases . set ( file , alias ) ;
173
171
await effects . writeFile ( alias , contents ) ;
174
172
}
175
173
174
+ // Write delayed stylesheets
175
+ if ( addPublic ) {
176
+ for ( const specifier of delayedStylesheets ) {
177
+ const sourcePath = join ( root , specifier ) ;
178
+ effects . output . write ( `${ faint ( "build" ) } ${ sourcePath } ${ faint ( "→" ) } ` ) ;
179
+ const contents = await bundleStyles ( { path : sourcePath , minify : true , aliases : plainaliases } ) ;
180
+ const hash = createHash ( "sha256" ) . update ( contents ) . digest ( "hex" ) . slice ( 0 , 8 ) ;
181
+ const ext = extname ( specifier ) ;
182
+ const alias = `/${ join ( "_import" , dirname ( specifier ) , `${ basename ( specifier , ext ) } .${ hash } ${ ext } ` ) } ` ;
183
+ aliases . set ( resolveStylesheetPath ( root , specifier ) , alias ) ;
184
+ await effects . writeFile ( alias , contents ) ;
185
+ }
186
+ }
187
+
176
188
// Download npm imports. TODO It might be nice to use content hashes for
177
189
// these, too, but it would involve rewriting the files since populateNpmCache
178
190
// doesn’t let you pass in a resolver.
0 commit comments