refactor: better script and vite-config

This commit is contained in:
草鞋没号
2022-02-08 11:14:05 +08:00
parent 6e184ad54c
commit 77046fc5b5
10 changed files with 144 additions and 141 deletions

View File

@@ -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)
}

View 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 || {}),
],
},
},
})