Skip to content

Commit

Permalink
feat: 添加sourcemap支持和代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
duan602728596 committed Aug 12, 2024
1 parent 48d5e5f commit 87adbb8
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 66 deletions.
11 changes: 8 additions & 3 deletions packages/main/gulpfile.mjs
Original file line number Diff line number Diff line change
@@ -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
}))
Expand Down
2 changes: 2 additions & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
62 changes: 62 additions & 0 deletions packages/main/src/ProcessWindow.mts
Original file line number Diff line number Diff line change
@@ -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<void> {
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();
}
3 changes: 3 additions & 0 deletions packages/main/src/_sourcemap.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sourcemap from 'source-map-support';

sourcemap.install();
6 changes: 3 additions & 3 deletions packages/main/src/ipc.mts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,7 +16,7 @@ export function removeIpc(): void {
}

/* ipc通信 */
export function ipc(win: BrowserWindow): void {
openDevTools(win);
export function ipc(): void {
openDevTools();
proxyServerInit();
}
7 changes: 4 additions & 3 deletions packages/main/src/ipcListener/openDevTools.mts
Original file line number Diff line number Diff line change
@@ -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();
});
}

Expand Down
60 changes: 3 additions & 57 deletions packages/main/src/main.mts
Original file line number Diff line number Diff line change
@@ -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<void> {
await proxyServerClose();
xiaohongshuCloseAll();
nodeNimCleanup();
removeIpc();
processWindow = null;
});

webRequest();
}

app.whenReady().then(createWindow);

app.on('window-all-closed', function(): void {
Expand Down

0 comments on commit 87adbb8

Please sign in to comment.