Skip to content

Commit 51f4e28

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

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
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: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,51 @@ const chunks = [
1515
'two-instances',
1616
];
1717

18-
const pages = Object.fromEntries(
19-
chunks.map((chunk) => [
20-
chunk,
21-
{ template: 'public/index.html', entry: `example/${chunk}.js` },
22-
])
23-
);
18+
const htmlPlugin = (isDev) => {
19+
/**
20+
* `vite-plugin-virtual-html-template` intercepts & handles requests for html
21+
* from the client. Vite normally handles these requests and injects a script
22+
* tag during dev (with a client runtime for HMR).
23+
*
24+
* The plugin uses `lodash.template` to runder the HTML, so a `<%= vite %>`
25+
* tag is replaced with the missing vite client during dev. In prod, nothing is
26+
* added.
27+
*/
28+
const vite = isDev
29+
? '<script type="module" src="/@vite/client"></script>'
30+
: '';
31+
const pages = Object.fromEntries(
32+
chunks.map((c) => [c, { entry: `example/${c}.js` }])
33+
);
34+
return {
35+
...virtualHtmlTemplate({ pages, data: { vite } }),
36+
handleHotUpdate({ server }) {
37+
// force auto-reload for changes
38+
server.ws.send({ type: 'full-reload' });
39+
},
40+
};
41+
};
2442

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' });
43+
export default ({ command }) =>
44+
defineConfig({
45+
base: './',
46+
plugins: [htmlPlugin(command === 'serve')],
47+
build: {
48+
outDir: 'docs',
49+
rollupOptions: {
50+
input: Object.fromEntries(
51+
chunks.map((chunk) => [chunk, `${chunk}.html`])
52+
),
4053
},
4154
},
42-
],
43-
build: {
44-
outDir: 'docs',
45-
rollupOptions: {
46-
input: Object.fromEntries(
47-
chunks.map((chunk) => [chunk, `${chunk}.html`])
48-
),
49-
},
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',
55+
resolve: {
56+
alias: {
57+
/**
58+
* vite pre-bundling (esbuild) can't be configured to
59+
* resolve .fs/.vs in regl-line. This alias forces
60+
* resolution with rollup, which avoids this error.
61+
*/
62+
'regl-line': '/node_modules/regl-line/src/index.js',
63+
},
5964
},
60-
},
61-
});
65+
});

0 commit comments

Comments
 (0)