Added more comments and changed existing ones

This commit is contained in:
Paul committed 2022-02-06 20:08:30 +02:00
1 parent d83b9644f3
commit 77c73c7a1c
8 files changed
+98 -83

No files matched your search

+70 -56
View File
@@ -5,21 +5,24 @@ import react from '@vitejs/plugin-react'
import resolve from 'vite-plugin-resolve'
import pkg from '../../package.json'
// https://vitejs.dev/config/
/**
* @see https://vitejs.dev/config/
*/
export default defineConfig({
mode: process.env.NODE_ENV,
root: __dirname,
plugins: [
react(),
resolveElectron(
/**
* you can custom other module in here
* 🚧 need to make sure custom-resolve-module in `dependencies`, that will ensure that the electron-builder can package them correctly
* @example
* {
* 'electron-store': 'const Store = require("electron-store"); export defalut Store;',
* }
*/
/**
* 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;',
* }
*/
),
],
base: './',
@@ -30,7 +33,7 @@ export default defineConfig({
resolve: {
alias: {
'@': join(__dirname, 'src'),
'src': join(__dirname, '../../src'),
src: join(__dirname, '../../src'),
},
},
server: {
@@ -39,12 +42,18 @@ export default defineConfig({
},
})
// ------- For use Electron, NodeJs in Renderer-process -------
// 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('_'))
/**
* 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('_'))
// https://github.com/caoxiemeihao/vite-plugins/tree/main/packages/resolve#readme
/**
* @see https://github.com/caoxiemeihao/vite-plugins/tree/main/packages/resolve#readme
*/
return resolve({
electron: electronExport(),
...builtinModulesExport(builtins),
@@ -53,52 +62,57 @@ export function resolveElectron(resolves: Parameters<typeof resolve>[0] = {}): P
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,
}
`
/**
* 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}
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}
${exportDefault}
${exportMembers}
`
${exportMembers}
`
return { [moduleId]: nodeModuleCode }
}).reduce((memo, item) => Object.assign(memo, item), {})
return { [moduleId]: nodeModuleCode }
})
.reduce((memo, item) => Object.assign(memo, item), {})
}
}