forked from DanielBaulig/esphome-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
117 lines (107 loc) · 3.17 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import manifest from 'rollup-plugin-output-manifest';
import topLevelAwait from 'vite-plugin-top-level-await';
import fs from 'fs';
const commitHash = require('child_process')
.execSync('git rev-parse --short HEAD')
.toString();
function esphomeWebConfigVirtualModules() {
const cssModule = 'virtual:custom.css';
const moduleIds = [cssModule];
function resolveId(id) {
return `\0${id}`;
}
const esphomeWebConfigPath = 'esphome-web.json';
let esphomeWebConfig = {};
if (fs.existsSync(esphomeWebConfigPath)) {
esphomeWebConfig = JSON.parse(fs.readFileSync(esphomeWebConfigPath))
}
function generateCssModule() {
const o = esphomeWebConfig.css || {};
return `body { \n${Object.keys(o).map(k => ` ${k}: ${o[k]};\n`).join('')}}`;
}
return {
name: 'esphome-web-config-virtual-modules-plugin',
resolveId(id) {
if (moduleIds.includes(id)) {
return resolveId(id);
}
},
transformIndexHtml(html) {
if ('title' in esphomeWebConfig) {
html = html.replace(
/<title>(.*?)<\/title>/,
`<title>${esphomeWebConfig.title}</title>`
);
}
if (esphomeWebConfig.originTrialTokens?.pnaPermissionPromptToken) {
html = html.replace(
'<!-- %pnaPermissionPromptToken% -->',
`<meta http-equiv="Origin-Trial" content="${esphomeWebConfig.originTrialTokens.pnaPermissionPromptToken}" />`,
);
}
if (esphomeWebConfig.originTrialTokens?.pnaNonSecureToken) {
html = html.replace(
'<!-- %pnaNonSecureToken% -->',
`<meta http-equiv="Origin-Trial" content="${esphomeWebConfig.originTrialTokens.pnaNonSecureToken}" />`,
);
}
html = html.replace(
/.*<!-- %(pnaNonSecureToken|pnaPermissionPromptToken)% -->.*\n/g,
'',
);
return html;
},
async handleHotUpdate({file, modules, read, server}) {
if (file.endsWith('esphome-web.json')) {
// Refresh config cache
esphomeWebConfig = JSON.parse(await read());
// Find module in module graph
const mod = server.moduleGraph.getModuleById(resolveId(cssModule));
return [...modules, mod];
}
},
load(id) {
if (id === resolveId(cssModule)) {
return generateCssModule();
}
},
};
}
export default defineConfig({
define: {
__COMMIT_HASH__: JSON.stringify(commitHash),
},
plugins: [
esphomeWebConfigVirtualModules(),
react(),
manifest({nameWithExt: false, filter: () => true}),
topLevelAwait(),
],
build: {
manifest: 'vite-manifest.json',
outDir: "./public",
rollupOptions: {
input: {
app: "./index.html",
sw: "./src/sw.js",
},
output: {
entryFileNames: assetInfo => assetInfo.name == "sw" ? "[name].js" : "assets/js/[name]-[hash].js",
manualChunks(id) {
if (id.includes('react')) {
return 'react';
}
},
},
},
},
publicDir: "./static",
server: {
headers: {
'Content-Security-Policy': 'treat-as-public-address',
'Service-Worker-Allowed': '/',
},
},
});