mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-01-19 11:56:36 +08:00
use vite-plugin-electron-renderer
This commit is contained in:
parent
55f2300a2e
commit
0947c8e3d3
@ -1,7 +1,7 @@
|
||||
import { builtinModules } from 'module'
|
||||
import { defineConfig, Plugin } from 'vite'
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import optimizer from 'vite-plugin-optimizer'
|
||||
import resolve from 'vite-plugin-resolve'
|
||||
import electron from 'vite-plugin-electron-renderer'
|
||||
import pkg from '../../package.json'
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
@ -10,19 +10,17 @@ export default defineConfig({
|
||||
root: __dirname,
|
||||
plugins: [
|
||||
vue(),
|
||||
resolveElectron(
|
||||
electron(),
|
||||
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
|
||||
* @example
|
||||
* {
|
||||
* 'electron-store': 'const Store = require("electron-store"); export default Store;',
|
||||
* }
|
||||
*/
|
||||
{
|
||||
sqlite3: 'const sqlite3 = require("sqlite3"); export default sqlite3;',
|
||||
},
|
||||
// If you use electron-store, this will work
|
||||
'electron-store': 'const Store = require("electron-store"); export default Store;',
|
||||
}
|
||||
),
|
||||
],
|
||||
base: './',
|
||||
@ -35,77 +33,3 @@ export default defineConfig({
|
||||
port: pkg.env.VITE_DEV_SERVER_PORT,
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* For usage of Electron and NodeJS APIs in the Renderer process
|
||||
* @see https://github.com/caoxiemeihao/electron-vue-vite/issues/52
|
||||
*/
|
||||
export function resolveElectron(
|
||||
entries: Parameters<typeof optimizer>[0] = {}
|
||||
): Plugin {
|
||||
const builtins = builtinModules.filter((t) => !t.startsWith('_'))
|
||||
/**
|
||||
* @see https://github.com/caoxiemeihao/vite-plugins/tree/main/packages/resolve#readme
|
||||
*/
|
||||
return optimizer({
|
||||
electron: electronExport(),
|
||||
...builtinModulesExport(builtins),
|
||||
...entries,
|
||||
})
|
||||
|
||||
function electronExport() {
|
||||
return `
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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 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 = `
|
||||
${requireModule}
|
||||
|
||||
${exportDefault}
|
||||
|
||||
${exportMembers}
|
||||
`
|
||||
|
||||
return { [moduleId]: nodeModuleCode }
|
||||
})
|
||||
.reduce((memo, item) => Object.assign(memo, item), {})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user