Skip to content

Commit 2265432

Browse files
committed
extend virtual html template plugin
1 parent 77a43a2 commit 2265432

File tree

2 files changed

+64
-55
lines changed

2 files changed

+64
-55
lines changed

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<html lang="en">
44
<head>
5+
<%= vite %>
56
<meta charset="utf-8" />
67

78
<title>Regl Scatterplot</title>

vite.config.js

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,69 @@
11
import { defineConfig } from 'vite';
22
import virtualHtmlTemplate from 'vite-plugin-virtual-html-template';
33

4-
const chunks = [
5-
'index',
6-
'axes',
7-
'connected-points-by-segments',
8-
'connected-points',
9-
'dynamic-opacity',
10-
'embedded',
11-
'performance-mode',
12-
'size-encoding',
13-
'texture-background',
14-
'transition',
15-
'two-instances',
16-
];
17-
18-
const pages = Object.fromEntries(
19-
chunks.map((chunk) => [
20-
chunk,
21-
{ template: 'public/index.html', entry: `example/${chunk}.js` },
22-
])
23-
);
24-
25-
export default defineConfig({
26-
base: './',
27-
plugins: [
28-
virtualHtmlTemplate({ pages }),
29-
{
30-
name: 'inject-vite-client',
31-
apply: 'serve', // only for dev server
32-
transform(code, id) {
33-
if (id.includes('example')) {
34-
return `import "/@vite/client";\n${code}`;
35-
}
36-
return null;
37-
},
38-
handleHotUpdate({ server }) {
39-
server.ws.send({ type: 'full-reload' });
40-
},
4+
const htmlPlugin = ({ chunks, isDev }) => {
5+
/**
6+
* `vite-plugin-virtual-html-template` intercepts & handles requests for html
7+
* from the client. Vite normally handles these requests and injects a script
8+
* tag during dev (with a client runtime for HMR).
9+
*
10+
* The plugin uses `lodash.template` to runder the HTML, so a `<%= vite %>`
11+
* tag is replaced with the missing vite client during dev. In prod, nothing is
12+
* added.
13+
*/
14+
const vite = isDev
15+
? '<script type="module" src="/@vite/client"></script>'
16+
: '';
17+
const pages = Object.fromEntries(
18+
chunks.map((c) => [c, { entry: `example/${c}.js` }])
19+
);
20+
return {
21+
...virtualHtmlTemplate({ pages, data: { vite } }),
22+
handleHotUpdate({ server }) {
23+
// force auto-reload for changes
24+
server.ws.send({ type: 'full-reload' });
4125
},
42-
],
43-
build: {
44-
outDir: 'docs',
45-
rollupOptions: {
46-
input: Object.fromEntries(
47-
chunks.map((chunk) => [chunk, `${chunk}.html`])
48-
),
26+
config(config) {
27+
if (!isDev) {
28+
config.build.rollupOptions = {
29+
...config.build.rollupOptions,
30+
input: Object.fromEntries(chunks.map((c) => [c, `${c}.html`])),
31+
};
32+
}
4933
},
50-
},
51-
resolve: {
52-
alias: {
53-
/**
54-
* vite pre-bundling (esbuild) can't be configured to
55-
* resolve .fs/.vs in regl-line. This alias forces
56-
* resolution with rollup, which avoids this error.
57-
*/
58-
'regl-line': '/node_modules/regl-line/src/index.js',
34+
};
35+
};
36+
37+
export default ({ command }) =>
38+
defineConfig({
39+
base: './',
40+
plugins: [
41+
htmlPlugin({
42+
chunks: [
43+
'index',
44+
'axes',
45+
'connected-points-by-segments',
46+
'connected-points',
47+
'dynamic-opacity',
48+
'embedded',
49+
'performance-mode',
50+
'size-encoding',
51+
'texture-background',
52+
'transition',
53+
'two-instances',
54+
],
55+
isDev: command === 'serve',
56+
}),
57+
],
58+
build: { outDir: 'docs' },
59+
resolve: {
60+
alias: {
61+
/**
62+
* vite pre-bundling (esbuild) can't be configured to
63+
* resolve .fs/.vs in regl-line. This alias forces
64+
* resolution with rollup, which avoids this error.
65+
*/
66+
'regl-line': '/node_modules/regl-line/src/index.js',
67+
},
5968
},
60-
},
61-
});
69+
});

0 commit comments

Comments
 (0)