1- import { defineConfig , splitVendorChunkPlugin } from 'vite' ;
2- import react from '@vitejs/plugin-react' ;
3- import checker from 'vite-plugin-checker' ;
1+ import { defineConfig , splitVendorChunkPlugin } from "vite" ;
2+ import react from "@vitejs/plugin-react" ;
3+ import checker from "vite-plugin-checker" ;
4+ import legacy from "@vitejs/plugin-legacy" ;
5+ import { readFileSync , writeFileSync } from "fs" ;
6+ import path from "path" ;
7+
8+ function cspPlugin ( ) {
9+ return {
10+ name : "csp-plugin" ,
11+ closeBundle ( ) {
12+ // Path to the generated HTML file after build
13+ const htmlFilePath = path . resolve ( __dirname , "build/index.html" ) ;
14+
15+ // Read the HTML file content
16+ let htmlContent = readFileSync ( htmlFilePath , "utf-8" ) ;
17+
18+ // Insert the CSP meta tag in the HTML <head> section
19+ htmlContent = htmlContent . replaceAll (
20+ / < s c r i p t n o m o d u l e > / gi,
21+ `<script nomodule content="__CSP_NONCE__">`
22+ ) ;
23+
24+ htmlContent = htmlContent . replaceAll (
25+ / < s c r i p t t y p e = " m o d u l e " > / gi,
26+ `<script type="module" content="__CSP_NONCE__">`
27+ ) ;
28+
29+ htmlContent = htmlContent . replaceAll (
30+ / < s c r i p t n o m o d u l e c r o s s o r i g i n i d = " v i t e - l e g a c y - e n t r y " / gi,
31+ `<script nomodule crossorigin id="vite-legacy-entry" content="__CSP_NONCE__"`
32+ ) ;
33+
34+ // Write the updated HTML back to the file
35+ writeFileSync ( htmlFilePath , htmlContent ) ;
36+ } ,
37+ } ;
38+ }
439
540export default defineConfig ( ( env ) => {
641 return {
@@ -23,12 +58,17 @@ export default defineConfig((env) => {
2358 } ,
2459 plugins : [
2560 react ( ) ,
26- env . mode !== 'test' && checker ( {
27- typescript : true ,
28- eslint : {
29- lintCommand : 'eslint --max-warnings=0 src' ,
30- } ,
61+ legacy ( {
62+ targets : [ 'edge 18' ] ,
3163 } ) ,
64+ cspPlugin ( ) ,
65+ env . mode !== 'test' &&
66+ checker ( {
67+ typescript : true ,
68+ eslint : {
69+ lintCommand : 'eslint --max-warnings=0 src' ,
70+ } ,
71+ } ) ,
3272 splitVendorChunkPlugin ( ) ,
3373 ] ,
3474 test : {
0 commit comments