From 87adbb80e53a9bf39823a858c7759bd46c4c26f7 Mon Sep 17 00:00:00 2001 From: duan602728596 Date: Mon, 12 Aug 2024 13:13:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0sourcemap=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=92=8C=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/main/gulpfile.mjs | 11 +++- packages/main/package.json | 2 + packages/main/src/ProcessWindow.mts | 62 +++++++++++++++++++ packages/main/src/_sourcemap.mts | 3 + packages/main/src/ipc.mts | 6 +- .../main/src/ipcListener/openDevTools.mts | 7 ++- packages/main/src/main.mts | 60 +----------------- 7 files changed, 85 insertions(+), 66 deletions(-) create mode 100644 packages/main/src/ProcessWindow.mts create mode 100644 packages/main/src/_sourcemap.mts diff --git a/packages/main/gulpfile.mjs b/packages/main/gulpfile.mjs index c45b2fb0..5ccd00e3 100644 --- a/packages/main/gulpfile.mjs +++ b/packages/main/gulpfile.mjs @@ -1,13 +1,18 @@ import gulp from 'gulp'; -import terser from 'gulp-terser'; +import gulpTerser from 'gulp-terser'; +import gulpChangeFileContent from 'gulp-change-file-content'; /* 代码压缩 */ function minifyCode() { return gulp.src([ '.lib.mid/**/*.{js,cjs,mjs}', - '!.lib.mid/**/channelEnum.{js,cjs,mjs}' + '!.lib.mid/**/channelEnum.{js,cjs,mjs}', + '!.lib.mid/_sourcemap.{js,cjs,mjs}' ]) - .pipe(terser({ + .pipe(gulpChangeFileContent((content) => { + return content.replace(/(\/\*.*@#START_DEV_1.*\*\/).*(\/\*.*@#END_DEV_1.*\*\/)/, ''); + })) + .pipe(gulpTerser({ ecma: 2020, module: true })) diff --git a/packages/main/package.json b/packages/main/package.json index 55e50087..7f555e51 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -13,7 +13,9 @@ }, "devDependencies": { "gulp": "^4.0.2", + "gulp-change-file-content": "^1.0.1", "gulp-terser": "^2.1.0", + "source-map-support": "^0.5.21", "typescript": "^5.3.3" } } diff --git a/packages/main/src/ProcessWindow.mts b/packages/main/src/ProcessWindow.mts new file mode 100644 index 00000000..d74ab7c9 --- /dev/null +++ b/packages/main/src/ProcessWindow.mts @@ -0,0 +1,62 @@ +/* @#START_DEV_1 */ import './_sourcemap.mjs'; /* @#END_DEV_1 */ +import { BrowserWindow, Menu } from 'electron'; +import { createHtmlFilePath, isDevelopment, packageJson, titleBarIcon } from './utils.mjs'; +import { ipc, removeIpc } from './ipc.mjs'; +import logProtocol from './logProtocol/logProtocol.mjs'; +import { proxyServerClose } from './proxyServer/proxyServer.mjs'; +import xiaohongshuHandle, { closeAll as xiaohongshuCloseAll } from './ipcHandle/xiaohongshuHandle.mjs'; +import { nodeNimHandleLogin, nodeNimCleanup } from './ipcHandle/nodeNimHandleLogin.mjs'; +import ipcRemoteHandle from './ipcHandle/ipcRemoteHandle.mjs'; +import webRequest from './webRequest/webRequest.mjs'; + +export let processWindow: BrowserWindow | null = null; + +/* 窗口关闭事件 */ +async function handleProcessWindowClosed(): Promise { + await proxyServerClose(); + xiaohongshuCloseAll(); + nodeNimCleanup(); + removeIpc(); + processWindow = null; +} + +/* 初始化窗口 */ +export function createWindow(): void { + /* 初始化日志 */ + logProtocol(); + + /* 初始化窗口 */ + processWindow = new BrowserWindow({ + width: 1000, + height: 800, + webPreferences: { + nodeIntegration: true, + nodeIntegrationInWorker: true, + webSecurity: false, + contextIsolation: false + }, + title: `qqtools - ${ packageJson.version }`, + icon: titleBarIcon + }); + + if (isDevelopment) { + processWindow.webContents.openDevTools(); + } + + processWindow.loadFile(createHtmlFilePath('index')); + + Menu.setApplicationMenu(null); // 去掉顶层菜单 + + /* 事件监听和拦截协议的绑定 */ + ipc(); + + try { + ipcRemoteHandle(); + xiaohongshuHandle(); + nodeNimHandleLogin(); + } catch {} + + processWindow.on('closed', handleProcessWindowClosed); + + webRequest(); +} \ No newline at end of file diff --git a/packages/main/src/_sourcemap.mts b/packages/main/src/_sourcemap.mts new file mode 100644 index 00000000..6cfdac5a --- /dev/null +++ b/packages/main/src/_sourcemap.mts @@ -0,0 +1,3 @@ +import sourcemap from 'source-map-support'; + +sourcemap.install(); \ No newline at end of file diff --git a/packages/main/src/ipc.mts b/packages/main/src/ipc.mts index 071c19c6..29fba867 100644 --- a/packages/main/src/ipc.mts +++ b/packages/main/src/ipc.mts @@ -1,4 +1,4 @@ -import { ipcMain, type BrowserWindow } from 'electron'; +import { ipcMain } from 'electron'; import openDevTools from './ipcListener/openDevTools.mjs'; import { proxyServerInit } from './proxyServer/proxyServer.mjs'; import { WinIpcChannel, ProxyServerChannel } from './channelEnum.js'; @@ -16,7 +16,7 @@ export function removeIpc(): void { } /* ipc通信 */ -export function ipc(win: BrowserWindow): void { - openDevTools(win); +export function ipc(): void { + openDevTools(); proxyServerInit(); } \ No newline at end of file diff --git a/packages/main/src/ipcListener/openDevTools.mts b/packages/main/src/ipcListener/openDevTools.mts index 37082d61..86040681 100644 --- a/packages/main/src/ipcListener/openDevTools.mts +++ b/packages/main/src/ipcListener/openDevTools.mts @@ -1,10 +1,11 @@ -import { ipcMain, type BrowserWindow, type IpcMainEvent } from 'electron'; +import { ipcMain, type IpcMainEvent } from 'electron'; import { WinIpcChannel } from '../channelEnum.js'; +import { processWindow } from '../ProcessWindow.mjs'; /* 打开开发者工具 */ -function openDevTools(win: BrowserWindow): void { +function openDevTools(): void { ipcMain.on(WinIpcChannel.DeveloperTools, function(event: IpcMainEvent): void { - win.webContents.openDevTools(); + processWindow?.webContents.openDevTools(); }); } diff --git a/packages/main/src/main.mts b/packages/main/src/main.mts index d7177fe6..8ac6904e 100644 --- a/packages/main/src/main.mts +++ b/packages/main/src/main.mts @@ -1,64 +1,10 @@ import * as process from 'node:process'; -import { app, BrowserWindow, Menu } from 'electron'; -import { isDevelopment, titleBarIcon, createHtmlFilePath, packageJson } from './utils.mjs'; -import { ipc, removeIpc } from './ipc.mjs'; -import ipcRemoteHandle from './ipcHandle/ipcRemoteHandle.mjs'; -import xiaohongshuHandle, { closeAll as xiaohongshuCloseAll } from './ipcHandle/xiaohongshuHandle.mjs'; -import { nodeNimHandleLogin, nodeNimCleanup } from './ipcHandle/nodeNimHandleLogin.mjs'; -import webRequest from './webRequest/webRequest.mjs'; -import { proxyServerClose } from './proxyServer/proxyServer.mjs'; -import logProtocol from './logProtocol/logProtocol.mjs'; +import { app } from 'electron'; +import { nodeNimCleanup } from './ipcHandle/nodeNimHandleLogin.mjs'; +import { createWindow, processWindow } from './ProcessWindow.mjs'; process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = '1'; // 关闭警告 -/* BrowserWindow窗口对象 */ -let processWindow: BrowserWindow | null = null; - -/* 初始化 */ -function createWindow(): void { - logProtocol(); - - processWindow = new BrowserWindow({ - width: 1000, - height: 800, - webPreferences: { - nodeIntegration: true, - nodeIntegrationInWorker: true, - webSecurity: false, - contextIsolation: false - }, - title: `qqtools - ${ packageJson.version }`, - icon: titleBarIcon - }); - - if (isDevelopment) { - processWindow.webContents.openDevTools(); - } - - processWindow.loadFile(createHtmlFilePath('index')); - - // 去掉顶层菜单 - Menu.setApplicationMenu(null); - - ipc(processWindow); - - try { - ipcRemoteHandle(); - xiaohongshuHandle(); - nodeNimHandleLogin(); - } catch {} - - processWindow.on('closed', async function(): Promise { - await proxyServerClose(); - xiaohongshuCloseAll(); - nodeNimCleanup(); - removeIpc(); - processWindow = null; - }); - - webRequest(); -} - app.whenReady().then(createWindow); app.on('window-all-closed', function(): void {