diff --git a/README.md b/README.md index 0f02abc..7aa2c0a 100644 --- a/README.md +++ b/README.md @@ -136,13 +136,13 @@ export default defineConfig({ // if (type === 'main') {} // if (type === 'preload') {} }, - //2.1.8新增 开启自定义主进程日志时,你需要自己过滤electron无用日志 + //2.1.9新增 开启自定义主进程日志时 logProcess(log: string, type: LogType) { - // if (type === 'normal') { - // logProcess('Main', log, chalk.blue); - // } else if (type === 'error') { - // logProcess('Main', log, chalk.red); - // } + if (type === 'normal') { + console.log(log); + } else if (type === 'error') { + console.error(log); + } }, builderOptions: { //配置参考 https://www.electron.build/configuration/configuration diff --git a/examples/demo/package.json b/examples/demo/package.json index c6d289b..7626543 100644 --- a/examples/demo/package.json +++ b/examples/demo/package.json @@ -43,7 +43,7 @@ "prettier": "^2.3.2", "react": "17.x", "react-dom": "17.x", - "umi-plugin-electron-builder": "^2.1.8", + "umi-plugin-electron-builder": "^2.1.9", "yorkie": "^2.0.0" } } diff --git a/examples/main+renderer/package.json b/examples/main+renderer/package.json index 82c4219..2a3b382 100644 --- a/examples/main+renderer/package.json +++ b/examples/main+renderer/package.json @@ -44,7 +44,7 @@ "prettier": "^2.3.2", "react": "17.x", "react-dom": "17.x", - "umi-plugin-electron-builder": "^2.1.8", + "umi-plugin-electron-builder": "^2.1.9", "yorkie": "^2.0.0" } } diff --git a/lib/compile/index.js b/lib/compile/index.js index e38ca3e..47256f3 100644 --- a/lib/compile/index.js +++ b/lib/compile/index.js @@ -257,11 +257,19 @@ var runDev = function runDev(api) { } spawnProcess = child_process_1.spawn(String(electronPath), ['--inspect=5858', path_1.default.join(utils_1.getDevBuildDir(api), 'main.js')]); - spawnProcess.stdout.on('data', function (d) { - return logProcess(d.toString(), 'normal'); + spawnProcess.stdout.on('data', function (data) { + var log = utils_1.filterText(data.toString()); + + if (log) { + logProcess(log, 'normal'); + } }); spawnProcess.stderr.on('data', function (data) { - logProcess(data.toString(), 'error'); + var log = utils_1.filterText(data.toString()); + + if (log) { + logProcess(log, 'error'); + } }); spawnProcess.on('close', function (code, signal) { if (signal != 'SIGKILL') { diff --git a/lib/utils/index.d.ts b/lib/utils/index.d.ts index 6cb8823..9ce9f1d 100644 --- a/lib/utils/index.d.ts +++ b/lib/utils/index.d.ts @@ -1,6 +1,4 @@ -/// import { IApi } from 'umi'; -import { ChildProcess } from 'child_process'; export declare function debounce(f: () => void, ms: number): () => void; export declare function installRely(pkgName: string): void; export declare function getRootPkg(): any; @@ -10,9 +8,6 @@ export declare function getPreloadSrc(api: IApi): string; export declare function getDevBuildDir(api: IApi): string; export declare function getBuildDir(api: IApi): string; export declare function getAbsOutputDir(api: IApi): string; -export interface LineFilter { - filter(line: string): boolean; -} -export declare function logProcessErrorOutput(label: 'Electron' | 'Renderer' | 'Main', childProcess: ChildProcess): void; +export declare function filterText(s: string): string | null; export declare function logError(label: 'Electron' | 'Renderer' | 'Main', error: Error): void; -export declare function logProcess(label: 'Electron' | 'Renderer' | 'Main', data: string | Buffer, labelColor: any, lineFilter?: LineFilter | null): void; +export declare function logProcess(label: 'Electron' | 'Renderer' | 'Main', log: string, labelColor: any): void; diff --git a/lib/utils/index.js b/lib/utils/index.js index eedb28b..c4ab4b0 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -43,7 +43,7 @@ var __importDefault = void 0 && (void 0).__importDefault || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); -exports.logProcess = exports.logError = exports.logProcessErrorOutput = exports.getAbsOutputDir = exports.getBuildDir = exports.getDevBuildDir = exports.getPreloadSrc = exports.getMainSrc = exports.getNodeModulesPath = exports.getRootPkg = exports.installRely = exports.debounce = void 0; +exports.logProcess = exports.logError = exports.filterText = exports.getAbsOutputDir = exports.getBuildDir = exports.getDevBuildDir = exports.getPreloadSrc = exports.getMainSrc = exports.getNodeModulesPath = exports.getRootPkg = exports.installRely = exports.debounce = void 0; var path_1 = __importDefault(require("path")); @@ -153,17 +153,17 @@ function getAbsOutputDir(api) { exports.getAbsOutputDir = getAbsOutputDir; -function filterText(s, lineFilter) { +function filterText(s) { var lines = s.trim().split(/\r?\n/).filter(function (it) { - if (lineFilter != null && !lineFilter.filter(it)) { + if (it.includes("Couldn't set selectedTextBackgroundColor from default ()")) { return false; } - if (it.includes("Couldn't set selectedTextBackgroundColor from default ()")) { + if (it.includes("Use NSWindow's -titlebarAppearsTransparent=YES instead.")) { return false; } - if (it.includes("Use NSWindow's -titlebarAppearsTransparent=YES instead.")) { + if (it.includes('Debugger listening on')) { return false; } @@ -177,13 +177,7 @@ function filterText(s, lineFilter) { return ' ' + lines.join("\n ") + '\n'; } -function logProcessErrorOutput(label, childProcess) { - childProcess.stderr.on('data', function (data) { - logProcess(label, data.toString(), chalk_1.default.red); - }); -} - -exports.logProcessErrorOutput = logProcessErrorOutput; +exports.filterText = filterText; function logError(label, error) { logProcess(label, error.stack || error.toString(), chalk_1.default.red); @@ -191,13 +185,8 @@ function logError(label, error) { exports.logError = logError; -function logProcess(label, data, labelColor, lineFilter) { - if (lineFilter === void 0) { - lineFilter = null; - } - +function logProcess(label, log, labelColor) { var LABEL_LENGTH = 28; - var log = filterText(data.toString(), lineFilter); if (log == null || log.length === 0 || log.trim().length === 0) { return; diff --git a/package.json b/package.json index d9409a8..dfb8e87 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umi-plugin-electron-builder", - "version": "2.1.8", + "version": "2.1.9", "description": "Umi plugin for electron-builder", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/compile/index.ts b/src/compile/index.ts index a3b9bdd..7e4eb41 100644 --- a/src/compile/index.ts +++ b/src/compile/index.ts @@ -2,6 +2,7 @@ import { IApi, utils } from 'umi'; import { ChildProcessWithoutNullStreams, spawn } from 'child_process'; import { debounce, + filterText, getDevBuildDir, getMainSrc, getNodeModulesPath, @@ -94,11 +95,18 @@ export const runDev = async (api: IApi) => { '--inspect=5858', path.join(getDevBuildDir(api), 'main.js'), ]); - spawnProcess.stdout.on('data', (d) => logProcess(d.toString(), 'normal')); + spawnProcess.stdout.on('data', (data) => { + const log = filterText(data.toString()); + if (log) { + logProcess(log, 'normal'); + } + }); spawnProcess.stderr.on('data', (data) => { - logProcess(data.toString(), 'error'); + const log = filterText(data.toString()); + if (log) { + logProcess(log, 'error'); + } }); - spawnProcess.on('close', (code, signal) => { if (signal != 'SIGKILL') { process.exit(-1); diff --git a/src/utils/index.ts b/src/utils/index.ts index 1832d9a..1a49ec7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,7 +2,6 @@ import path from 'path'; import * as fse from 'fs-extra'; import { IApi, utils } from 'umi'; import chalk from 'chalk'; -import { ChildProcess } from 'child_process'; import { ElectronBuilder } from '../types'; const { execa } = utils; @@ -123,22 +122,14 @@ export function getAbsOutputDir(api: IApi) { return path.join(process.cwd(), outputDir); } -export interface LineFilter { - filter(line: string): boolean; -} - /** * 过滤electron输出 */ -function filterText(s: string, lineFilter: LineFilter | null) { +export function filterText(s: string) { const lines = s .trim() .split(/\r?\n/) .filter((it) => { - if (lineFilter != null && !lineFilter.filter(it)) { - return false; - } - // https://github.com/electron/electron/issues/4420 // this warning can be safely ignored if ( @@ -151,6 +142,9 @@ function filterText(s: string, lineFilter: LineFilter | null) { ) { return false; } + if (it.includes('Debugger listening on')) { + return false; + } return ( !it.includes( 'Warning: This is an experimental feature and could change at any time.', @@ -166,15 +160,6 @@ function filterText(s: string, lineFilter: LineFilter | null) { return ' ' + lines.join(`\n `) + '\n'; } -export function logProcessErrorOutput( - label: 'Electron' | 'Renderer' | 'Main', - childProcess: ChildProcess, -) { - childProcess.stderr!!.on('data', (data) => { - logProcess(label, data.toString(), chalk.red); - }); -} - export function logError( label: 'Electron' | 'Renderer' | 'Main', error: Error, @@ -184,12 +169,10 @@ export function logError( export function logProcess( label: 'Electron' | 'Renderer' | 'Main', - data: string | Buffer, + log: string, labelColor: any, - lineFilter: LineFilter | null = null, ) { const LABEL_LENGTH = 28; - const log = filterText(data.toString(), lineFilter); if (log == null || log.length === 0 || log.trim().length === 0) { return; }