2022-06-25 08:46:01 +08:00
|
|
|
import { join } from 'path'
|
2022-06-25 10:47:12 +08:00
|
|
|
import { builtinModules } from 'module'
|
2022-06-25 08:46:01 +08:00
|
|
|
import { defineConfig } from 'vite-plugin-electron'
|
2022-06-25 10:47:53 +08:00
|
|
|
import resolve, { lib2esm } from 'vite-plugin-resolve'
|
2022-06-25 10:47:12 +08:00
|
|
|
import pkg from './package.json'
|
|
|
|
|
|
|
|
const external = [
|
|
|
|
'electron',
|
|
|
|
...builtinModules,
|
|
|
|
// @ts-ignore
|
|
|
|
// For use Node.js package in Electron-main, Preload-script
|
|
|
|
...Object.keys(pkg.dependencies || {}),
|
|
|
|
]
|
2022-06-25 08:46:01 +08:00
|
|
|
|
|
|
|
export default defineConfig({
|
|
|
|
main: {
|
|
|
|
entry: 'electron/main/index.ts',
|
|
|
|
vite: {
|
|
|
|
build: {
|
|
|
|
sourcemap: false,
|
|
|
|
outDir: 'dist/electron/main',
|
2022-06-25 10:47:12 +08:00
|
|
|
rollupOptions: {
|
|
|
|
external,
|
|
|
|
},
|
2022-06-25 08:46:01 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preload: {
|
|
|
|
input: {
|
|
|
|
// You can configure multiple preload here
|
|
|
|
splash: join(__dirname, 'electron/preload/splash.ts'),
|
|
|
|
},
|
|
|
|
vite: {
|
|
|
|
build: {
|
|
|
|
// For debug
|
|
|
|
sourcemap: 'inline',
|
|
|
|
outDir: 'dist/electron/preload',
|
2022-06-25 10:47:12 +08:00
|
|
|
rollupOptions: {
|
|
|
|
external,
|
|
|
|
},
|
2022-06-25 08:46:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
2022-06-25 10:47:53 +08:00
|
|
|
|
|
|
|
export function useNodeJsInElectronRenderer() {
|
|
|
|
return resolve(
|
|
|
|
/**
|
|
|
|
* Here you can specify other modules
|
|
|
|
* 🚧 You have to make sure that your module is in `dependencies` and not in the` devDependencies`,
|
|
|
|
* which will ensure that the electron-builder can package it correctly
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
// If you use the following modules, the following configuration will work
|
|
|
|
// What they have in common is that they will return - ESM format code snippets
|
|
|
|
|
|
|
|
// ESM format string
|
|
|
|
'electron-store': 'export default require("electron-store");',
|
|
|
|
// Use lib2esm() to easy to convert ESM
|
|
|
|
// Equivalent to
|
|
|
|
/**
|
|
|
|
* sqlite3: () => `
|
|
|
|
* const _M_ = require('sqlite3');
|
|
|
|
* const _D_ = _M_.default || _M_;
|
|
|
|
* export { _D_ as default }
|
|
|
|
* `
|
|
|
|
*/
|
|
|
|
sqlite3: lib2esm('sqlite3', { format: 'cjs' }),
|
|
|
|
serialport: lib2esm(
|
|
|
|
// CJS lib name
|
|
|
|
'serialport',
|
|
|
|
// export memebers
|
|
|
|
[
|
|
|
|
'SerialPort',
|
|
|
|
'SerialPortMock',
|
|
|
|
],
|
|
|
|
{ format: 'cjs' },
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|