From ac2e7464be51ea425fc044a08eccf6d4cb3e2d09 Mon Sep 17 00:00:00 2001 From: Adilson Schmitt Junior Date: Fri, 7 Dec 2018 10:38:05 -0200 Subject: [PATCH 1/9] feat(plugin-webpack): add an option to export webpack compilation stats. BREAKING CHANGE: This Feat is opt-in, so probably there is no breaking changes. ISSUES CLOSED: #591 --- packages/plugin/webpack/src/Config.ts | 10 +++++++ packages/plugin/webpack/src/WebpackPlugin.ts | 28 +++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/plugin/webpack/src/Config.ts b/packages/plugin/webpack/src/Config.ts index e95a805ee8..057bb10417 100644 --- a/packages/plugin/webpack/src/Config.ts +++ b/packages/plugin/webpack/src/Config.ts @@ -52,6 +52,11 @@ export interface WebpackPluginRendererConfig { * The webpack config for your renderer process */ config: WebpackConfiguration | string; + /** + * Instructs webpack to emit a JSON file containing statistics about + * modules, the dependency graph and various other build information for the renderer process. + */ + jsonStats?: boolean /** * Array of entry points, these should map to the windows your app needs to * open. Each window requires it's own entry point @@ -64,6 +69,11 @@ export interface WebpackPluginConfig { * The webpack config for your main process */ mainConfig: WebpackConfiguration | string; + /** + * Instructs webpack to emit a JSON file containing statistics about + * modules, the dependency graph and various other build information for the main process. + */ + jsonStats?: boolean /** * Electron Forge webpack configuration for your renderer process */ diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index b3ff92688d..19579a227b 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -8,7 +8,7 @@ import fs from 'fs-extra'; import merge from 'webpack-merge'; import path from 'path'; import { spawnPromise } from 'spawn-rx'; -import webpack, { Configuration } from 'webpack'; +import webpack, { Configuration, Stats } from 'webpack'; import webpackHotMiddleware from 'webpack-hot-middleware'; import webpackDevMiddleware from 'webpack-dev-middleware'; import express from 'express'; @@ -270,14 +270,24 @@ Your packaged app may be larger than expected if you dont ignore everything othe } await asyncOra('Compiling Main Process Code', async () => { await new Promise(async (resolve, reject) => { - const compiler = webpack(await this.getMainConfig()); + const mainConfig = await this.getMainConfig() + const compiler = webpack(mainConfig); const [onceResolve, onceReject] = once(resolve, reject); - const cb: webpack.ICompiler.Handler = (err, stats) => { + const cb: webpack.ICompiler.Handler = async (err, stats: Stats) => { if (tab) { tab.log(stats.toString({ colors: true, })); } + if (this.config.jsonStats) { + const jsonStats = stats.toJson(mainConfig.stats); + const jsonStatsFilename = path.resolve(this.baseDir, 'main', 'stats.json'); + await fs.writeFile( + jsonStatsFilename, + JSON.stringify(jsonStats), + { encoding: 'utf8' } + ); + } if (err) return onceReject(err); onceResolve(); @@ -294,7 +304,17 @@ Your packaged app may be larger than expected if you dont ignore everything othe compileRenderers = async (watch = false) => { await asyncOra('Compiling Renderer Template', async () => { await new Promise(async (resolve, reject) => { - webpack(await this.getRendererConfig(this.config.renderer.entryPoints)).run((err, stats) => { + const rendererConfig = await this.getRendererConfig(this.config.renderer.entryPoints); + webpack(rendererConfig).run(async (err, stats: Stats) => { + if (this.config.renderer.jsonStats) { + const jsonStats = stats.toJson(rendererConfig.stats); + const jsonStatsFilename = path.resolve(this.baseDir, 'renderer', 'stats.json'); + await fs.writeFile( + jsonStatsFilename, + JSON.stringify(jsonStats), + { encoding: 'utf8' } + ); + } if (err) return reject(err); resolve(); }); From 6a9a84c12b8a171e0fa0afd8b3e5b33d4d8ca5f6 Mon Sep 17 00:00:00 2001 From: Adilson Schmitt Junior Date: Fri, 7 Dec 2018 10:38:05 -0200 Subject: [PATCH 2/9] feat(plugin-webpack): add an option to export webpack compilation stats. BREAKING CHANGE: This Feat is opt-in, so probably there is no breaking changes. ISSUES CLOSED: #591 --- packages/plugin/webpack/src/Config.ts | 6 +++--- packages/plugin/webpack/src/WebpackPlugin.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/plugin/webpack/src/Config.ts b/packages/plugin/webpack/src/Config.ts index 057bb10417..c90a957f05 100644 --- a/packages/plugin/webpack/src/Config.ts +++ b/packages/plugin/webpack/src/Config.ts @@ -56,7 +56,7 @@ export interface WebpackPluginRendererConfig { * Instructs webpack to emit a JSON file containing statistics about * modules, the dependency graph and various other build information for the renderer process. */ - jsonStats?: boolean + jsonStats?: boolean; /** * Array of entry points, these should map to the windows your app needs to * open. Each window requires it's own entry point @@ -71,9 +71,9 @@ export interface WebpackPluginConfig { mainConfig: WebpackConfiguration | string; /** * Instructs webpack to emit a JSON file containing statistics about - * modules, the dependency graph and various other build information for the main process. + * modules, the dependency graph and various other build information for the main process. */ - jsonStats?: boolean + jsonStats?: boolean; /** * Electron Forge webpack configuration for your renderer process */ diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 19579a227b..7c028b6a34 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -270,7 +270,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe } await asyncOra('Compiling Main Process Code', async () => { await new Promise(async (resolve, reject) => { - const mainConfig = await this.getMainConfig() + const mainConfig = await this.getMainConfig(); const compiler = webpack(mainConfig); const [onceResolve, onceReject] = once(resolve, reject); const cb: webpack.ICompiler.Handler = async (err, stats: Stats) => { @@ -285,7 +285,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe await fs.writeFile( jsonStatsFilename, JSON.stringify(jsonStats), - { encoding: 'utf8' } + { encoding: 'utf8' }, ); } @@ -312,7 +312,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe await fs.writeFile( jsonStatsFilename, JSON.stringify(jsonStats), - { encoding: 'utf8' } + { encoding: 'utf8' }, ); } if (err) return reject(err); From 1ed8d1e9884eba8f7f0590b9be5cab4230780bfd Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 09:59:06 -0800 Subject: [PATCH 3/9] refactor: add debug lines & simplify code --- packages/plugin/webpack/src/WebpackPlugin.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index b3bd99730f..11fb133e8e 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -97,13 +97,9 @@ export default class WebpackPlugin extends PluginBase { webpack(options) .run(async (err, stats) => { if (isRenderer && this.config.renderer.jsonStats) { - const jsonStats = stats.toJson(options.stats); + d('writing JSON stats for renderers'); const jsonStatsFilename = path.resolve(this.baseDir, 'renderer', 'stats.json'); - await fs.writeFile( - jsonStatsFilename, - JSON.stringify(jsonStats), - { encoding: 'utf8' }, - ); + await fs.writeJson(jsonStatsFilename, stats.toJson(options.stats), { spaces: 2 }); } if (err) { return reject(err); @@ -220,13 +216,9 @@ Your packaged app may be larger than expected if you dont ignore everything othe })); } if (this.config.jsonStats) { - const jsonStats = stats.toJson(mainConfig.stats); + d('writing JSON stats for main'); const jsonStatsFilename = path.resolve(this.baseDir, 'main', 'stats.json'); - await fs.writeFile( - jsonStatsFilename, - JSON.stringify(jsonStats), - { encoding: 'utf8' }, - ); + await fs.writeJson(jsonStatsFilename, stats.toJson(mainConfig.stats), { spaces: 2 }); } if (err) return onceReject(err); From 9ddeb16297c54f3f07130ad3bfb783088f41cf59 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:16:14 -0800 Subject: [PATCH 4/9] refactor: DRY up code --- packages/plugin/webpack/src/WebpackPlugin.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 11fb133e8e..88b1b9c025 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -92,14 +92,18 @@ export default class WebpackPlugin extends PluginBase { if (options.exit) process.exit(); } + async writeJSONStats(type: string, stats: webpack.Stats, statsOptions?: webpack.Stats.ToStringOptions): Promise { + d(`Writing JSON stats for ${type} config`); + const jsonStatsFilename = path.resolve(this.baseDir, type, 'stats.json'); + await fs.writeJson(jsonStatsFilename, stats.toJson(statsOptions as webpack.Stats.ToJsonOptions), { spaces: 2 }); + } + // eslint-disable-next-line max-len private runWebpack = async (options: Configuration, isRenderer = false): Promise => new Promise((resolve, reject) => { webpack(options) .run(async (err, stats) => { if (isRenderer && this.config.renderer.jsonStats) { - d('writing JSON stats for renderers'); - const jsonStatsFilename = path.resolve(this.baseDir, 'renderer', 'stats.json'); - await fs.writeJson(jsonStatsFilename, stats.toJson(options.stats), { spaces: 2 }); + await this.writeJSONStats('renderer', stats, options.stats); } if (err) { return reject(err); @@ -216,9 +220,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe })); } if (this.config.jsonStats) { - d('writing JSON stats for main'); - const jsonStatsFilename = path.resolve(this.baseDir, 'main', 'stats.json'); - await fs.writeJson(jsonStatsFilename, stats.toJson(mainConfig.stats), { spaces: 2 }); + await this.writeJSONStats('main', stats, mainConfig.stats); } if (err) return onceReject(err); From afe5d10abf4c33f00d232b558604129c5455117d Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:16:41 -0800 Subject: [PATCH 5/9] fix: don't accidentally package stats files --- packages/plugin/webpack/src/WebpackPlugin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 88b1b9c025..64b60bda91 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -177,6 +177,14 @@ Your packaged app may be larger than expected if you dont ignore everything othe forgeConfig.packagerConfig.ignore = (file: string) => { if (!file) return false; + if (this.config.jsonStats && file.endsWith(path.join('.webpack', 'main', 'stats.json'))) { + return true; + } + + if (this.config.renderer.jsonStats && file.endsWith(path.join('.webpack', 'renderer', 'stats.json'))) { + return true; + } + return !/^[/\\]\.webpack($|[/\\]).*$/.test(file); }; return forgeConfig; From 5f073390969e877895da53191aa6eadce848457b Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:18:13 -0800 Subject: [PATCH 6/9] Remove unnecessary import --- packages/plugin/webpack/src/WebpackPlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 64b60bda91..d28bf7e400 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -6,7 +6,7 @@ import Logger, { Tab } from '@electron-forge/web-multi-logger'; import debug from 'debug'; import fs from 'fs-extra'; import path from 'path'; -import webpack, { Configuration, Stats } from 'webpack'; +import webpack, { Configuration } from 'webpack'; import webpackHotMiddleware from 'webpack-hot-middleware'; import webpackDevMiddleware from 'webpack-dev-middleware'; import express from 'express'; @@ -221,7 +221,7 @@ Your packaged app may be larger than expected if you dont ignore everything othe await new Promise((resolve, reject) => { const compiler = webpack(mainConfig); const [onceResolve, onceReject] = once(resolve, reject); - const cb: webpack.ICompiler.Handler = async (err, stats: Stats) => { + const cb: webpack.ICompiler.Handler = async (err, stats: webpack.Stats) => { if (tab && stats) { tab.log(stats.toString({ colors: true, From a89408c132b09608c72b884321f16e52ed929fcd Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:20:46 -0800 Subject: [PATCH 7/9] docs: clarify where the files are emitted --- packages/plugin/webpack/src/Config.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/plugin/webpack/src/Config.ts b/packages/plugin/webpack/src/Config.ts index 15c0662285..65fd8a3331 100644 --- a/packages/plugin/webpack/src/Config.ts +++ b/packages/plugin/webpack/src/Config.ts @@ -55,6 +55,7 @@ export interface WebpackPluginRendererConfig { /** * Instructs webpack to emit a JSON file containing statistics about * modules, the dependency graph and various other build information for the renderer process. + * This file is located in `.webpack/renderer/stats.json` and is not packaged with your app. */ jsonStats?: boolean; /** @@ -72,6 +73,7 @@ export interface WebpackPluginConfig { /** * Instructs webpack to emit a JSON file containing statistics about * modules, the dependency graph and various other build information for the main process. + * This file is located in `.webpack/main/stats.json` and is not packaged with your app. */ jsonStats?: boolean; /** From 0768021ad624f4eaa7886c180a0ba09b6d272f9a Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:25:10 -0800 Subject: [PATCH 8/9] fix lint warnings --- packages/plugin/webpack/src/WebpackPlugin.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index d28bf7e400..e73b5f61c3 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -92,10 +92,15 @@ export default class WebpackPlugin extends PluginBase { if (options.exit) process.exit(); } - async writeJSONStats(type: string, stats: webpack.Stats, statsOptions?: webpack.Stats.ToStringOptions): Promise { + async writeJSONStats( + type: string, + stats: webpack.Stats, + statsOptions?: webpack.Stats.ToStringOptions, + ): Promise { d(`Writing JSON stats for ${type} config`); + const jsonStats = stats.toJson(statsOptions as webpack.Stats.ToJsonOptions); const jsonStatsFilename = path.resolve(this.baseDir, type, 'stats.json'); - await fs.writeJson(jsonStatsFilename, stats.toJson(statsOptions as webpack.Stats.ToJsonOptions), { spaces: 2 }); + await fs.writeJson(jsonStatsFilename, jsonStats, { spaces: 2 }); } // eslint-disable-next-line max-len From 75211b832827e8d0dc3f3ad533ae1a79abb127fa Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Thu, 23 Jan 2020 11:34:02 -0800 Subject: [PATCH 9/9] Clarify docs more --- packages/plugin/webpack/src/Config.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/plugin/webpack/src/Config.ts b/packages/plugin/webpack/src/Config.ts index 65fd8a3331..e9fbb32628 100644 --- a/packages/plugin/webpack/src/Config.ts +++ b/packages/plugin/webpack/src/Config.ts @@ -53,9 +53,10 @@ export interface WebpackPluginRendererConfig { */ config: WebpackConfiguration | string; /** - * Instructs webpack to emit a JSON file containing statistics about - * modules, the dependency graph and various other build information for the renderer process. - * This file is located in `.webpack/renderer/stats.json` and is not packaged with your app. + * Instructs webpack to emit a JSON file containing statistics about modules, the dependency + * graph, and various other build information for the renderer process during the app + * packaging process. This file is located in `.webpack/renderer/stats.json`, but is not + * actually packaged with your app. */ jsonStats?: boolean; /** @@ -71,9 +72,9 @@ export interface WebpackPluginConfig { */ mainConfig: WebpackConfiguration | string; /** - * Instructs webpack to emit a JSON file containing statistics about - * modules, the dependency graph and various other build information for the main process. - * This file is located in `.webpack/main/stats.json` and is not packaged with your app. + * Instructs webpack to emit a JSON file containing statistics about modules, the dependency + * graph, and various other build information for the main process. This file is located in + * `.webpack/main/stats.json`, but is not packaged with your app. */ jsonStats?: boolean; /**