From 366bc70020d76e952393b739090caf5c6d7c3642 Mon Sep 17 00:00:00 2001 From: Ryan Albrecht Date: Thu, 2 Nov 2017 21:42:08 +0000 Subject: [PATCH 1/5] Use bfj to write stats json data. --- package.json | 1 + src/BundleAnalyzerPlugin.js | 30 ++++++++++++++++++++---------- test/BundleAnalyzerPlugin.js | 0 yarn.lock | 20 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 test/BundleAnalyzerPlugin.js diff --git a/package.json b/package.json index 1e5b7778..d9293669 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ ], "dependencies": { "acorn": "^5.1.1", + "bfj": "^4.2.4", "chalk": "^1.1.3", "commander": "^2.9.0", "ejs": "^2.5.6", diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index a81d14cf..a183b545 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const bfj = require('bfj'); const path = require('path'); const mkdir = require('mkdirp'); const { bold } = require('chalk'); @@ -63,17 +63,27 @@ class BundleAnalyzerPlugin { generateStatsFile(stats) { const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename); - mkdir.sync(path.dirname(statsFilepath)); - fs.writeFileSync( - statsFilepath, - JSON.stringify(stats, null, 2) - ); - - this.logger.info( - `${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}` - ); + const options = { + promises: 'ignore', + buffers: 'ignore', + maps: 'ignore', + iterables: 'ignore', + circular: 'ignore' + }; + return bfj.write(statsFilepath, stats, options) + .then(() => { + this.logger.info( + `${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}` + ); + }) + .catch((error) => { + this.logger.error( + `${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}.`, + JSON.stringify(error, null, "\n") // eslint-disable-line quotes + ); + }); } async startAnalyzerServer(stats) { diff --git a/test/BundleAnalyzerPlugin.js b/test/BundleAnalyzerPlugin.js new file mode 100644 index 00000000..e69de29b diff --git a/yarn.lock b/yarn.lock index 62442b46..e0efc4d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -755,6 +755,14 @@ beeper@^1.0.0: version "1.1.1" resolved "https://registry.npmjs.org/beeper/-/beeper-1.1.1.tgz#e6d5ea8c5dad001304a70b22638447f69cb2f809" +bfj@^4.2.4: + version "4.2.4" + resolved "https://registry.yarnpkg.com/bfj/-/bfj-4.2.4.tgz#85f7b23683c2afdc15860384a2d1c3fac80ed33a" + dependencies: + check-types "^7.3.0" + hoopy "^0.1.2" + tryer "^1.0.0" + big.js@^3.1.3: version "3.1.3" resolved "https://registry.npmjs.org/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978" @@ -963,6 +971,10 @@ chalk@^1.0.0, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" +check-types@^7.3.0: + version "7.3.0" + resolved "https://registry.yarnpkg.com/check-types/-/check-types-7.3.0.tgz#468f571a4435c24248f5fd0cb0e8d87c3c341e7d" + chokidar@^1.4.3, chokidar@^1.6.0, chokidar@^1.6.1: version "1.7.0" resolved "https://registry.npmjs.org/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" @@ -2458,6 +2470,10 @@ homedir-polyfill@^1.0.0: dependencies: parse-passwd "^1.0.0" +hoopy@^0.1.2: + version "0.1.2" + resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.2.tgz#bd64d4648eb13d77971a129a493fbb380b030602" + hosted-git-info@^2.1.4: version "2.4.2" resolved "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.4.2.tgz#0076b9f46a270506ddbaaea56496897460612a67" @@ -4882,6 +4898,10 @@ trim-right@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" +tryer@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/tryer/-/tryer-1.0.0.tgz#027b69fa823225e551cace3ef03b11f6ab37c1d7" + tryit@^1.0.1: version "1.0.3" resolved "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb" From 846999ae9833af36ea246422d389838a7ca53c06 Mon Sep 17 00:00:00 2001 From: Ryan Albrecht Date: Sun, 5 Nov 2017 09:10:01 -0800 Subject: [PATCH 2/5] Use bfj-node4 instead of bfj for support in v2.x --- package.json | 2 +- src/BundleAnalyzerPlugin.js | 2 +- yarn.lock | 9 ++------- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index d9293669..0460b005 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ ], "dependencies": { "acorn": "^5.1.1", - "bfj": "^4.2.4", + "bfj-node4": "^4.2.4", "chalk": "^1.1.3", "commander": "^2.9.0", "ejs": "^2.5.6", diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index a183b545..41914899 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -1,4 +1,4 @@ -const bfj = require('bfj'); +const bfj = require('bfj-node4'); const path = require('path'); const mkdir = require('mkdirp'); const { bold } = require('chalk'); diff --git a/yarn.lock b/yarn.lock index e0efc4d6..952d70d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -755,12 +755,11 @@ beeper@^1.0.0: version "1.1.1" resolved "https://registry.npmjs.org/beeper/-/beeper-1.1.1.tgz#e6d5ea8c5dad001304a70b22638447f69cb2f809" -bfj@^4.2.4: +bfj-node4@^4.2.4: version "4.2.4" - resolved "https://registry.yarnpkg.com/bfj/-/bfj-4.2.4.tgz#85f7b23683c2afdc15860384a2d1c3fac80ed33a" + resolved "https://registry.yarnpkg.com/bfj-node4/-/bfj-node4-4.2.4.tgz#8484fe2ec73ea195906f3fd26ebd309be233e91a" dependencies: check-types "^7.3.0" - hoopy "^0.1.2" tryer "^1.0.0" big.js@^3.1.3: @@ -2470,10 +2469,6 @@ homedir-polyfill@^1.0.0: dependencies: parse-passwd "^1.0.0" -hoopy@^0.1.2: - version "0.1.2" - resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.2.tgz#bd64d4648eb13d77971a129a493fbb380b030602" - hosted-git-info@^2.1.4: version "2.4.2" resolved "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.4.2.tgz#0076b9f46a270506ddbaaea56496897460612a67" From a08fbb566cbb71b179d18f4c9e6dcbfab432199d Mon Sep 17 00:00:00 2001 From: Ryan Albrecht Date: Mon, 6 Nov 2017 22:29:38 +0000 Subject: [PATCH 3/5] Remove bonus file --- test/BundleAnalyzerPlugin.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/BundleAnalyzerPlugin.js diff --git a/test/BundleAnalyzerPlugin.js b/test/BundleAnalyzerPlugin.js deleted file mode 100644 index e69de29b..00000000 From abda6c7d739be405d5ba664b3629e5083b181a57 Mon Sep 17 00:00:00 2001 From: Ryan Albrecht Date: Tue, 7 Nov 2017 00:16:36 -0800 Subject: [PATCH 4/5] Drop double-quotes and indent json with tab --- src/BundleAnalyzerPlugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 41914899..59e16f28 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -81,7 +81,7 @@ class BundleAnalyzerPlugin { .catch((error) => { this.logger.error( `${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}.`, - JSON.stringify(error, null, "\n") // eslint-disable-line quotes + JSON.stringify(error, null, '\t') ); }); } From b5254aa11643efe506bbb76ed873c04d9d5bd279 Mon Sep 17 00:00:00 2001 From: Ryan Albrecht Date: Mon, 13 Nov 2017 23:55:18 -0800 Subject: [PATCH 5/5] Use await, nicer error and inline options --- src/BundleAnalyzerPlugin.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/BundleAnalyzerPlugin.js b/src/BundleAnalyzerPlugin.js index 59e16f28..f05c41cb 100644 --- a/src/BundleAnalyzerPlugin.js +++ b/src/BundleAnalyzerPlugin.js @@ -61,29 +61,27 @@ class BundleAnalyzerPlugin { }); } - generateStatsFile(stats) { + async generateStatsFile(stats) { const statsFilepath = path.resolve(this.compiler.outputPath, this.opts.statsFilename); mkdir.sync(path.dirname(statsFilepath)); - const options = { - promises: 'ignore', - buffers: 'ignore', - maps: 'ignore', - iterables: 'ignore', - circular: 'ignore' - }; - return bfj.write(statsFilepath, stats, options) - .then(() => { - this.logger.info( - `${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}` - ); - }) - .catch((error) => { - this.logger.error( - `${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}.`, - JSON.stringify(error, null, '\t') - ); + try { + await bfj.write(statsFilepath, stats, { + promises: 'ignore', + buffers: 'ignore', + maps: 'ignore', + iterables: 'ignore', + circular: 'ignore' }); + + this.logger.info( + `${bold('Webpack Bundle Analyzer')} saved stats file to ${bold(statsFilepath)}` + ); + } catch (error) { + this.logger.error( + `${bold('Webpack Bundle Analyzer')} error saving stats file to ${bold(statsFilepath)}: ${error}` + ); + } } async startAnalyzerServer(stats) {