1
1
import { builtinModules } from 'node:module'
2
- import type { Plugin } from 'vite'
2
+ import type {
3
+ Alias ,
4
+ Plugin ,
5
+ UserConfig ,
6
+ } from 'vite'
3
7
import type { ExternalOption , RollupOptions } from 'rollup'
4
8
5
9
export const builtins = [
@@ -8,34 +12,64 @@ export const builtins = [
8
12
...builtinModules . filter ( m => ! m . startsWith ( '_' ) ) . map ( mod => `node:${ mod } ` ) ,
9
13
]
10
14
11
- export default function buildConfig ( nodeIntegration ?: boolean ) : Plugin {
12
- return {
13
- name : 'vite-plugin-electron-renderer:build-config' ,
14
- apply : 'build' ,
15
- config ( config ) {
16
- // Make sure that Electron can be loaded into the local file using `loadFile` after packaging
17
- config . base ??= './'
15
+ export default function buildConfig ( nodeIntegration ?: boolean ) : Plugin [ ] {
16
+ return [
17
+ {
18
+ name : 'vite-plugin-electron-renderer:builtins' ,
19
+ config ( config ) {
20
+ const aliases : Alias [ ] = [
21
+ // Always polyfill electron.
22
+ {
23
+ find : 'electron' ,
24
+ replacement : 'vite-plugin-electron-renderer/builtins/electron' ,
25
+ } ,
26
+ ...( nodeIntegration ? builtins
27
+ . filter ( m => m !== 'electron' )
28
+ . filter ( m => ! m . startsWith ( 'node:' ) )
29
+ . map < Alias > ( m => ( {
30
+ find : new RegExp ( `^(node:)?${ m } $` ) ,
31
+ replacement : `vite-plugin-electron-renderer/builtins/${ m } ` ,
32
+ } ) ) : [ ] ) ,
33
+ ]
18
34
19
- config . build ??= { }
35
+ modifyAlias ( config , aliases )
36
+ modifyOptimizeDeps (
37
+ config ,
38
+ [
39
+ 'electron' ,
40
+ 'vite-plugin-electron-renderer/builtins/electron' ,
41
+ ] . concat ( nodeIntegration ? aliases . map ( ( { replacement } ) => replacement ) : [ ] ) ,
42
+ )
43
+ } ,
44
+ } ,
45
+ {
46
+ name : 'vite-plugin-electron-renderer:build-config' ,
47
+ apply : 'build' ,
48
+ config ( config ) {
49
+ // Make sure that Electron can be loaded into the local file using `loadFile` after packaging
50
+ config . base ??= './'
20
51
21
- // TODO: init `config.build.target`
22
- // https://github.com/vitejs/vite/pull/8843
52
+ config . build ??= { }
23
53
24
- // https://github.com/electron-vite/electron-vite-vue/issues/107
25
- config . build . cssCodeSplit ??= false
54
+ // TODO: init `config.build.target`
55
+ // https://github.com/vitejs/vite/pull/8843
26
56
27
- // TODO: compatible with custom assetsDir
28
- // This will guarantee the proper loading of static resources, such as images, `worker.js`
29
- // The `.js` file can be loaded correctly with cjs-shim.ts
30
- config . build . assetsDir ??= ''
57
+ // https://github.com/electron-vite/electron-vite-vue/issues/107
58
+ config . build . cssCodeSplit ??= false
31
59
32
- if ( nodeIntegration ) {
33
- config . build . rollupOptions ??= { }
34
- config . build . rollupOptions . external = withExternal ( config . build . rollupOptions . external )
35
- setOutputFormat ( config . build . rollupOptions )
36
- }
60
+ // TODO: compatible with custom assetsDir
61
+ // This will guarantee the proper loading of static resources, such as images, `worker.js`
62
+ // The `.js` file can be loaded correctly with cjs-shim.ts
63
+ config . build . assetsDir ??= ''
64
+
65
+ if ( nodeIntegration ) {
66
+ config . build . rollupOptions ??= { }
67
+ config . build . rollupOptions . external = withExternal ( config . build . rollupOptions . external )
68
+ setOutputFormat ( config . build . rollupOptions )
69
+ }
70
+ } ,
37
71
} ,
38
- }
72
+ ]
39
73
}
40
74
41
75
function withExternal ( external ?: ExternalOption ) {
@@ -71,3 +105,20 @@ function setOutputFormat(rollupOptions: RollupOptions) {
71
105
rollupOptions . output . format ??= 'cjs'
72
106
}
73
107
}
108
+
109
+ export function modifyOptimizeDeps ( config : UserConfig , exclude : string [ ] ) {
110
+ config . optimizeDeps ??= { }
111
+ config . optimizeDeps . exclude ??= [ ]
112
+ config . optimizeDeps . exclude . push ( ...exclude )
113
+ }
114
+
115
+ export function modifyAlias ( config : UserConfig , aliases : Alias [ ] ) {
116
+ config . resolve ??= { }
117
+ config . resolve . alias ??= [ ]
118
+ if ( Object . prototype . toString . call ( config . resolve . alias ) === '[object Object]' ) {
119
+ config . resolve . alias = Object
120
+ . entries ( config . resolve . alias )
121
+ . reduce < Alias [ ] > ( ( memo , [ find , replacement ] ) => memo . concat ( { find, replacement } ) , [ ] )
122
+ }
123
+ ( config . resolve . alias as Alias [ ] ) . push ( ...aliases )
124
+ }
0 commit comments