Skip to content

Commit

Permalink
chore: upgrade deps & scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Oct 11, 2024
1 parent 75c514d commit 07eec1c
Show file tree
Hide file tree
Showing 17 changed files with 3,317 additions and 2,197 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Release for Windows
if: matrix.os == 'windows-latest'
run: |
pnpm build:app
pnpm pack:dev
- name: Release for MacOS
if: matrix.os == 'macos-latest'
Expand Down Expand Up @@ -77,9 +77,9 @@ jobs:
# apply provisioning profile
mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles
cp $PP_PATH ~/Library/MobileDevice/Provisioning\ Profiles
pnpm build:app
pnpm pack:dev
- name: Release for Linux
if: matrix.os == 'ubuntu-latest'
run: |
pnpm build:app
pnpm pack:dev
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ node_modules
.pnp
.pnp.js
dist/
dist-vite/
**/components.d.ts
**/auto-imports.d.ts

# testing
coverage
Expand Down Expand Up @@ -36,3 +39,5 @@ yarn-error.log*
# vitepress build output
.vitepress/dist
.vitepress/cache

vite.config.ts.*
2 changes: 1 addition & 1 deletion apps/electron/.electron-vendors.cache.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"chrome":"126","node":"20"}
{ "chrome": "128", "node": "20" }
3 changes: 3 additions & 0 deletions apps/electron/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 测试环境配置
NODE_ENV=development
VITE_APP_VERSION=0.2.0
3 changes: 3 additions & 0 deletions apps/electron/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 生产环境配置
NODE_ENV=production
VITE_APP_VERSION=0.1.0
3 changes: 3 additions & 0 deletions apps/electron/dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider: generic
url: 'http://localhost:8080'
# url: 'https://lumibrowser-test.oss-cn-shenzhen.aliyuncs.com/public/package/app/Windows/64/1.5.0'
106 changes: 106 additions & 0 deletions apps/electron/dist-vite/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"use strict";
const electron = require("electron");
const node_path = require("node:path");
class IPCMain {
channel;
listeners = {};
constructor(channel = "IPC-bridge") {
this.channel = channel;
this.bindMessage();
}
on(name, fn) {
console.log("on", name);
if (this.listeners[name])
throw new Error(`消息处理器 ${String(name)} 已存在`);
this.listeners[name] = fn;
}
off(action) {
if (this.listeners[action]) {
delete this.listeners[action];
}
}
async send(name, ...payload) {
const windows = electron.BrowserWindow.getAllWindows();
windows.forEach((window) => {
window.webContents.send(this.channel, {
name,
payload
});
});
}
bindMessage() {
electron.ipcMain.handle(this.channel, this.handleReceivingMessage.bind(this));
}
async handleReceivingMessage(event, payload) {
try {
if (this.listeners[payload.name]) {
const res = await this.listeners[payload.name](...payload.payload);
return {
type: "success",
result: res
};
} else {
throw new Error(`未知的 IPC 消息 ${String(payload.name)}`);
}
} catch (e) {
return {
type: "error",
error: e.toString()
};
}
}
}
const ipcMain = new IPCMain();
ipcMain.on("getUsernameById", (userID) => {
console.log("getUsernameById", `User ID: ${userID}`);
return "User Name";
});
setTimeout(() => {
ipcMain.send("newUserJoin", 1);
}, 5e3);
const isPackaged = electron.app.isPackaged;
process.platform === "darwin";
process.platform === "win32";
async function createWindow() {
const browserWindow = new electron.BrowserWindow({
// Use 'ready-to-show' event to show window
show: false,
webPreferences: {
// https://www.electronjs.org/docs/latest/api/webview-tag#warning
webviewTag: false,
preload: isPackaged ? node_path.join(__dirname, "./preload/index.cjs") : node_path.join(__dirname, "../../preload/dist/index.cjs")
}
});
browserWindow.on("ready-to-show", () => {
browserWindow?.show();
{
browserWindow?.webContents.openDevTools({ mode: "detach" });
}
});
const pageUrl = "http://localhost:5173/";
await browserWindow.loadURL(pageUrl);
return browserWindow;
}
async function restoreOrCreateWindow() {
let window = electron.BrowserWindow.getAllWindows().find((w) => !w.isDestroyed());
if (window === void 0) {
window = await createWindow();
}
if (window.isMinimized()) {
window.restore();
}
window.focus();
}
const isSingleInstance = electron.app.requestSingleInstanceLock();
if (!isSingleInstance) {
electron.app.quit();
process.exit(0);
}
electron.app.on("second-instance", restoreOrCreateWindow);
electron.app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
electron.app.quit();
}
});
electron.app.on("activate", restoreOrCreateWindow);
electron.app.whenReady().then(restoreOrCreateWindow).catch((e) => console.error("Failed create window:", e));
30 changes: 16 additions & 14 deletions apps/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@
"type": "git",
"url": "https://github.com/buqiyuan/electron-vite-monorepo.git"
},
"main": "dist/index.cjs",
"main": "dist/main.cjs",
"engines": {
"node": ">=v16.13",
"npm": ">=8.1"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"build:app": "npx tsx scripts/build.ts",
"compile": "npm run build && npm run build:app",
"lint": "eslint . --ext js,ts",
"bundle": "npx rimraf ./dist && webpack --config webpack.config.js",
"build:dev": "vite build --mode development && pnpm bundle",
"build:prod": "vite build && pnpm bundle",
"pack:app": "npx tsx scripts/build.ts",
"pack:dev": "pnpm build:dev && dotenvx run -f .env.development -- pnpm pack:app",
"pack:prod": "pnpm build:prod && dotenvx run -f .env.production -- pnpm pack:app",
"typecheck": "tsc --noEmit -p tsconfig.json",
"postinstall": "cross-env ELECTRON_RUN_AS_NODE=1 electron ../../scripts/update-electron-vendors.js"
},
"dependencies": {
"electron-updater": "6.2.1"
},
"devDependencies": {
"@electron/notarize": "^2.3.2",
"@electron/notarize": "^2.5.0",
"@repo/electron-preload": "workspace:*",
"@types/node": "^22.2.0",
"electron": "31.3.1",
"electron-builder": "24.13.3",
"typescript": "5.5.4",
"vite": "5.4.0"
"electron": "32.2.0",
"electron-builder": "25.1.7",
"electron-updater": "6.3.9",
"terser-webpack-plugin": "^5.3.10",
"typescript": "5.6.3",
"vite": "5.4.8",
"webpack": "^5.95.0",
"webpack-cli": "^5.1.4"
}
}
22 changes: 16 additions & 6 deletions apps/electron/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { Configuration } from 'electron-builder'
import { cpSync } from 'node:fs'
import process, { exit, platform } from 'node:process'
import path from 'node:path'
import { Platform, build } from 'electron-builder'
import type { Configuration } from 'electron-builder'
import process, { exit, platform } from 'node:process'
import { build, Platform } from 'electron-builder'

const version = process.env.npm_package_version
console.log('版本号:', version)
const version = process.env.VITE_APP_VERSION
const isDev = process.env.NODE_ENV === 'development'
console.log('是否是测试环境:', isDev)
console.log('APP 版本号:', version)

const workDir = path.join(__dirname, '../')

Expand All @@ -19,11 +21,19 @@ const options: Configuration = {
productName: 'ElectronApp',
copyright: 'ElectronApp',
asar: true,
extraMetadata: {
version,
name: 'ElectronViteApp',
main: 'dist/main.cjs',
},
directories: {
output: 'out',
buildResources: 'buildResources',
},
files: ['dist'],
files: [
'dist',
'resources',
],
protocols: {
name: 'Deeplink Example',
// Don't forget to set `MimeType: "x-scheme-handler/deeplink"` for `linux.desktop` entry!
Expand Down
6 changes: 3 additions & 3 deletions apps/electron/src/mainWindow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BrowserWindow } from 'electron'
import { join } from 'node:path'
import { isDev, isProd } from '/@/utils/'
import { BrowserWindow } from 'electron'
import { isDev, isPackaged } from '/@/utils/'

async function createWindow() {
const browserWindow = new BrowserWindow({
Expand All @@ -9,7 +9,7 @@ async function createWindow() {
webPreferences: {
// https://www.electronjs.org/docs/latest/api/webview-tag#warning
webviewTag: false,
preload: isProd ? join(__dirname, './preload/index.cjs') : join(__dirname, '../../preload/dist/index.cjs'),
preload: isPackaged ? join(__dirname, './preload/index.cjs') : join(__dirname, '../../preload/dist/index.cjs'),
},
})

Expand Down
9 changes: 9 additions & 0 deletions apps/electron/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import { app } from 'electron'

/** 是否处于开发环境 */
export const isDev = import.meta.env.DEV
/** 是否处于生产环境 */
export const isProd = import.meta.env.PROD
/** 是否处于打包状态 */
export const isPackaged = app.isPackaged

export const isMac = process.platform === 'darwin'
export const isWindows = process.platform === 'win32'
16 changes: 9 additions & 7 deletions apps/electron/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { join } from 'node:path'
import type { UserConfig } from 'vite'
import { join } from 'node:path'
import { node } from './.electron-vendors.cache.json'

const PACKAGE_ROOT = __dirname
const PROJECT_ROOT = join(PACKAGE_ROOT, '../..')
// const PROJECT_ROOT = join(PACKAGE_ROOT, '../..')

const config: UserConfig = {
mode: process.env.MODE,
envDir: PACKAGE_ROOT,
root: PACKAGE_ROOT,
envDir: PROJECT_ROOT,
resolve: {
alias: {
'/@/': `${join(PACKAGE_ROOT, 'src')}/`,
},
},
build: {
ssr: true,
sourcemap: 'inline',
sourcemap: false,
target: `node${node}`,
outDir: 'dist',
outDir: 'dist-vite',
assetsDir: '.',
minify: process.env.MODE !== 'development',
lib: {
entry: 'src/index.ts',
entry: {
index: join(PACKAGE_ROOT, 'src/index.ts'),
},
formats: ['cjs'],
},
rollupOptions: {
output: {
entryFileNames: '[name].cjs',
chunkFileNames: '[name].cjs',
},
},
emptyOutDir: true,
Expand Down
57 changes: 57 additions & 0 deletions apps/electron/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const path = require('node:path')
const TerserPlugin = require('terser-webpack-plugin')

/** @type { import('webpack').Configuration } */
module.exports = {
mode: 'production', // 或者 'development'
target: 'electron-main', // 目标环境设置为 Node.js
entry: {
main: './dist-vite/index.cjs',
// extensionWorker: './dist-vite/extensionWorker.cjs',
},
output: {
path: path.resolve(__dirname, 'dist'), // 输出目录
filename: '[name].cjs', // 使用 [name] 占位符来确保每个 chunk 有唯一的文件名
},
node: {
__dirname: false, // 保持 __dirname 的原样(在 Node.js 中很重要)
__filename: false,
},
optimization: {
minimize: true, // 启用代码压缩和混淆
minimizer: [
new TerserPlugin({
terserOptions: {
compress: true, // 启用代码压缩
mangle: true, // 混淆变量名称
},
extractComments: false, // 禁止生成 LICENSE.txt 文件
}),
],
splitChunks: {
chunks: 'all', // 对所有类型的代码进行分割
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
common: {
name: 'common',
minChunks: 2, // 至少两个 chunk 共享的模块会被提取到 common chunk 中
priority: -10,
reuseExistingChunk: true,
},
},
},
},
// module: {
// rules: [
// {
// test: /\.js$/,
// exclude: /node_modules/,
// use: 'babel-loader', // 如果需要转换现代 JavaScript 语法
// },
// ],
// },
}
6 changes: 0 additions & 6 deletions apps/server/nodemon.json

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/.browserslistrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Chrome 126
Chrome 128
Loading

0 comments on commit 07eec1c

Please sign in to comment.