A Vite plugin that generates Content Security Policy meta tags and HTTP headers (CSP, CSP Report-Only, Report-To, Referrer-Policy) from all sources in the final HTML. Current stable version is 1.1.2 (June 2022). Uses csp-typed-directives for typed CSP directives. Compatible with csp-html-webpack-plugin configuration. No official releases since 2022; project appears in maintenance mode. Key differentiators: automated policy generation from Vite build output, TypeScript support, and header validation.
npm install vite-plugin-cspVerified import paths — ran on the pinned version, not inferred.
Demonstrates minimal Vite CSP plugin configuration with custom policies.
Use absolute paths or resolve manually with path.resolve(__dirname, ...).
Do not use in SSR environments; avoids nonce-based CSP.
Add other directives manually if needed; plugin ignores them.
Manually include inline script hashes in policy.
Evaluate vite-plugin-csp2 or other actively maintained CSP plugins.
Use import ViteCspPlugin from 'vite-plugin-csp' (no braces).
Add "types": ["vite-plugin-csp"] in tsconfig or ensure moduleResolution is 'node'.
Remove unsupported directives from policy; only 'script-src', 'style-src', and 'report-to' work.
Set nonceEnabled: false or avoid SSR.