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'
|
2022-01-27 08:49:40 +08:00
|
|
|
import pkg from '../../package.json'
|
2021-11-11 17:52:50 +08:00
|
|
|
|
|
|
|
// https://vitejs.dev/config/
|
|
|
|
export default defineConfig({
|
2021-11-27 10:51:32 +08:00
|
|
|
mode: process.env.NODE_ENV,
|
2022-01-27 08:49:40 +08:00
|
|
|
root: __dirname,
|
2022-01-16 09:09:21 +08:00
|
|
|
plugins: [
|
|
|
|
vue(),
|
2022-01-21 10:14:56 +08:00
|
|
|
resolveElectron(
|
|
|
|
/**
|
2022-02-08 11:14:05 +08:00
|
|
|
* 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
|
2022-01-21 10:14:56 +08:00
|
|
|
* @example
|
|
|
|
* {
|
2022-02-08 11:14:05 +08:00
|
|
|
* 'electron-store': 'const Store = require("electron-store"); export default Store;',
|
2022-01-21 10:14:56 +08:00
|
|
|
* }
|
|
|
|
*/
|
2022-03-13 21:52:58 +08:00
|
|
|
{
|
|
|
|
sqlite3: 'const sqlite3 = require("sqlite3"); export default sqlite3;',
|
|
|
|
},
|
2022-01-21 10:14:56 +08:00
|
|
|
),
|
2022-01-16 09:09:21 +08:00
|
|
|
],
|
2021-11-11 17:52:50 +08:00
|
|
|
base: './',
|
|
|
|
build: {
|
2022-02-14 17:48:27 +08:00
|
|
|
sourcemap: true,
|
2021-11-11 17:52:50 +08:00
|
|
|
outDir: '../../dist/renderer',
|
|
|
|
},
|
|
|
|
server: {
|
2022-03-15 08:34:23 +08:00
|
|
|
host: pkg.env.VITE_DEV_SERVER_HOST,
|
|
|
|
port: pkg.env.VITE_DEV_SERVER_PORT,
|
2021-11-11 17:52:50 +08:00
|
|
|
},
|
|
|
|
})
|
2022-01-17 09:34:42 +08:00
|
|
|
|
2022-02-08 11:14:05 +08:00
|
|
|
/**
|
|
|
|
* For usage of Electron and NodeJS APIs in the Renderer process
|
|
|
|
* @see https://github.com/caoxiemeihao/electron-vue-vite/issues/52
|
|
|
|
*/
|
|
|
|
export function resolveElectron(
|
|
|
|
resolves: Parameters<typeof resolve>[0] = {}
|
|
|
|
): Plugin {
|
|
|
|
const builtins = builtinModules.filter((t) => !t.startsWith('_'))
|
|
|
|
/**
|
|
|
|
* @see https://github.com/caoxiemeihao/vite-plugins/tree/main/packages/resolve#readme
|
|
|
|
*/
|
2022-01-30 08:28:34 +08:00
|
|
|
return resolve({
|
|
|
|
electron: electronExport(),
|
|
|
|
...builtinModulesExport(builtins),
|
|
|
|
...resolves,
|
|
|
|
})
|
2022-01-17 09:34:42 +08:00
|
|
|
|
|
|
|
function electronExport() {
|
|
|
|
return `
|
2022-02-08 11:24:32 +08:00
|
|
|
/**
|
|
|
|
* For all exported modules see https://www.electronjs.org/docs/latest/api/clipboard -> Renderer Process Modules
|
|
|
|
*/
|
|
|
|
const electron = require("electron");
|
|
|
|
const {
|
|
|
|
clipboard,
|
|
|
|
nativeImage,
|
|
|
|
shell,
|
|
|
|
contextBridge,
|
|
|
|
crashReporter,
|
|
|
|
ipcRenderer,
|
|
|
|
webFrame,
|
|
|
|
desktopCapturer,
|
|
|
|
deprecate,
|
|
|
|
} = electron;
|
2022-02-08 11:14:05 +08:00
|
|
|
|
2022-02-08 11:24:32 +08:00
|
|
|
export {
|
|
|
|
electron as default,
|
|
|
|
clipboard,
|
|
|
|
nativeImage,
|
|
|
|
shell,
|
|
|
|
contextBridge,
|
|
|
|
crashReporter,
|
|
|
|
ipcRenderer,
|
|
|
|
webFrame,
|
|
|
|
desktopCapturer,
|
|
|
|
deprecate,
|
|
|
|
}
|
2022-02-08 11:14:05 +08:00
|
|
|
`
|
2022-01-17 09:34:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function builtinModulesExport(modules: string[]) {
|
2022-02-08 11:14:05 +08:00
|
|
|
return modules
|
|
|
|
.map((moduleId) => {
|
|
|
|
const nodeModule = require(moduleId)
|
|
|
|
const requireModule = `const M = require("${moduleId}");`
|
|
|
|
const exportDefault = `export default M;`
|
|
|
|
const exportMembers =
|
|
|
|
Object.keys(nodeModule)
|
|
|
|
.map((attr) => `export const ${attr} = M.${attr}`)
|
|
|
|
.join(';\n') + ';'
|
|
|
|
const nodeModuleCode = `
|
2022-01-24 10:01:33 +08:00
|
|
|
${requireModule}
|
2022-01-17 09:34:42 +08:00
|
|
|
|
|
|
|
${exportDefault}
|
|
|
|
|
2022-01-24 09:57:38 +08:00
|
|
|
${exportMembers}
|
2022-02-08 11:14:05 +08:00
|
|
|
`
|
2022-01-17 09:34:42 +08:00
|
|
|
|
2022-02-08 11:14:05 +08:00
|
|
|
return { [moduleId]: nodeModuleCode }
|
|
|
|
})
|
|
|
|
.reduce((memo, item) => Object.assign(memo, item), {})
|
2022-01-17 09:34:42 +08:00
|
|
|
}
|
|
|
|
}
|