2021-11-11 17:52:50 +08:00
|
|
|
import { join } from 'path'
|
2022-01-17 09:34:42 +08:00
|
|
|
import { builtinModules } from 'module'
|
|
|
|
import { defineConfig, Plugin } from 'vite'
|
2021-11-11 17:52:50 +08:00
|
|
|
import vue from '@vitejs/plugin-vue'
|
2022-01-16 09:09:21 +08:00
|
|
|
import resolve from 'vite-plugin-resolve'
|
2021-11-11 17:52:50 +08:00
|
|
|
import pkg from '../package.json'
|
|
|
|
|
|
|
|
// https://vitejs.dev/config/
|
|
|
|
export default defineConfig({
|
2021-11-27 10:51:32 +08:00
|
|
|
mode: process.env.NODE_ENV,
|
2021-11-11 17:52:50 +08:00
|
|
|
root: join(__dirname, '../src/renderer'),
|
2022-01-16 09:09:21 +08:00
|
|
|
plugins: [
|
|
|
|
vue(),
|
2022-01-17 09:34:42 +08:00
|
|
|
resolveElectron(),
|
2022-01-16 09:09:21 +08:00
|
|
|
],
|
2021-11-11 17:52:50 +08:00
|
|
|
base: './',
|
|
|
|
build: {
|
|
|
|
emptyOutDir: true,
|
|
|
|
outDir: '../../dist/renderer',
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
host: pkg.env.HOST,
|
|
|
|
port: pkg.env.PORT,
|
|
|
|
},
|
|
|
|
})
|
2022-01-17 09:34:42 +08:00
|
|
|
|
|
|
|
// ----------------- For use Electron, NodeJs in Renderer-process -----------------
|
|
|
|
export function resolveElectron(): Plugin[] {
|
|
|
|
const builtins = builtinModules.filter(t => !t.startsWith('_'))
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
name: 'vite-plugin-electron-config',
|
|
|
|
config(config) {
|
|
|
|
if (!config.optimizeDeps) config.optimizeDeps = {}
|
|
|
|
if (!config.optimizeDeps.exclude) config.optimizeDeps.exclude = []
|
|
|
|
|
|
|
|
config.optimizeDeps.exclude.push('electron', ...builtins)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// https://github.com/caoxiemeihao/vite-plugins/tree/main/packages/resolve#readme
|
|
|
|
resolve({
|
|
|
|
electron: electronExport(),
|
|
|
|
...builtinModulesExport(builtins),
|
|
|
|
// you can custom other module in here, need to make sure it's in `dependencies`
|
|
|
|
// 'electron-store': 'const Store = require("electron-store"); export defalut Store;'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
|
|
|
|
function electronExport() {
|
|
|
|
return `
|
|
|
|
/**
|
|
|
|
* All exports module see https://www.electronjs.org -> API -> Renderer Process Modules
|
|
|
|
*/
|
|
|
|
const electron = require("electron");
|
|
|
|
const {
|
|
|
|
clipboard,
|
|
|
|
nativeImage,
|
|
|
|
shell,
|
|
|
|
contextBridge,
|
|
|
|
crashReporter,
|
|
|
|
ipcRenderer,
|
|
|
|
webFrame,
|
|
|
|
desktopCapturer,
|
|
|
|
deprecate,
|
|
|
|
} = electron;
|
|
|
|
|
|
|
|
export {
|
|
|
|
electron as default,
|
|
|
|
clipboard,
|
|
|
|
nativeImage,
|
|
|
|
shell,
|
|
|
|
contextBridge,
|
|
|
|
crashReporter,
|
|
|
|
ipcRenderer,
|
|
|
|
webFrame,
|
|
|
|
desktopCapturer,
|
|
|
|
deprecate,
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
function builtinModulesExport(modules: string[]) {
|
|
|
|
return modules.map((moduleId) => {
|
|
|
|
const nodeModule = require(moduleId)
|
|
|
|
const attrs = Object.keys(nodeModule)
|
|
|
|
const requireTpl = `const __builtinModule = require("${moduleId}");`
|
|
|
|
const exportDefault = `export default __builtinModule`
|
|
|
|
const exportTpl = attrs.map(attr => `export const ${attr} = __builtinModule.${attr}`).join(';\n') + ';'
|
|
|
|
const nodeModuleCode = `
|
|
|
|
${requireTpl}
|
|
|
|
|
|
|
|
${exportDefault}
|
|
|
|
|
|
|
|
${exportTpl}
|
|
|
|
`
|
|
|
|
|
|
|
|
return { [moduleId]: nodeModuleCode }
|
|
|
|
}).reduce((memo, item) => Object.assign(memo, item), {})
|
|
|
|
}
|
|
|
|
}
|