-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
vite.config.js
121 lines (108 loc) · 3.02 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {resolveModuleExportNames} from 'mlly';
import {getChromeMajorVersion} from '@vite-electron-builder/electron-versions';
export default /**
* @type {import('vite').UserConfig}
* @see https://vitejs.dev/config/
*/
({
build: {
ssr: true,
sourcemap: 'inline',
outDir: 'dist',
target: `chrome${getChromeMajorVersion()}`,
assetsDir: '.',
lib: {
entry: ['src/exposed.ts', 'virtual:browser.js'],
},
rollupOptions: {
output: [
{
// ESM preload scripts must have the .mjs extension
// https://www.electronjs.org/docs/latest/tutorial/esm#esm-preload-scripts-must-have-the-mjs-extension
entryFileNames: '[name].mjs',
},
],
},
emptyOutDir: true,
reportCompressedSize: false,
},
plugins: [mockExposed(), handleHotReload()],
});
/**
* This plugin creates a browser (renderer) version of `preload` package.
* Basically, it just read all nominals you exported from package and define it as globalThis properties
* expecting that real values were exposed by `electron.contextBridge.exposeInMainWorld()`
*
* Example:
* ```ts
* // index.ts
* export const someVar = 'my-value';
* ```
*
* Output
* ```js
* // _virtual_browser.mjs
* export const someVar = globalThis[<hash>] // 'my-value'
* ```
*/
function mockExposed() {
const virtualModuleId = 'virtual:browser.js';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
return {
name: 'electron-main-exposer',
resolveId(id) {
if (id.endsWith(virtualModuleId)) {
return resolvedVirtualModuleId;
}
},
async load(id) {
if (id === resolvedVirtualModuleId) {
const exportedNames = await resolveModuleExportNames('./src/index.ts', {
url: import.meta.url,
});
return exportedNames.reduce((s, key) => {
return (
s +
(key === 'default'
? `export default globalThis['${btoa(key)}'];\n`
: `export const ${key} = globalThis['${btoa(key)}'];\n`)
);
}, '');
}
},
};
}
/**
* Implement Electron webview reload when some file was changed
* @return {import('vite').Plugin}
*/
function handleHotReload() {
/** @type {import('vite').ViteDevServer|null} */
let rendererWatchServer = null;
return {
name: '@vite-electron-builder/preload-process-hot-reload',
config(config, env) {
if (env.mode !== 'development') {
return;
}
const rendererWatchServerProvider = config.plugins.find(p => p.name === '@vite-electron-builder/renderer-watch-server-provider');
if (!rendererWatchServerProvider) {
throw new Error('Renderer watch server provider not found');
}
rendererWatchServer = rendererWatchServerProvider.api.provideRendererWatchServer();
return {
build: {
watch: {},
},
};
},
writeBundle() {
if (!rendererWatchServer) {
return;
}
rendererWatchServer.ws.send({
type: 'full-reload',
});
},
};
}