refactor: resolveElectron -> electron

This commit is contained in:
草鞋没号 2022-04-18 09:56:00 +08:00
parent 61e3bcdba3
commit 60aa955f36
1 changed files with 15 additions and 10 deletions

View File

@ -3,6 +3,7 @@ import { builtinModules } from 'module'
import { defineConfig, Plugin } from 'vite' import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import optimizer from 'vite-plugin-optimizer' import optimizer from 'vite-plugin-optimizer'
import resolve from 'vite-plugin-resolve'
import pkg from '../../package.json' import pkg from '../../package.json'
/** /**
@ -13,17 +14,21 @@ export default defineConfig({
root: __dirname, root: __dirname,
plugins: [ plugins: [
react(), react(),
resolveElectron( electron(),
resolve({
/** /**
* Here you can specify other modules * Here you resolve some CommonJs module.
* 🚧 You have to make sure that your module is in `dependencies` and not in the` devDependencies`, * Or some Node.js native modules they may not be built correctly by vite.
* which will ensure that the electron-builder can package it correctly * At the same time, these modules should be put in `dependencies`,
* @example * because they will not be built by vite, but will be packaged into `app.asar` by electron-builder
* {
* 'electron-store': 'const Store = require("electron-store"); export default Store;',
* }
*/ */
), 'electron-store': 'export default require("electron-store");',
// Node.js native module
serialport: `
const { SerialPort } = require("serialport");
export { SerialPort }
`,
}),
], ],
base: './', base: './',
build: { build: {
@ -46,7 +51,7 @@ export default defineConfig({
* For usage of Electron and NodeJS APIs in the Renderer process * For usage of Electron and NodeJS APIs in the Renderer process
* @see https://github.com/caoxiemeihao/electron-vue-vite/issues/52 * @see https://github.com/caoxiemeihao/electron-vue-vite/issues/52
*/ */
export function resolveElectron( export function electron(
entries: Parameters<typeof optimizer>[0] = {} entries: Parameters<typeof optimizer>[0] = {}
): Plugin { ): Plugin {
const builtins = builtinModules.filter((t) => !t.startsWith('_')) const builtins = builtinModules.filter((t) => !t.startsWith('_'))