Skip to content

Commit 3f220c4

Browse files
committed
build: Use Vite HTML env variable replacement
1 parent d304300 commit 3f220c4

6 files changed

Lines changed: 20 additions & 49 deletions

File tree

packages/code-studio/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<base href="#BASE_URL#" />
5-
<meta name="ui-version" content="v#npm_package_version#" />
4+
<base href="%BASE_URL%" />
5+
<meta name="ui-version" content="v%npm_package_version%" />
66
<meta charset="utf-8" />
77
<meta
88
name="viewport"
@@ -14,7 +14,7 @@
1414
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
1515
-->
1616
<link rel="manifest" href="/manifest.json" />
17-
<link rel="icon" href="#VITE_FAVICON#" />
17+
<link rel="icon" href="%VITE_FAVICON%" />
1818
<title>Deephaven</title>
1919
</head>
2020
<body>

packages/code-studio/vite.config.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ import path from 'path';
88
export default defineConfig(({ mode }) => {
99
const env = loadEnv(mode, process.cwd(), '');
1010

11-
// https://github.com/vitejs/vite/issues/3105#issuecomment-939703781
12-
const htmlPlugin = () => ({
13-
name: 'html-transform',
14-
transformIndexHtml: {
15-
enforce: 'pre' as const,
16-
transform(html: string) {
17-
return html.replace(/#(.*?)#/g, (_, p1) => env[p1]);
18-
},
19-
},
20-
});
21-
2211
const packagesDir = path.resolve(__dirname, '..');
2312

2413
let port = Number.parseInt(env.PORT, 10);
@@ -148,6 +137,6 @@ export default defineConfig(({ mode }) => {
148137
css: {
149138
devSourcemap: true,
150139
},
151-
plugins: [htmlPlugin(), react()],
140+
plugins: [react()],
152141
};
153142
});

packages/embed-chart/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<base href="#BASE_URL#" />
5-
<meta name="ui-version" content="v#npm_package_version#" />
4+
<base href="%BASE_URL%" />
5+
<meta name="ui-version" content="v%npm_package_version%" />
66
<meta charset="utf-8" />
77
<meta
88
name="viewport"
@@ -15,7 +15,7 @@
1515
-->
1616
<link rel="manifest" href="/manifest.json" />
1717

18-
<link rel="icon" href="#VITE_FAVICON#" />
18+
<link rel="icon" href="%VITE_FAVICON%" />
1919
<title>Deephaven Embedded Chart</title>
2020
</head>
2121
<body>

packages/embed-chart/vite.config.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ import path from 'path';
77
export default defineConfig(({ mode }) => {
88
const env = loadEnv(mode, process.cwd(), '');
99

10-
// https://github.com/vitejs/vite/issues/3105#issuecomment-939703781
11-
const htmlPlugin = () => ({
12-
name: 'html-transform',
13-
transformIndexHtml(html: string) {
14-
return html.replace(/#(.*?)#/g, (_, p1) => env[p1]);
15-
},
16-
});
17-
1810
const packagesDir = path.resolve(__dirname, '..');
1911

2012
let port = Number.parseInt(env.PORT, 10);
2113
if (Number.isNaN(port) || port <= 0) {
2214
port = 4020;
2315
}
2416

17+
const baseURL = new URL(env.BASE_URL, `http://localhost:${port}/`);
2518
// These are paths which should be proxied to the core server
2619
// https://vitejs.dev/config/server-options.html#server-proxy
2720
const proxy = {
@@ -42,11 +35,9 @@ export default defineConfig(({ mode }) => {
4235
// Vite does not have a "any unknown fallback to proxy" like CRA
4336
// It is possible to add one with a custom middleware though if this list grows
4437
if (env.VITE_PROXY_URL) {
45-
[
46-
path.resolve(env.BASE_URL, env.VITE_CORE_API_URL),
47-
path.resolve(env.BASE_URL, env.VITE_MODULE_PLUGINS_URL),
48-
].forEach(p => {
49-
proxy[p] = {
38+
[env.VITE_CORE_API_URL, env.VITE_MODULE_PLUGINS_URL].forEach(p => {
39+
const route = new URL(p, baseURL).pathname;
40+
proxy[route] = {
5041
target: env.VITE_PROXY_URL,
5142
changeOrigin: true,
5243
};
@@ -116,6 +107,6 @@ export default defineConfig(({ mode }) => {
116107
},
117108
},
118109
},
119-
plugins: [htmlPlugin(), react()],
110+
plugins: [react()],
120111
};
121112
});

packages/embed-grid/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<base href="#BASE_URL#" />
5-
<meta name="ui-version" content="v#npm_package_version#" />
4+
<base href="%BASE_URL%" />
5+
<meta name="ui-version" content="v%npm_package_version%" />
66
<meta charset="utf-8" />
77
<meta
88
name="viewport"
@@ -15,7 +15,7 @@
1515
-->
1616
<link rel="manifest" href="/manifest.json" />
1717

18-
<link rel="icon" href="#VITE_FAVICON#" />
18+
<link rel="icon" href="%VITE_FAVICON%" />
1919
<title>Deephaven Embedded Grid</title>
2020
</head>
2121
<body>

packages/embed-grid/vite.config.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ import path from 'path';
77
export default defineConfig(({ mode }) => {
88
const env = loadEnv(mode, process.cwd(), '');
99

10-
// https://github.com/vitejs/vite/issues/3105#issuecomment-939703781
11-
const htmlPlugin = () => ({
12-
name: 'html-transform',
13-
transformIndexHtml(html: string) {
14-
return html.replace(/#(.*?)#/g, (_, p1) => env[p1]);
15-
},
16-
});
17-
1810
const packagesDir = path.resolve(__dirname, '..');
1911

2012
let port = Number.parseInt(env.PORT, 10);
2113
if (Number.isNaN(port) || port <= 0) {
2214
port = 4010;
2315
}
2416

17+
const baseURL = new URL(env.BASE_URL, `http://localhost:${port}/`);
2518
// These are paths which should be proxied to the core server
2619
// https://vitejs.dev/config/server-options.html#server-proxy
2720
const proxy = {
@@ -42,11 +35,9 @@ export default defineConfig(({ mode }) => {
4235
// Vite does not have a "any unknown fallback to proxy" like CRA
4336
// It is possible to add one with a custom middleware though if this list grows
4437
if (env.VITE_PROXY_URL) {
45-
[
46-
path.resolve(env.BASE_URL, env.VITE_CORE_API_URL),
47-
path.resolve(env.BASE_URL, env.VITE_MODULE_PLUGINS_URL),
48-
].forEach(p => {
49-
proxy[p] = {
38+
[env.VITE_CORE_API_URL, env.VITE_MODULE_PLUGINS_URL].forEach(p => {
39+
const route = new URL(p, baseURL).pathname;
40+
proxy[route] = {
5041
target: env.VITE_PROXY_URL,
5142
changeOrigin: true,
5243
};
@@ -119,6 +110,6 @@ export default defineConfig(({ mode }) => {
119110
},
120111
},
121112
},
122-
plugins: [htmlPlugin(), react()],
113+
plugins: [react()],
123114
};
124115
});

0 commit comments

Comments
 (0)