Skip to content

Commit 52c4be4

Browse files
committed
Add Legacy Support for Lico
Added Legacy Support for Lico via legacy plugin and added csp headers dynamically in build html. Related: #143
1 parent d829bd9 commit 52c4be4

File tree

4 files changed

+1454
-18
lines changed

4 files changed

+1454
-18
lines changed

identifier/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head data-kopano-build="%VITE_KOPANO_BUILD%">
44
<meta charset="utf-8">
5+
<meta http-equiv="Content-Security-Policy">
56
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
67
<meta name="theme-color" content="#ffffff">
78
<link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">

identifier/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@typescript-eslint/eslint-plugin": "^6.11.0",
5151
"@typescript-eslint/parser": "^6.9.0",
5252
"@typescript-eslint/typescript-estree": "^6.11.0",
53+
"@vitejs/plugin-legacy": "^5.3.2",
5354
"@vitejs/plugin-react": "^4.1.1",
5455
"cldr": "^7.4.0",
5556
"eslint": "^8.53.0",
@@ -60,6 +61,7 @@
6061
"if-node-version": "^1.1.1",
6162
"jsdom": "^22.1.0",
6263
"source-map-explorer": "^2.5.3",
64+
"terser": "^5.30.4",
6365
"typescript": "^5.2.2",
6466
"vite": "^4.5.2",
6567
"vite-plugin-checker": "^0.6.2",

identifier/vite.config.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
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, { cspHashes } 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+
const scriptHashes = cspHashes
19+
? cspHashes.map((id) => {
20+
return `'sha256-${id}'`;
21+
})
22+
: [];
23+
24+
// Create the CSP header
25+
const cspHeader = `default-src 'self'; script-src 'self' ${scriptHashes.join(
26+
" "
27+
)}`;
28+
29+
// Insert the CSP meta tag in the HTML <head> section
30+
htmlContent = htmlContent.replace(
31+
/<meta http-equiv="Content-Security-Policy">/,
32+
`<meta http-equiv="Content-Security-Policy" content="${cspHeader}">`
33+
);
34+
35+
// Write the updated HTML back to the file
36+
writeFileSync(htmlFilePath, htmlContent);
37+
38+
console.log("CSP header added to HTML:", cspHeader);
39+
},
40+
};
41+
}
442

543
export default defineConfig((env) => {
644
return {
@@ -23,12 +61,17 @@ export default defineConfig((env) => {
2361
},
2462
plugins: [
2563
react(),
26-
env.mode !== 'test' && checker({
27-
typescript: true,
28-
eslint: {
29-
lintCommand: 'eslint --max-warnings=0 src',
30-
},
64+
legacy({
65+
targets: ['edge 18'],
3166
}),
67+
cspPlugin(),
68+
env.mode !== 'test' &&
69+
checker({
70+
typescript: true,
71+
eslint: {
72+
lintCommand: 'eslint --max-warnings=0 src',
73+
},
74+
}),
3275
splitVendorChunkPlugin(),
3376
],
3477
test: {

0 commit comments

Comments
 (0)