@@ -3,6 +3,8 @@ import { readFile } from 'node:fs/promises';
3
3
import { isAbsolute } from 'node:path' ;
4
4
import { fileURLToPath } from 'node:url' ;
5
5
import type { Plugin } from 'vite' ;
6
+ import { getAlgorithm , shouldTrackCspHashes } from '../../core/csp/common.js' ;
7
+ import { generateCspDigest } from '../../core/encryption.js' ;
6
8
import { collectErrorMetadata } from '../../core/errors/dev/utils.js' ;
7
9
import { AstroError , AstroErrorData , isAstroError } from '../../core/errors/index.js' ;
8
10
import type { Logger } from '../../core/logger/core.js' ;
@@ -56,7 +58,7 @@ interface Options {
56
58
}
57
59
58
60
export function fontsPlugin ( { settings, sync, logger } : Options ) : Plugin {
59
- if ( ! settings . config . experimental . fonts ) {
61
+ if ( sync || ! settings . config . experimental . fonts ) {
60
62
// This is required because the virtual module may be imported as
61
63
// a side effect
62
64
// TODO: remove once fonts are stabilized
@@ -171,6 +173,20 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
171
173
// to avoid locking memory
172
174
fontFileDataMap = res . fontFileDataMap ;
173
175
consumableMap = res . consumableMap ;
176
+
177
+ // Handle CSP
178
+ if ( shouldTrackCspHashes ( settings . config . experimental . csp ) ) {
179
+ const algorithm = getAlgorithm ( settings . config . experimental . csp ) ;
180
+
181
+ // Generate a hash for each style we generate
182
+ for ( const { css } of consumableMap . values ( ) ) {
183
+ settings . injectedCsp . styleHashes . push ( await generateCspDigest ( css , algorithm ) ) ;
184
+ }
185
+ const resources = urlResolver . getCspResources ( ) ;
186
+ for ( const resource of resources ) {
187
+ settings . injectedCsp . fontResources . add ( resource ) ;
188
+ }
189
+ }
174
190
}
175
191
176
192
return {
@@ -271,7 +287,7 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
271
287
}
272
288
} ,
273
289
async buildEnd ( ) {
274
- if ( sync || settings . config . experimental . fonts ! . length === 0 ) {
290
+ if ( settings . config . experimental . fonts ! . length === 0 ) {
275
291
cleanup ( ) ;
276
292
return ;
277
293
}
0 commit comments