@@ -7,15 +7,40 @@ const optimizer = require('vite-plugin-optimizer');
77 */
88module . exports = function ( ) {
99 const name = 'vite-plugin-electron-renderer' ;
10- const plugin = optimizer (
11- builtinModulesExport ( builtinModules . filter ( e => ! e . startsWith ( '_' ) ) ) ,
12- { dir : `. ${ name } ` } ,
13- ) ;
14- plugin . name = name ;
10+ const builtins = builtinModules . filter ( e => ! e . startsWith ( '_' ) ) ;
11+ const plugin = optimizer ( builtinModulesExport ( builtins ) , { dir : `. ${ name } ` } , ) ;
12+
13+ plugin . name = ` ${ name } :optimizer` ;
14+ plugin . apply = 'serve' ;
1515
1616 return [
17+ plugin ,
18+ {
19+ name : `${ name } :config-serve` ,
20+ apply : 'serve' ,
21+ config ( config ) {
22+ // Vite ---- resolve.alias ----
23+ if ( ! config . resolve ) config . resolve = { } ;
24+ if ( ! config . resolve . conditions ) config . resolve . conditions = [ 'node' ] ;
25+ if ( ! config . resolve . alias ) config . resolve . alias = [ ] ;
26+
27+ const electronjs = path . join ( __dirname , 'modules/electron-renderer.js' ) ;
28+ if ( Array . isArray ( config . resolve . alias ) ) {
29+ config . resolve . alias . push ( { find : 'electron' , replacement : electronjs } ) ;
30+ } else {
31+ config . resolve . alias [ 'electron' ] = electronjs ;
32+ }
33+
34+ // Vite ---- optimizeDeps.exclude ----
35+ if ( ! config . optimizeDeps ) config . optimizeDeps = { } ;
36+ if ( ! config . optimizeDeps . exclude ) config . optimizeDeps . exclude = [ ] ;
37+
38+ config . optimizeDeps . exclude . push ( 'electron' ) ;
39+ } ,
40+ } ,
1741 {
18- name : `${ name } :config` ,
42+ name : `${ name } :config-build` ,
43+ apply : 'build' ,
1944 config ( config ) {
2045 // make sure that Electron can be loaded into the local file using `loadFile` after packaging
2146 if ( ! config . base ) config . base = './' ;
@@ -25,57 +50,47 @@ module.exports = function () {
2550 // ensure that static resources are loaded normally
2651 if ( ! config . build . assetsDir ) config . build . assetsDir = '' ;
2752
28- // ----------------------------------------
29- // Rollup: output.format, output.external
30-
53+ // Rollup ---- init ----
3154 if ( ! config . build . rollupOptions ) config . build . rollupOptions = { } ;
3255 if ( ! config . build . rollupOptions . output ) config . build . rollupOptions . output = { } ;
3356
34- const prodExternals = [ ...builtinModules . filter ( e => ! e . startsWith ( '_' ) ) , 'electron' ] ;
35-
36- const modifyOutput = output => {
37- if ( ! output . format ) {
38- // the packaged Electron app should use "cjs"
39- output . format = 'cjs' ;
40- }
41-
42- // make builtin modules & electron external when rollup
43- output . external = [ ...( output . external || [ ] ) , ...prodExternals ] ;
44- } ;
45- if ( Array . isArray ( config . build . rollupOptions . output ) ) {
46- config . build . rollupOptions . output . forEach ( output => {
47- modifyOutput ( output ) ;
48- } ) ;
57+ // Rollup ---- external ----
58+ let external = config . build . rollupOptions . external ;
59+ const electronBuiltins = builtins . map ( e => [ e , `node:${ e } ` ] ) . flat ( ) . concat ( 'electron' ) ;
60+ if (
61+ Array . isArray ( external ) ||
62+ typeof external === 'string' ||
63+ external instanceof RegExp
64+ ) {
65+ external = electronBuiltins . concat ( external ) ;
66+ } else if ( typeof external === 'function' ) {
67+ const original = external ;
68+ external = function ( source , importer , isResolved ) {
69+ if ( electronBuiltins . includes ( source ) ) {
70+ return true ;
71+ }
72+ return original ( source , importer , isResolved ) ;
73+ } ;
4974 } else {
50- modifyOutput ( config . build . rollupOptions . output ) ;
75+ external = electronBuiltins ;
5176 }
5277
53- // ----------------------------------------
54- // Vite: resolve.alias
55-
56- if ( ! config . resolve ) config . resolve = { } ;
57- if ( ! config . resolve . conditions ) config . resolve . conditions = [ 'node' ] ;
58- if ( ! config . resolve . alias ) config . resolve . alias = [ ] ;
59- const electronjs = path . join ( __dirname , 'modules/electron-renderer.js' ) ;
78+ // make builtin modules & electron external when rollup
79+ config . build . rollupOptions . external = external ;
6080
61- if ( Array . isArray ( config . resolve . alias ) ) {
62- config . resolve . alias . push ( {
63- find : 'electron' ,
64- replacement : electronjs ,
65- } ) ;
81+ // Rollup ---- output.format ----
82+ const output = config . build . rollupOptions . output ;
83+ if ( Array . isArray ( output ) ) {
84+ for ( const o of output ) {
85+ if ( o . format === undefined ) o . format = 'cjs' ;
86+ }
6687 } else {
67- config . resolve . alias [ 'electron' ] = electronjs ;
88+ // external modules such as `electron`, `fs`
89+ // they can only be loaded normally under CommonJs
90+ if ( output . format === undefined ) output . format = 'cjs' ;
6891 }
69-
70- // ----------------------------------------
71- // Vite: optimizeDeps.exclude
72-
73- if ( ! config . optimizeDeps ) config . optimizeDeps = { } ;
74- if ( ! config . optimizeDeps . exclude ) config . optimizeDeps . exclude = [ ] ;
75- config . optimizeDeps . exclude . push ( 'electron' ) ;
7692 } ,
7793 } ,
78- plugin ,
7994 ] ;
8095} ;
8196
0 commit comments