@@ -15,47 +15,51 @@ const chunks = [
15
15
'two-instances' ,
16
16
] ;
17
17
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
+ } ;
24
42
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
+ ) ,
40
53
} ,
41
54
} ,
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
+ } ,
59
64
} ,
60
- } ,
61
- } ) ;
65
+ } ) ;
0 commit comments