@@ -3,6 +3,8 @@ import { readFile } from 'node:fs/promises';
33import { isAbsolute } from 'node:path' ;
44import { fileURLToPath } from 'node:url' ;
55import type { Plugin } from 'vite' ;
6+ import { getAlgorithm , shouldTrackCspHashes } from '../../core/csp/common.js' ;
7+ import { generateCspDigest } from '../../core/encryption.js' ;
68import { collectErrorMetadata } from '../../core/errors/dev/utils.js' ;
79import { AstroError , AstroErrorData , isAstroError } from '../../core/errors/index.js' ;
810import type { Logger } from '../../core/logger/core.js' ;
@@ -56,7 +58,7 @@ interface Options {
5658}
5759
5860export function fontsPlugin ( { settings, sync, logger } : Options ) : Plugin {
59- if ( ! settings . config . experimental . fonts ) {
61+ if ( sync || ! settings . config . experimental . fonts ) {
6062 // This is required because the virtual module may be imported as
6163 // a side effect
6264 // TODO: remove once fonts are stabilized
@@ -171,6 +173,20 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
171173 // to avoid locking memory
172174 fontFileDataMap = res . fontFileDataMap ;
173175 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+ }
174190 }
175191
176192 return {
@@ -271,7 +287,7 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
271287 }
272288 } ,
273289 async buildEnd ( ) {
274- if ( sync || settings . config . experimental . fonts ! . length === 0 ) {
290+ if ( settings . config . experimental . fonts ! . length === 0 ) {
275291 cleanup ( ) ;
276292 return ;
277293 }
0 commit comments