Skip to content

Commit 5472373

Browse files
committed
Add preview script
1 parent 2265432 commit 5472373

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"prerelease": "rm -rf dist/*; npm run build; zip -r dist.zip dist",
2929
"pretest": "npm run lint",
3030
"start": "vite",
31+
"preview": "vite preview",
3132
"test": "rollup -c ./rollup.test.config.js | tape-run --render='tap-spec'",
3233
"watch": "rollup -cw"
3334
},

vite.config.js

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

4-
const htmlPlugin = ({ chunks, isDev }) => {
4+
/** @returns {import('vite').Plugin} */
5+
const htmlPlugin = ({ chunks }) => {
56
/**
67
* `vite-plugin-virtual-html-template` intercepts & handles requests for html
78
* from the client. Vite normally handles these requests and injects a script
@@ -11,59 +12,64 @@ const htmlPlugin = ({ chunks, isDev }) => {
1112
* tag is replaced with the missing vite client during dev. In prod, nothing is
1213
* added.
1314
*/
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-
);
15+
let plugin;
16+
const init = (isDev) => {
17+
const vite = isDev
18+
? '<script type="module" src="/@vite/client"></script>'
19+
: '';
20+
const pages = Object.fromEntries(
21+
chunks.map((c) => [c, { entry: `example/${c}.js` }])
22+
);
23+
return virtualHtmlTemplate({ pages, data: { vite } });
24+
};
2025
return {
21-
...virtualHtmlTemplate({ pages, data: { vite } }),
22-
handleHotUpdate({ server }) {
23-
// force auto-reload for changes
24-
server.ws.send({ type: 'full-reload' });
25-
},
26-
config(config) {
27-
if (!isDev) {
26+
config(config, { command }) {
27+
if (command === 'build') {
2828
config.build.rollupOptions = {
2929
...config.build.rollupOptions,
3030
input: Object.fromEntries(chunks.map((c) => [c, `${c}.html`])),
3131
};
3232
}
33+
plugin = init(command === 'serve');
34+
},
35+
handleHotUpdate({ server }) {
36+
// force auto refresh
37+
server.ws.send({ type: 'full-reload' });
3338
},
39+
configureServer: (server) => plugin.configureServer(server),
40+
resolveId: (id) => plugin.resolveId(id),
41+
load: (id) => plugin.load(id),
3442
};
3543
};
3644

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-
},
45+
export default defineConfig({
46+
base: './',
47+
plugins: [
48+
htmlPlugin({
49+
chunks: [
50+
'index',
51+
'axes',
52+
'connected-points-by-segments',
53+
'connected-points',
54+
'dynamic-opacity',
55+
'embedded',
56+
'performance-mode',
57+
'size-encoding',
58+
'texture-background',
59+
'transition',
60+
'two-instances',
61+
],
62+
}),
63+
],
64+
build: { outDir: 'docs' },
65+
resolve: {
66+
alias: {
67+
/**
68+
* vite pre-bundling (esbuild) can't be configured to
69+
* resolve .fs/.vs in regl-line. This alias forces
70+
* resolution with rollup, which avoids this error.
71+
*/
72+
'regl-line': '/node_modules/regl-line/src/index.js',
6873
},
69-
});
74+
},
75+
});

0 commit comments

Comments
 (0)