提交 a000dba0 作者: 方治民

feat: 完成 electron dev/preview/build

上级 2dd65128
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
VITE_USE_MOCK = true VITE_USE_MOCK = true
# public path # public path
VITE_PUBLIC_PATH = / VITE_PUBLIC_PATH = ./
# Cross-domain proxy, you can configure multiple # Cross-domain proxy, you can configure multiple
# Please note that no line breaks # Please note that no line breaks
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
VITE_USE_MOCK = false VITE_USE_MOCK = false
# public path # public path
VITE_PUBLIC_PATH = / VITE_PUBLIC_PATH = ./
# Delete console # Delete console
VITE_DROP_CONSOLE = true VITE_DROP_CONSOLE = true
...@@ -17,7 +17,7 @@ VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = false ...@@ -17,7 +17,7 @@ VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = false
# Basic interface address SPA # Basic interface address SPA
# VITE_GLOB_API_URL=/basic-api # VITE_GLOB_API_URL=/basic-api
VITE_GLOB_API_URL=http://192.168.0.156:17181 VITE_GLOB_API_URL=http://192.168.0.156:8081
# Interface prefix # Interface prefix
# VITE_GLOB_API_URL_PREFIX= # VITE_GLOB_API_URL_PREFIX=
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
VITE_USE_MOCK = false VITE_USE_MOCK = false
# public path # public path
VITE_PUBLIC_PATH = / VITE_PUBLIC_PATH = ./
# Delete console # Delete console
VITE_DROP_CONSOLE = true VITE_DROP_CONSOLE = true
......
...@@ -18,7 +18,7 @@ export function isReportMode(): boolean { ...@@ -18,7 +18,7 @@ export function isReportMode(): boolean {
} }
// Read all environment variable configuration files to process.env // Read all environment variable configuration files to process.env
export function wrapperEnv(envConf: Recordable): ViteEnv { export function wrapperEnv(envConf: Recordable, includePort = false): ViteEnv {
const ret: any = {} const ret: any = {}
for (const envName of Object.keys(envConf)) { for (const envName of Object.keys(envConf)) {
...@@ -27,6 +27,9 @@ export function wrapperEnv(envConf: Recordable): ViteEnv { ...@@ -27,6 +27,9 @@ export function wrapperEnv(envConf: Recordable): ViteEnv {
if (envName === 'VITE_PORT') { if (envName === 'VITE_PORT') {
realName = Number(realName) realName = Number(realName)
if (includePort) {
process.env[envName] = realName
}
} }
if (envName === 'VITE_PROXY' && realName) { if (envName === 'VITE_PROXY' && realName) {
try { try {
......
...@@ -22,7 +22,7 @@ export function createProxy(list: ProxyList = []) { ...@@ -22,7 +22,7 @@ export function createProxy(list: ProxyList = []) {
// https://github.com/http-party/node-http-proxy#options // https://github.com/http-party/node-http-proxy#options
ret[prefix] = { ret[prefix] = {
target: target, target,
changeOrigin: true, changeOrigin: true,
ws: true, ws: true,
rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''), rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
......
appId: com.yiring.basic.electron.app appId: com.yiring.basic.electron.app
productName: Basic Electron App productName: 'Basic Electron App'
directories: directories:
buildResources: build buildResources: build
output: release/${version}
files: files:
- '!**/.vscode/*' - 'out'
- '!src/*' - 'build/electron'
- '!electron/*'
- '!tests/*'
- '!types/*'
- '!mock/*'
- '!electron.vite.config.{js,ts,mjs,cjs}'
- '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
- '!{tsconfig.json,tsconfig.node.json,tsconfig.web.json}'
asarUnpack: asarUnpack:
- '**/*.{node,dll}' - '**/*.{node,dll}'
afterSign: build/electron/notarize.js afterSign: build/electron/notarize.js
win: win:
executableName: basic-electron-app executableName: basic-electron-app
nsis: nsis:
artifactName: ${name}-${version}-setup.${ext} artifactName: ${productName}_${version}.${ext}
shortcutName: ${productName} shortcutName: ${productName}
uninstallDisplayName: ${productName} uninstallDisplayName: ${productName}
createDesktopShortcut: always createDesktopShortcut: always
oneClick: false
perMachine: false
allowToChangeInstallationDirectory: true
deleteAppDataOnUninstall: false
mac: mac:
entitlementsInherit: build/electron/entitlements.mac.plist entitlementsInherit: build/electron/entitlements.mac.plist
extendInfo: extendInfo:
......
import { resolve } from 'node:path' import { resolve } from 'node:path'
import { merge } from 'lodash-es' import { merge } from 'lodash-es'
import { defineConfig, defineViteConfig, externalizeDepsPlugin } from 'electron-vite' import { defineConfig, defineViteConfig, externalizeDepsPlugin } from 'electron-vite'
import type { ConfigEnv, UserConfig } from 'vite'
import { loadEnv } from 'vite'
import { wrapperEnv } from './build/utils'
export default defineConfig({ export default defineConfig({
main: { main: defineViteConfig(({ command, mode }: ConfigEnv) => {
plugins: [externalizeDepsPlugin()], // The boolean type read by loadEnv is a string. This function can be converted to boolean type
build: { const viteEnv = wrapperEnv(loadEnv(mode, process.cwd()), true)
rollupOptions: { if (command === 'serve') {
input: { process.env.ELECTRON_RENDERER_URL = `http://localhost:${viteEnv.VITE_PORT}`
index: resolve(__dirname, 'electron/main/index.ts'), }
return {
plugins: [externalizeDepsPlugin()],
envPrefix: ['MAIN_VITE_', 'VITE_', 'ELECTRON_'],
build: {
rollupOptions: {
input: {
index: resolve(__dirname, 'src-electron/main/index.ts'),
},
}, },
}, },
}, }
}, }),
preload: { preload: {
plugins: [externalizeDepsPlugin()], plugins: [externalizeDepsPlugin()],
build: { build: {
rollupOptions: { rollupOptions: {
input: { input: {
index: resolve(__dirname, 'electron/preload/index.ts'), index: resolve(__dirname, 'src-electron/preload/index.ts'),
}, },
}, },
}, },
}, },
renderer: defineViteConfig(async (env) => { renderer: defineViteConfig(async (env): Promise<UserConfig> => {
const renderConfig = (await import('./vite.config')).default // 设置运行时环境,用于减少一些不必要的构建处理
const config = renderConfig(env) process.env.RUNTIME = 'electron'
return merge(config, { const defaultConfig = (await import('./vite.config')).default(env)
const config: UserConfig = {
envPrefix: 'VITE_',
build: { build: {
target: 'chrome80', target: 'chrome80',
rollupOptions: { rollupOptions: {
...@@ -36,6 +50,8 @@ export default defineConfig({ ...@@ -36,6 +50,8 @@ export default defineConfig({
}, },
}, },
}, },
}) }
return merge(defaultConfig, config)
}), }),
}) })
...@@ -27,9 +27,11 @@ ...@@ -27,9 +27,11 @@
"serve": "npm run dev", "serve": "npm run dev",
"dev": "vite", "dev": "vite",
"dev:app": "electron-vite dev", "dev:app": "electron-vite dev",
"build": "cross-env NODE_ENV=production vite build && esno ./build/script/postBuild.ts", "preview:app": "electron-vite preview",
"build:test": "cross-env vite build --mode test && esno ./build/script/postBuild.ts", "build:app": "electron-vite build",
"build:preview": "cross-env vite build --mode preview && esno ./build/script/postBuild.ts", "build": "vite build",
"build:test": "vite build --mode test",
"build:preview": "vite build --mode preview",
"build:no-cache": "pnpm clean:cache && npm run build", "build:no-cache": "pnpm clean:cache && npm run build",
"report": "cross-env REPORT=true npm run build", "report": "cross-env REPORT=true npm run build",
"type:check": "vue-tsc --noEmit --skipLibCheck", "type:check": "vue-tsc --noEmit --skipLibCheck",
...@@ -48,7 +50,11 @@ ...@@ -48,7 +50,11 @@
"test:br": "npx http-server dist --cors --brotli -c-1", "test:br": "npx http-server dist --cors --brotli -c-1",
"reinstall": "rimraf pnpm-lock.yaml && rimraf package.lock.json && rimraf node_modules && npm run bootstrap", "reinstall": "rimraf pnpm-lock.yaml && rimraf package.lock.json && rimraf node_modules && npm run bootstrap",
"prepare": "husky install", "prepare": "husky install",
"gen:icon": "esno ./build/generate/icon/index.ts" "gen:icon": "esno ./build/generate/icon/index.ts",
"postinstall": "electron-builder install-app-deps",
"build:win": "npm run build:app && electron-builder --win --config",
"build:mac": "npm run build:app && electron-builder --mac --config",
"build:linux": "npm run build:app && electron-builder --linux --config"
}, },
"config": { "config": {
"commitizen": { "commitizen": {
...@@ -63,6 +69,7 @@ ...@@ -63,6 +69,7 @@
"@iconify/iconify": "^3.1.0", "@iconify/iconify": "^3.1.0",
"@logicflow/core": "^1.2.1", "@logicflow/core": "^1.2.1",
"@logicflow/extension": "^1.2.1", "@logicflow/extension": "^1.2.1",
"@purge-icons/generated": "^0.9.0",
"@stomp/stompjs": "^7.0.0", "@stomp/stompjs": "^7.0.0",
"@vue/runtime-core": "^3.2.47", "@vue/runtime-core": "^3.2.47",
"@vue/shared": "^3.2.47", "@vue/shared": "^3.2.47",
...@@ -109,13 +116,12 @@ ...@@ -109,13 +116,12 @@
"@electron-toolkit/tsconfig": "^1.0.1", "@electron-toolkit/tsconfig": "^1.0.1",
"@electron/notarize": "^1.2.3", "@electron/notarize": "^1.2.3",
"@iconify/json": "^2.2.40", "@iconify/json": "^2.2.40",
"@purge-icons/generated": "^0.9.0",
"@types/codemirror": "^5.60.7", "@types/codemirror": "^5.60.7",
"@types/crypto-js": "^4.1.1", "@types/crypto-js": "^4.1.1",
"@types/fs-extra": "^11.0.1", "@types/fs-extra": "^11.0.1",
"@types/inquirer": "^9.0.3", "@types/inquirer": "^9.0.3",
"@types/intro.js": "^5.1.1", "@types/intro.js": "^5.1.1",
"@types/lodash": "^4.14.191", "@types/lodash": "^4.14.192",
"@types/lodash-es": "^4.17.7", "@types/lodash-es": "^4.17.7",
"@types/mockjs": "^1.0.7", "@types/mockjs": "^1.0.7",
"@types/node": "^18.15.10", "@types/node": "^18.15.10",
...@@ -126,8 +132,8 @@ ...@@ -126,8 +132,8 @@
"@types/sockjs-client": "^1.5.1", "@types/sockjs-client": "^1.5.1",
"@types/sortablejs": "^1.15.1", "@types/sortablejs": "^1.15.1",
"@types/stompjs": "^2.3.5", "@types/stompjs": "^2.3.5",
"@typescript-eslint/eslint-plugin": "^5.56.0", "@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.56.0", "@typescript-eslint/parser": "^5.57.0",
"@vitejs/plugin-legacy": "^4.0.2", "@vitejs/plugin-legacy": "^4.0.2",
"@vitejs/plugin-vue": "^4.1.0", "@vitejs/plugin-vue": "^4.1.0",
"@vitejs/plugin-vue-jsx": "^3.0.1", "@vitejs/plugin-vue-jsx": "^3.0.1",
......
...@@ -2,6 +2,14 @@ import * as path from 'node:path' ...@@ -2,6 +2,14 @@ import * as path from 'node:path'
import { BrowserWindow, app, shell } from 'electron' import { BrowserWindow, app, shell } from 'electron'
import { electronApp, is, optimizer } from '@electron-toolkit/utils' import { electronApp, is, optimizer } from '@electron-toolkit/utils'
// Remove electron security warnings
// This warning only shows in development mode
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
process.env.DIST_ELECTRON = path.join(__dirname, '..')
process.env.DIST = path.join(process.env.DIST_ELECTRON, '../dist')
process.env.PUBLIC = app.isPackaged ? __dirname : path.join(process.env.DIST_ELECTRON, '../public')
function createWindow(): void { function createWindow(): void {
// Create the browser window. // Create the browser window.
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
...@@ -34,7 +42,7 @@ function createWindow(): void { ...@@ -34,7 +42,7 @@ function createWindow(): void {
if (is.dev && process.env.ELECTRON_RENDERER_URL) { if (is.dev && process.env.ELECTRON_RENDERER_URL) {
mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL) mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL)
} else { } else {
mainWindow.loadFile(path.join(__dirname, '../../index.html')) mainWindow.loadFile(path.join(process.env.DIST as string, 'index.html'))
} }
} }
......
{ {
"compilerOptions": {
"esModuleInterop": true
},
"files": [],
"references": [{ "path": "./tsconfig.web.json" }, { "path": "./tsconfig.node.json" }] "references": [{ "path": "./tsconfig.web.json" }, { "path": "./tsconfig.node.json" }]
} }
...@@ -7,5 +7,13 @@ ...@@ -7,5 +7,13 @@
"/#/*": ["types/*"] "/#/*": ["types/*"]
} }
}, },
"include": ["package.json", "electron", "build", "vite.config.ts", "electron.vite.config.ts"] "include": [
"package.json",
"src-electron",
"build",
"vite.config.ts",
"electron.vite.config.ts",
"types/**/*.d.ts",
"types/**/*.ts"
]
} }
...@@ -43,7 +43,8 @@ ...@@ -43,7 +43,8 @@
"build/**/*.ts", "build/**/*.ts",
"build/**/*.d.ts", "build/**/*.d.ts",
"mock/**/*.ts", "mock/**/*.ts",
"vite.config.ts" "vite.config.ts",
"package.json"
], ],
"exclude": ["node_modules", "tests/server/**/*.ts", "dist", "**/*.js"] "exclude": ["node_modules", "tests/server/**/*.ts", "dist", "**/*.js"]
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论