mirror of
https://github.com/electron-vite/electron-vite-vue
synced 2025-08-29 06:47:29 +08:00
refactor: better script and vite-config
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import fs from 'fs'
|
||||
import { contextBridge, ipcRenderer, IpcRenderer } from 'electron'
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
import { domReady } from './utils'
|
||||
import { useLoading } from './loading'
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development'
|
||||
const { removeLoading, appendLoading } = useLoading()
|
||||
const { appendLoading, removeLoading } = useLoading()
|
||||
|
||||
;(async () => {
|
||||
await domReady()
|
||||
|
||||
domReady().then(() => {
|
||||
appendLoading()
|
||||
})
|
||||
})()
|
||||
|
||||
|
||||
// --------- Expose some API to Renderer process. ---------
|
||||
// --------- Expose some API to the Renderer process. ---------
|
||||
contextBridge.exposeInMainWorld('fs', fs)
|
||||
contextBridge.exposeInMainWorld('removeLoading', removeLoading)
|
||||
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
|
||||
|
||||
// `exposeInMainWorld` can not detect `prototype` attribute and methods, manually patch it.
|
||||
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
|
||||
function withPrototype(obj: Record<string, any>) {
|
||||
const protos = Object.getPrototypeOf(obj)
|
||||
|
||||
@@ -24,7 +24,7 @@ function withPrototype(obj: Record<string, any>) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
|
||||
|
||||
if (typeof value === 'function') {
|
||||
// Some native API not work in Renderer-process, like `NodeJS.EventEmitter['on']`. Wrap a function patch it.
|
||||
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
|
||||
obj[key] = function (...args: any) {
|
||||
return value.call(obj, ...args)
|
||||
}
|
||||
|
24
packages/preload/vite.config.ts
Normal file
24
packages/preload/vite.config.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { builtinModules } from 'module'
|
||||
import { defineConfig } from 'vite'
|
||||
import pkg from '../../package.json'
|
||||
|
||||
export default defineConfig({
|
||||
root: __dirname,
|
||||
build: {
|
||||
outDir: '../../dist/preload',
|
||||
lib: {
|
||||
entry: 'index.ts',
|
||||
formats: ['cjs'],
|
||||
fileName: () => '[name].cjs',
|
||||
},
|
||||
minify: process.env./* from mode option */NODE_ENV === 'production',
|
||||
emptyOutDir: true,
|
||||
rollupOptions: {
|
||||
external: [
|
||||
'electron',
|
||||
...builtinModules,
|
||||
...Object.keys(pkg.dependencies || {}),
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user