From f4fbdcc6bfac7dae5b1d997744204e950b10b7eb Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:10:21 +0700 Subject: [PATCH 01/21] extract generic build functions to react-dev-utils --- .../react-dev-utils/getDifferenceLabel.js | 19 +++++ packages/react-dev-utils/package.json | 17 ++--- .../react-dev-utils/paths/copyPublicFolder.js | 7 ++ .../react-dev-utils/paths/printFileSizes.js | 47 +++++++++++++ .../paths/removeFileNameHash.js | 7 ++ packages/react-scripts/scripts/build.js | 70 ++----------------- 6 files changed, 88 insertions(+), 79 deletions(-) create mode 100755 packages/react-dev-utils/getDifferenceLabel.js create mode 100755 packages/react-dev-utils/paths/copyPublicFolder.js create mode 100644 packages/react-dev-utils/paths/printFileSizes.js create mode 100755 packages/react-dev-utils/paths/removeFileNameHash.js diff --git a/packages/react-dev-utils/getDifferenceLabel.js b/packages/react-dev-utils/getDifferenceLabel.js new file mode 100755 index 00000000000..eb7ab52f1dd --- /dev/null +++ b/packages/react-dev-utils/getDifferenceLabel.js @@ -0,0 +1,19 @@ +var chalk = require('chalk'); +var filesize = require('filesize'); + +// Input: 1024, 2048 +// Output: "(+1 KB)" +module.exports = function getDifferenceLabel(currentSize, previousSize) { + var FIFTY_KILOBYTES = 1024 * 50; + var difference = currentSize - previousSize; + var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0; + if (difference >= FIFTY_KILOBYTES) { + return chalk.red('+' + fileSize); + } else if (difference < FIFTY_KILOBYTES && difference > 0) { + return chalk.yellow('+' + fileSize); + } else if (difference < 0) { + return chalk.green(fileSize); + } else { + return ''; + } +}; diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index aae3b83ff01..6994005a472 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -10,25 +10,16 @@ "engines": { "node": ">=4" }, - "files": [ - "clearConsole.js", - "checkRequiredFiles.js", - "formatWebpackMessages.js", - "getProcessForPort.js", - "InterpolateHtmlPlugin.js", - "openChrome.applescript", - "openBrowser.js", - "prompt.js", - "WatchMissingNodeModulesPlugin.js", - "webpackHotDevClient.js" - ], + "files": ["clearConsole.js", "checkRequiredFiles.js", "formatWebpackMessages.js", "getProcessForPort.js", "InterpolateHtmlPlugin.js", "openChrome.applescript", "openBrowser.js", "prompt.js", "WatchMissingNodeModulesPlugin.js", "webpackHotDevClient.js"], "dependencies": { "ansi-html": "0.0.5", "chalk": "1.1.3", "escape-string-regexp": "1.0.5", + "filesize": "3.3.0", + "gzip-size": "^3.0.0", "html-entities": "1.2.0", "opn": "4.0.2", "sockjs-client": "1.1.2", "strip-ansi": "3.0.1" } -} +} \ No newline at end of file diff --git a/packages/react-dev-utils/paths/copyPublicFolder.js b/packages/react-dev-utils/paths/copyPublicFolder.js new file mode 100755 index 00000000000..84bf301f1d5 --- /dev/null +++ b/packages/react-dev-utils/paths/copyPublicFolder.js @@ -0,0 +1,7 @@ +var fs = require('fs-extra'); +module.exports = paths => function copyPublicFolder() { + fs.copySync(paths.appPublic, paths.appBuild, { + dereference: true, + filter: file => file !== paths.appHtml + }); +}; diff --git a/packages/react-dev-utils/paths/printFileSizes.js b/packages/react-dev-utils/paths/printFileSizes.js new file mode 100644 index 00000000000..a49c9dc0ca4 --- /dev/null +++ b/packages/react-dev-utils/paths/printFileSizes.js @@ -0,0 +1,47 @@ +var fs = require('fs'); +var path = require('path'); +var chalk = require('chalk'); +var filesize = require('filesize'); +var stripAnsi = require('strip-ansi'); +var gzipSize = require('gzip-size').sync; + +// Print a detailed summary of build files. +module.exports = paths => function printFileSizes(stats, previousSizeMap) { + var removeFileNameHash = require('./removeFileNameHash')(paths); + var getDifferenceLabel = require('../getDifferenceLabel'); + var assets = stats + .toJson() + .assets.filter(asset => /\.(js|css)$/.test(asset.name)) + .map(asset => { + var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name); + var size = gzipSize(fileContents); + var previousSize = previousSizeMap[removeFileNameHash(asset.name)]; + var difference = getDifferenceLabel(size, previousSize); + return { + folder: path.join('build', path.dirname(asset.name)), + name: path.basename(asset.name), + size: size, + sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : '') + }; + }); + assets.sort((a, b) => b.size - a.size); + var longestSizeLabelLength = Math.max.apply( + null, + assets.map(a => stripAnsi(a.sizeLabel).length) + ); + assets.forEach(asset => { + var sizeLabel = asset.sizeLabel; + var sizeLength = stripAnsi(sizeLabel).length; + if (sizeLength < longestSizeLabelLength) { + var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength); + sizeLabel += rightPadding; + } + console.log( + ' ' + + sizeLabel + + ' ' + + chalk.dim(asset.folder + path.sep) + + chalk.cyan(asset.name) + ); + }); +}; \ No newline at end of file diff --git a/packages/react-dev-utils/paths/removeFileNameHash.js b/packages/react-dev-utils/paths/removeFileNameHash.js new file mode 100755 index 00000000000..0df934124bb --- /dev/null +++ b/packages/react-dev-utils/paths/removeFileNameHash.js @@ -0,0 +1,7 @@ +// Input: /User/dan/app/build/static/js/main.82be8.js +// Output: /static/js/main.js +module.exports = paths => function removeFileNameHash(fileName) { + return fileName + .replace(paths.appBuild, '') + .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); +}; diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 6892fcd6aff..8da6949c62b 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -28,6 +28,10 @@ var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); +var copyPublicFolder = require('react-dev-utils/paths/copyPublicFolder')(paths); +var printFileSizes = require('react-dev-utils/paths/printFileSizes')(paths); +var removeFileNameHash = require('react-dev-utils/paths/removeFileNameHash')(paths); +var getDifferenceLabel = require('react-dev-utils/getDifferenceLabel'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); @@ -38,31 +42,6 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { process.exit(1); } -// Input: /User/dan/app/build/static/js/main.82be8.js -// Output: /static/js/main.js -function removeFileNameHash(fileName) { - return fileName - .replace(paths.appBuild, '') - .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); -} - -// Input: 1024, 2048 -// Output: "(+1 KB)" -function getDifferenceLabel(currentSize, previousSize) { - var FIFTY_KILOBYTES = 1024 * 50; - var difference = currentSize - previousSize; - var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0; - if (difference >= FIFTY_KILOBYTES) { - return chalk.red('+' + fileSize); - } else if (difference < FIFTY_KILOBYTES && difference > 0) { - return chalk.yellow('+' + fileSize); - } else if (difference < 0) { - return chalk.green(fileSize); - } else { - return ''; - } -} - // First, read the current file sizes in build directory. // This lets us display how much they changed later. recursive(paths.appBuild, (err, fileNames) => { @@ -86,40 +65,6 @@ recursive(paths.appBuild, (err, fileNames) => { copyPublicFolder(); }); -// Print a detailed summary of build files. -function printFileSizes(stats, previousSizeMap) { - var assets = stats.toJson().assets - .filter(asset => /\.(js|css)$/.test(asset.name)) - .map(asset => { - var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name); - var size = gzipSize(fileContents); - var previousSize = previousSizeMap[removeFileNameHash(asset.name)]; - var difference = getDifferenceLabel(size, previousSize); - return { - folder: path.join('build', path.dirname(asset.name)), - name: path.basename(asset.name), - size: size, - sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : '') - }; - }); - assets.sort((a, b) => b.size - a.size); - var longestSizeLabelLength = Math.max.apply(null, - assets.map(a => stripAnsi(a.sizeLabel).length) - ); - assets.forEach(asset => { - var sizeLabel = asset.sizeLabel; - var sizeLength = stripAnsi(sizeLabel).length; - if (sizeLength < longestSizeLabelLength) { - var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength); - sizeLabel += rightPadding; - } - console.log( - ' ' + sizeLabel + - ' ' + chalk.dim(asset.folder + path.sep) + chalk.cyan(asset.name) - ); - }); -} - // Print out errors function printErrors(summary, errors) { console.log(chalk.red(summary)); @@ -238,10 +183,3 @@ function build(previousSizeMap) { } }); } - -function copyPublicFolder() { - fs.copySync(paths.appPublic, paths.appBuild, { - dereference: true, - filter: file => file !== paths.appHtml - }); -} From dc2e108c633d38539134481c9b5edd8446e6363e Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:15:36 +0700 Subject: [PATCH 02/21] tweak package json files and move removeFileNameHash --- packages/react-dev-utils/package.json | 16 +++++++++++++++- packages/react-dev-utils/paths/printFileSizes.js | 9 ++++++++- .../react-dev-utils/paths/removeFileNameHash.js | 7 ------- 3 files changed, 23 insertions(+), 9 deletions(-) delete mode 100755 packages/react-dev-utils/paths/removeFileNameHash.js diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 6994005a472..338c981c0c3 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -10,7 +10,21 @@ "engines": { "node": ">=4" }, - "files": ["clearConsole.js", "checkRequiredFiles.js", "formatWebpackMessages.js", "getProcessForPort.js", "InterpolateHtmlPlugin.js", "openChrome.applescript", "openBrowser.js", "prompt.js", "WatchMissingNodeModulesPlugin.js", "webpackHotDevClient.js"], + "files": [ + "paths/copyPublicFolder.js", + "paths/printFileSizes.js", + "clearConsole.js", + "checkRequiredFiles.js", + "formatWebpackMessages.js", + "getProcessForPort.js", + "getDifferenceLabel.js", + "InterpolateHtmlPlugin.js", + "openChrome.applescript", + "openBrowser.js", + "prompt.js", + "WatchMissingNodeModulesPlugin.js", + "webpackHotDevClient.js" + ], "dependencies": { "ansi-html": "0.0.5", "chalk": "1.1.3", diff --git a/packages/react-dev-utils/paths/printFileSizes.js b/packages/react-dev-utils/paths/printFileSizes.js index a49c9dc0ca4..f2b81174047 100644 --- a/packages/react-dev-utils/paths/printFileSizes.js +++ b/packages/react-dev-utils/paths/printFileSizes.js @@ -7,6 +7,11 @@ var gzipSize = require('gzip-size').sync; // Print a detailed summary of build files. module.exports = paths => function printFileSizes(stats, previousSizeMap) { + function removeFileNameHash(fileName) { + return fileName + .replace(paths.appBuild, '') + .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); + }; var removeFileNameHash = require('./removeFileNameHash')(paths); var getDifferenceLabel = require('../getDifferenceLabel'); var assets = stats @@ -44,4 +49,6 @@ module.exports = paths => function printFileSizes(stats, previousSizeMap) { chalk.cyan(asset.name) ); }); -}; \ No newline at end of file +}; + + diff --git a/packages/react-dev-utils/paths/removeFileNameHash.js b/packages/react-dev-utils/paths/removeFileNameHash.js deleted file mode 100755 index 0df934124bb..00000000000 --- a/packages/react-dev-utils/paths/removeFileNameHash.js +++ /dev/null @@ -1,7 +0,0 @@ -// Input: /User/dan/app/build/static/js/main.82be8.js -// Output: /static/js/main.js -module.exports = paths => function removeFileNameHash(fileName) { - return fileName - .replace(paths.appBuild, '') - .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); -}; From d501eceb619ddb58ae2d998cc8769b773d3f0ff3 Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:20:34 +0700 Subject: [PATCH 03/21] revert removeFileNameHash --- packages/react-dev-utils/paths/printFileSizes.js | 5 ----- packages/react-dev-utils/paths/removeFileNameHash.js | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 packages/react-dev-utils/paths/removeFileNameHash.js diff --git a/packages/react-dev-utils/paths/printFileSizes.js b/packages/react-dev-utils/paths/printFileSizes.js index f2b81174047..e28a8d2c9be 100644 --- a/packages/react-dev-utils/paths/printFileSizes.js +++ b/packages/react-dev-utils/paths/printFileSizes.js @@ -7,11 +7,6 @@ var gzipSize = require('gzip-size').sync; // Print a detailed summary of build files. module.exports = paths => function printFileSizes(stats, previousSizeMap) { - function removeFileNameHash(fileName) { - return fileName - .replace(paths.appBuild, '') - .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); - }; var removeFileNameHash = require('./removeFileNameHash')(paths); var getDifferenceLabel = require('../getDifferenceLabel'); var assets = stats diff --git a/packages/react-dev-utils/paths/removeFileNameHash.js b/packages/react-dev-utils/paths/removeFileNameHash.js new file mode 100644 index 00000000000..bd3d6090255 --- /dev/null +++ b/packages/react-dev-utils/paths/removeFileNameHash.js @@ -0,0 +1,5 @@ +module.exports = paths => function removeFileNameHash(fileName) { + return fileName + .replace(paths.appBuild, '') + .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); +}; \ No newline at end of file From 39cfc67e69940514e8a67eb0784a293ada74dc10 Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:24:00 +0700 Subject: [PATCH 04/21] use paths.appBuild in printFileSizes --- packages/react-dev-utils/paths/printFileSizes.js | 6 +++--- packages/react-scripts/scripts/build.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/react-dev-utils/paths/printFileSizes.js b/packages/react-dev-utils/paths/printFileSizes.js index e28a8d2c9be..e7d7eee935a 100644 --- a/packages/react-dev-utils/paths/printFileSizes.js +++ b/packages/react-dev-utils/paths/printFileSizes.js @@ -6,14 +6,14 @@ var stripAnsi = require('strip-ansi'); var gzipSize = require('gzip-size').sync; // Print a detailed summary of build files. -module.exports = paths => function printFileSizes(stats, previousSizeMap) { - var removeFileNameHash = require('./removeFileNameHash')(paths); +module.exports = appBuild => function printFileSizes(stats, previousSizeMap) { + var removeFileNameHash = require('./removeFileNameHash')(appBuild); var getDifferenceLabel = require('../getDifferenceLabel'); var assets = stats .toJson() .assets.filter(asset => /\.(js|css)$/.test(asset.name)) .map(asset => { - var fileContents = fs.readFileSync(paths.appBuild + '/' + asset.name); + var fileContents = fs.readFileSync(appBuild + '/' + asset.name); var size = gzipSize(fileContents); var previousSize = previousSizeMap[removeFileNameHash(asset.name)]; var difference = getDifferenceLabel(size, previousSize); diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 8da6949c62b..85653cbdc12 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -29,7 +29,7 @@ var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); var copyPublicFolder = require('react-dev-utils/paths/copyPublicFolder')(paths); -var printFileSizes = require('react-dev-utils/paths/printFileSizes')(paths); +var printFileSizes = require('react-dev-utils/paths/printFileSizes')(paths.appBuild); var removeFileNameHash = require('react-dev-utils/paths/removeFileNameHash')(paths); var getDifferenceLabel = require('react-dev-utils/getDifferenceLabel'); var recursive = require('recursive-readdir'); From 8cc0799cf506e3d364750a6f3d1329311c0bcee9 Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:27:56 +0700 Subject: [PATCH 05/21] use paths.appBuild in removeFileNameHash --- packages/react-dev-utils/paths/removeFileNameHash.js | 4 ++-- packages/react-scripts/scripts/build.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/react-dev-utils/paths/removeFileNameHash.js b/packages/react-dev-utils/paths/removeFileNameHash.js index bd3d6090255..5dd2e4650c8 100644 --- a/packages/react-dev-utils/paths/removeFileNameHash.js +++ b/packages/react-dev-utils/paths/removeFileNameHash.js @@ -1,5 +1,5 @@ -module.exports = paths => function removeFileNameHash(fileName) { +module.exports = appBuild => function removeFileNameHash(fileName) { return fileName - .replace(paths.appBuild, '') + .replace(appBuild, '') .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); }; \ No newline at end of file diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 85653cbdc12..0a33ee58e58 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -30,7 +30,7 @@ var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); var copyPublicFolder = require('react-dev-utils/paths/copyPublicFolder')(paths); var printFileSizes = require('react-dev-utils/paths/printFileSizes')(paths.appBuild); -var removeFileNameHash = require('react-dev-utils/paths/removeFileNameHash')(paths); +var removeFileNameHash = require('react-dev-utils/paths/removeFileNameHash')(paths.appBuild); var getDifferenceLabel = require('react-dev-utils/getDifferenceLabel'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); From 7646b292ac009fe94e616b6433701a9fe9c793c3 Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:40:58 +0700 Subject: [PATCH 06/21] change curried functions to regular functions --- packages/react-dev-utils/copyPublicFolder.js | 7 +++++++ packages/react-dev-utils/package.json | 13 +++++++------ .../react-dev-utils/paths/copyPublicFolder.js | 7 ------- .../{paths => }/printFileSizes.js | 9 ++++----- .../{paths => }/removeFileNameHash.js | 2 +- packages/react-scripts/scripts/build.js | 16 ++++++++++------ 6 files changed, 29 insertions(+), 25 deletions(-) create mode 100755 packages/react-dev-utils/copyPublicFolder.js delete mode 100755 packages/react-dev-utils/paths/copyPublicFolder.js rename packages/react-dev-utils/{paths => }/printFileSizes.js (82%) rename packages/react-dev-utils/{paths => }/removeFileNameHash.js (64%) diff --git a/packages/react-dev-utils/copyPublicFolder.js b/packages/react-dev-utils/copyPublicFolder.js new file mode 100755 index 00000000000..d2c39a2d9f2 --- /dev/null +++ b/packages/react-dev-utils/copyPublicFolder.js @@ -0,0 +1,7 @@ +var fs = require('fs-extra'); +module.exports = function copyPublicFolder(appPublic, appBuild, appHtml) { + fs.copySync(appPublic, appBuild, { + dereference: true, + filter: file => file !== appHtml + }); +}; diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 338c981c0c3..f52c06f15c7 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -11,17 +11,18 @@ "node": ">=4" }, "files": [ - "paths/copyPublicFolder.js", - "paths/printFileSizes.js", - "clearConsole.js", + "copyPublicFolder.js", "checkRequiredFiles.js", + "clearConsole.js", "formatWebpackMessages.js", - "getProcessForPort.js", "getDifferenceLabel.js", + "getProcessForPort.js", "InterpolateHtmlPlugin.js", - "openChrome.applescript", "openBrowser.js", - "prompt.js", + "openChrome.applescript", + "printFileSizes.js", + "prompt.js", + "removeFileNameHash.js", "WatchMissingNodeModulesPlugin.js", "webpackHotDevClient.js" ], diff --git a/packages/react-dev-utils/paths/copyPublicFolder.js b/packages/react-dev-utils/paths/copyPublicFolder.js deleted file mode 100755 index 84bf301f1d5..00000000000 --- a/packages/react-dev-utils/paths/copyPublicFolder.js +++ /dev/null @@ -1,7 +0,0 @@ -var fs = require('fs-extra'); -module.exports = paths => function copyPublicFolder() { - fs.copySync(paths.appPublic, paths.appBuild, { - dereference: true, - filter: file => file !== paths.appHtml - }); -}; diff --git a/packages/react-dev-utils/paths/printFileSizes.js b/packages/react-dev-utils/printFileSizes.js similarity index 82% rename from packages/react-dev-utils/paths/printFileSizes.js rename to packages/react-dev-utils/printFileSizes.js index e7d7eee935a..727276aed53 100644 --- a/packages/react-dev-utils/paths/printFileSizes.js +++ b/packages/react-dev-utils/printFileSizes.js @@ -4,18 +4,17 @@ var chalk = require('chalk'); var filesize = require('filesize'); var stripAnsi = require('strip-ansi'); var gzipSize = require('gzip-size').sync; - +var removeFileNameHash = require('./removeFileNameHash'); +var getDifferenceLabel = require('./getDifferenceLabel'); // Print a detailed summary of build files. -module.exports = appBuild => function printFileSizes(stats, previousSizeMap) { - var removeFileNameHash = require('./removeFileNameHash')(appBuild); - var getDifferenceLabel = require('../getDifferenceLabel'); +module.exports = function printFileSizes(appBuild, stats, previousSizeMap) { var assets = stats .toJson() .assets.filter(asset => /\.(js|css)$/.test(asset.name)) .map(asset => { var fileContents = fs.readFileSync(appBuild + '/' + asset.name); var size = gzipSize(fileContents); - var previousSize = previousSizeMap[removeFileNameHash(asset.name)]; + var previousSize = previousSizeMap[removeFileNameHash(appBuild, asset.name)]; var difference = getDifferenceLabel(size, previousSize); return { folder: path.join('build', path.dirname(asset.name)), diff --git a/packages/react-dev-utils/paths/removeFileNameHash.js b/packages/react-dev-utils/removeFileNameHash.js similarity index 64% rename from packages/react-dev-utils/paths/removeFileNameHash.js rename to packages/react-dev-utils/removeFileNameHash.js index 5dd2e4650c8..18b56ef1770 100644 --- a/packages/react-dev-utils/paths/removeFileNameHash.js +++ b/packages/react-dev-utils/removeFileNameHash.js @@ -1,4 +1,4 @@ -module.exports = appBuild => function removeFileNameHash(fileName) { +module.exports = function removeFileNameHash(appBuild, fileName) { return fileName .replace(appBuild, '') .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 0a33ee58e58..ab77d4ff5e7 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -28,9 +28,9 @@ var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); -var copyPublicFolder = require('react-dev-utils/paths/copyPublicFolder')(paths); -var printFileSizes = require('react-dev-utils/paths/printFileSizes')(paths.appBuild); -var removeFileNameHash = require('react-dev-utils/paths/removeFileNameHash')(paths.appBuild); +var copyPublicFolder = require('react-dev-utils/copyPublicFolder'); +var printFileSizes = require('react-dev-utils/printFileSizes'); +var removeFileNameHash = require('react-dev-utils/removeFileNameHash'); var getDifferenceLabel = require('react-dev-utils/getDifferenceLabel'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); @@ -49,7 +49,7 @@ recursive(paths.appBuild, (err, fileNames) => { .filter(fileName => /\.(js|css)$/.test(fileName)) .reduce((memo, fileName) => { var contents = fs.readFileSync(fileName); - var key = removeFileNameHash(fileName); + var key = removeFileNameHash(paths.appBuild, fileName); memo[key] = gzipSize(contents); return memo; }, {}); @@ -62,7 +62,11 @@ recursive(paths.appBuild, (err, fileNames) => { build(previousSizeMap); // Merge with the public folder - copyPublicFolder(); + copyPublicFolder( + paths.appPublic, + paths.appBuild, + paths.appHtml + ); }); // Print out errors @@ -108,7 +112,7 @@ function build(previousSizeMap) { console.log('File sizes after gzip:'); console.log(); - printFileSizes(stats, previousSizeMap); + printFileSizes(paths.appBuild, stats, previousSizeMap); console.log(); var openCommand = process.platform === 'win32' ? 'start' : 'open'; From 03140f4d2aae0c008cb108dd8a03d38d52f376fb Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:45:18 +0700 Subject: [PATCH 07/21] add fs-extra to react-dev-utils deps --- packages/react-dev-utils/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index f52c06f15c7..14d1ef91fc2 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -33,6 +33,7 @@ "filesize": "3.3.0", "gzip-size": "^3.0.0", "html-entities": "1.2.0", + "fs-extra": "0.30.0", "opn": "4.0.2", "sockjs-client": "1.1.2", "strip-ansi": "3.0.1" From 79b4f98a2bbd959a6178144927e56647823c772f Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:54:55 +0700 Subject: [PATCH 08/21] move getDifferenceLabel inside printFileSizes --- .../react-dev-utils/getDifferenceLabel.js | 19 -------------- packages/react-dev-utils/printFileSizes.js | 25 +++++++++++++++++-- packages/react-scripts/scripts/build.js | 1 - 3 files changed, 23 insertions(+), 22 deletions(-) delete mode 100755 packages/react-dev-utils/getDifferenceLabel.js diff --git a/packages/react-dev-utils/getDifferenceLabel.js b/packages/react-dev-utils/getDifferenceLabel.js deleted file mode 100755 index eb7ab52f1dd..00000000000 --- a/packages/react-dev-utils/getDifferenceLabel.js +++ /dev/null @@ -1,19 +0,0 @@ -var chalk = require('chalk'); -var filesize = require('filesize'); - -// Input: 1024, 2048 -// Output: "(+1 KB)" -module.exports = function getDifferenceLabel(currentSize, previousSize) { - var FIFTY_KILOBYTES = 1024 * 50; - var difference = currentSize - previousSize; - var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0; - if (difference >= FIFTY_KILOBYTES) { - return chalk.red('+' + fileSize); - } else if (difference < FIFTY_KILOBYTES && difference > 0) { - return chalk.yellow('+' + fileSize); - } else if (difference < 0) { - return chalk.green(fileSize); - } else { - return ''; - } -}; diff --git a/packages/react-dev-utils/printFileSizes.js b/packages/react-dev-utils/printFileSizes.js index 727276aed53..1719c83f109 100644 --- a/packages/react-dev-utils/printFileSizes.js +++ b/packages/react-dev-utils/printFileSizes.js @@ -4,8 +4,6 @@ var chalk = require('chalk'); var filesize = require('filesize'); var stripAnsi = require('strip-ansi'); var gzipSize = require('gzip-size').sync; -var removeFileNameHash = require('./removeFileNameHash'); -var getDifferenceLabel = require('./getDifferenceLabel'); // Print a detailed summary of build files. module.exports = function printFileSizes(appBuild, stats, previousSizeMap) { var assets = stats @@ -45,4 +43,27 @@ module.exports = function printFileSizes(appBuild, stats, previousSizeMap) { }); }; +function removeFileNameHash(appBuild, fileName) { + return fileName + .replace(appBuild, '') + .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); +}; + +// Input: 1024, 2048 +// Output: "(+1 KB)" +function getDifferenceLabel(currentSize, previousSize) { + var FIFTY_KILOBYTES = 1024 * 50; + var difference = currentSize - previousSize; + var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0; + if (difference >= FIFTY_KILOBYTES) { + return chalk.red('+' + fileSize); + } else if (difference < FIFTY_KILOBYTES && difference > 0) { + return chalk.yellow('+' + fileSize); + } else if (difference < 0) { + return chalk.green(fileSize); + } else { + return ''; + } +}; + diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index ab77d4ff5e7..e8bca2173fb 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -31,7 +31,6 @@ var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); var copyPublicFolder = require('react-dev-utils/copyPublicFolder'); var printFileSizes = require('react-dev-utils/printFileSizes'); var removeFileNameHash = require('react-dev-utils/removeFileNameHash'); -var getDifferenceLabel = require('react-dev-utils/getDifferenceLabel'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); From 6eb9a3413bd3584f1daaf6a670318b5d944ed7ce Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 04:58:33 +0700 Subject: [PATCH 09/21] inline copyPublicFolder --- packages/react-dev-utils/copyPublicFolder.js | 7 ------- packages/react-dev-utils/package.json | 3 --- packages/react-scripts/scripts/build.js | 9 ++++++++- 3 files changed, 8 insertions(+), 11 deletions(-) delete mode 100755 packages/react-dev-utils/copyPublicFolder.js diff --git a/packages/react-dev-utils/copyPublicFolder.js b/packages/react-dev-utils/copyPublicFolder.js deleted file mode 100755 index d2c39a2d9f2..00000000000 --- a/packages/react-dev-utils/copyPublicFolder.js +++ /dev/null @@ -1,7 +0,0 @@ -var fs = require('fs-extra'); -module.exports = function copyPublicFolder(appPublic, appBuild, appHtml) { - fs.copySync(appPublic, appBuild, { - dereference: true, - filter: file => file !== appHtml - }); -}; diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 14d1ef91fc2..147ecf7bd16 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -11,11 +11,9 @@ "node": ">=4" }, "files": [ - "copyPublicFolder.js", "checkRequiredFiles.js", "clearConsole.js", "formatWebpackMessages.js", - "getDifferenceLabel.js", "getProcessForPort.js", "InterpolateHtmlPlugin.js", "openBrowser.js", @@ -33,7 +31,6 @@ "filesize": "3.3.0", "gzip-size": "^3.0.0", "html-entities": "1.2.0", - "fs-extra": "0.30.0", "opn": "4.0.2", "sockjs-client": "1.1.2", "strip-ansi": "3.0.1" diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index e8bca2173fb..95e4f61f8f7 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -28,7 +28,6 @@ var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); -var copyPublicFolder = require('react-dev-utils/copyPublicFolder'); var printFileSizes = require('react-dev-utils/printFileSizes'); var removeFileNameHash = require('react-dev-utils/removeFileNameHash'); var recursive = require('recursive-readdir'); @@ -186,3 +185,11 @@ function build(previousSizeMap) { } }); } + +function copyPublicFolder(appPublic, appBuild, appHtml) { + fs.copySync(appPublic, appBuild, { + dereference: true, + filter: file => file !== appHtml + }); +}; + From c5184d05e37b08fb6f33c1987d553755b4358edb Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 05:38:45 +0700 Subject: [PATCH 10/21] combine printFileSizes and removeFileNameHash to fileSizeReporter --- .../{printFileSizes.js => fileSizeReporter.js} | 18 +++++++++++++++++- packages/react-dev-utils/removeFileNameHash.js | 5 ----- packages/react-scripts/scripts/build.js | 14 +++----------- 3 files changed, 20 insertions(+), 17 deletions(-) rename packages/react-dev-utils/{printFileSizes.js => fileSizeReporter.js} (79%) delete mode 100644 packages/react-dev-utils/removeFileNameHash.js diff --git a/packages/react-dev-utils/printFileSizes.js b/packages/react-dev-utils/fileSizeReporter.js similarity index 79% rename from packages/react-dev-utils/printFileSizes.js rename to packages/react-dev-utils/fileSizeReporter.js index 1719c83f109..5f71f21977d 100644 --- a/packages/react-dev-utils/printFileSizes.js +++ b/packages/react-dev-utils/fileSizeReporter.js @@ -5,7 +5,7 @@ var filesize = require('filesize'); var stripAnsi = require('strip-ansi'); var gzipSize = require('gzip-size').sync; // Print a detailed summary of build files. -module.exports = function printFileSizes(appBuild, stats, previousSizeMap) { +function printFileSizesAfterBuild(appBuild, stats, previousSizeMap) { var assets = stats .toJson() .assets.filter(asset => /\.(js|css)$/.test(asset.name)) @@ -66,4 +66,20 @@ function getDifferenceLabel(currentSize, previousSize) { } }; +function measureFileSizesBeforeBuild(appBuild, fileNames){ + return (fileNames || []) + .filter(fileName => /\.(js|css)$/.test(fileName)) + .reduce((memo, fileName) => { + var contents = fs.readFileSync(fileName); + var key = removeFileNameHash(appBuild, fileName); + memo[key] = gzipSize(contents); + return memo; + }, {}); +} + +module.exports = { + measureFileSizesBeforeBuild: measureFileSizesBeforeBuild, + printFileSizesAfterBuild: printFileSizesAfterBuild +} + diff --git a/packages/react-dev-utils/removeFileNameHash.js b/packages/react-dev-utils/removeFileNameHash.js deleted file mode 100644 index 18b56ef1770..00000000000 --- a/packages/react-dev-utils/removeFileNameHash.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = function removeFileNameHash(appBuild, fileName) { - return fileName - .replace(appBuild, '') - .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); -}; \ No newline at end of file diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 95e4f61f8f7..e807125c6f0 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -28,8 +28,7 @@ var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); -var printFileSizes = require('react-dev-utils/printFileSizes'); -var removeFileNameHash = require('react-dev-utils/removeFileNameHash'); +var fileSizeReporter = require('react-dev-utils/fileSizeReporter'); var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); @@ -43,14 +42,7 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { // First, read the current file sizes in build directory. // This lets us display how much they changed later. recursive(paths.appBuild, (err, fileNames) => { - var previousSizeMap = (fileNames || []) - .filter(fileName => /\.(js|css)$/.test(fileName)) - .reduce((memo, fileName) => { - var contents = fs.readFileSync(fileName); - var key = removeFileNameHash(paths.appBuild, fileName); - memo[key] = gzipSize(contents); - return memo; - }, {}); + var previousSizeMap = fileSizeReporter.measureFileSizesBeforeBuild(paths, fileNames); // Remove all content but keep the directory so that // if you're in it, you don't end up in Trash @@ -110,7 +102,7 @@ function build(previousSizeMap) { console.log('File sizes after gzip:'); console.log(); - printFileSizes(paths.appBuild, stats, previousSizeMap); + fileSizeReporter.printFileSizesAfterBuild(paths.appBuild, stats, previousSizeMap); console.log(); var openCommand = process.platform === 'win32' ? 'start' : 'open'; From a363d8df9a186a5b711e3ed26e3e13d642ad6bc7 Mon Sep 17 00:00:00 2001 From: Ade Viankakrisna Fadlil Date: Mon, 6 Mar 2017 05:40:08 +0700 Subject: [PATCH 11/21] fix typo --- packages/react-scripts/scripts/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index e807125c6f0..34f5f23338f 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -42,7 +42,7 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { // First, read the current file sizes in build directory. // This lets us display how much they changed later. recursive(paths.appBuild, (err, fileNames) => { - var previousSizeMap = fileSizeReporter.measureFileSizesBeforeBuild(paths, fileNames); + var previousSizeMap = fileSizeReporter.measureFileSizesBeforeBuild(paths.appBuild, fileNames); // Remove all content but keep the directory so that // if you're in it, you don't end up in Trash From d29f858141399f9afc39fb41dae3215434567df0 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 00:47:23 +0000 Subject: [PATCH 12/21] Temp rename --- .../react-dev-utils/{fileSizeReporter.js => fileSizeReporter1.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/react-dev-utils/{fileSizeReporter.js => fileSizeReporter1.js} (100%) diff --git a/packages/react-dev-utils/fileSizeReporter.js b/packages/react-dev-utils/fileSizeReporter1.js similarity index 100% rename from packages/react-dev-utils/fileSizeReporter.js rename to packages/react-dev-utils/fileSizeReporter1.js From 27ce96e2eb070540859a24d667d46fac0dfe7ac9 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 00:47:37 +0000 Subject: [PATCH 13/21] Rename to change the case --- .../react-dev-utils/{fileSizeReporter1.js => FileSizeReporter.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/react-dev-utils/{fileSizeReporter1.js => FileSizeReporter.js} (100%) diff --git a/packages/react-dev-utils/fileSizeReporter1.js b/packages/react-dev-utils/FileSizeReporter.js similarity index 100% rename from packages/react-dev-utils/fileSizeReporter1.js rename to packages/react-dev-utils/FileSizeReporter.js From 07fed0450dc1c76543b90e38da161394c321f5bd Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:17:05 +0000 Subject: [PATCH 14/21] Tweak APIs and fix issues --- packages/react-dev-utils/FileSizeReporter.js | 69 +++++++++++++------- packages/react-dev-utils/README.md | 23 +++++++ packages/react-dev-utils/package.json | 23 +++---- packages/react-scripts/package.json | 4 -- packages/react-scripts/scripts/build.js | 30 ++++----- 5 files changed, 93 insertions(+), 56 deletions(-) diff --git a/packages/react-dev-utils/FileSizeReporter.js b/packages/react-dev-utils/FileSizeReporter.js index 5f71f21977d..44c972c4546 100644 --- a/packages/react-dev-utils/FileSizeReporter.js +++ b/packages/react-dev-utils/FileSizeReporter.js @@ -1,18 +1,33 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +'use strict'; + var fs = require('fs'); var path = require('path'); var chalk = require('chalk'); var filesize = require('filesize'); +var recursive = require('recursive-readdir'); var stripAnsi = require('strip-ansi'); var gzipSize = require('gzip-size').sync; -// Print a detailed summary of build files. -function printFileSizesAfterBuild(appBuild, stats, previousSizeMap) { - var assets = stats + +// Prints a detailed summary of build files. +function printFileSizesAfterBuild(webpackStats, previousSizeMap) { + var root = previousSizeMap.root; + var sizes = previousSizeMap.sizes; + var assets = webpackStats .toJson() .assets.filter(asset => /\.(js|css)$/.test(asset.name)) .map(asset => { - var fileContents = fs.readFileSync(appBuild + '/' + asset.name); + var fileContents = fs.readFileSync(path.join(root, asset.name)); var size = gzipSize(fileContents); - var previousSize = previousSizeMap[removeFileNameHash(appBuild, asset.name)]; + var previousSize = sizes[removeFileNameHash(root, asset.name)]; var difference = getDifferenceLabel(size, previousSize); return { folder: path.join('build', path.dirname(asset.name)), @@ -41,13 +56,13 @@ function printFileSizesAfterBuild(appBuild, stats, previousSizeMap) { chalk.cyan(asset.name) ); }); -}; +} -function removeFileNameHash(appBuild, fileName) { +function removeFileNameHash(buildFolder, fileName) { return fileName - .replace(appBuild, '') + .replace(buildFolder, '') .replace(/\/?(.*)(\.\w+)(\.js|\.css)/, (match, p1, p2, p3) => p1 + p3); -}; +} // Input: 1024, 2048 // Output: "(+1 KB)" @@ -64,22 +79,32 @@ function getDifferenceLabel(currentSize, previousSize) { } else { return ''; } -}; +} -function measureFileSizesBeforeBuild(appBuild, fileNames){ - return (fileNames || []) - .filter(fileName => /\.(js|css)$/.test(fileName)) - .reduce((memo, fileName) => { - var contents = fs.readFileSync(fileName); - var key = removeFileNameHash(appBuild, fileName); - memo[key] = gzipSize(contents); - return memo; - }, {}); +function measureFileSizesBeforeBuild(buildFolder) { + return new Promise(resolve => { + recursive(buildFolder, (err, fileNames) => { + var sizes; + if (!err && fileNames) { + sizes = fileNames + .filter(fileName => /\.(js|css)$/.test(fileName)) + .reduce((memo, fileName) => { + var contents = fs.readFileSync(fileName); + var key = removeFileNameHash(buildFolder, fileName); + memo[key] = gzipSize(contents); + return memo; + }, {}); + } + resolve({ + root: buildFolder, + sizes: sizes || {}, + }); + }); + }); } module.exports = { measureFileSizesBeforeBuild: measureFileSizesBeforeBuild, - printFileSizesAfterBuild: printFileSizesAfterBuild -} - + printFileSizesAfterBuild: printFileSizesAfterBuild, +}; diff --git a/packages/react-dev-utils/README.md b/packages/react-dev-utils/README.md index 2d430e2a299..0245c7e8086 100644 --- a/packages/react-dev-utils/README.md +++ b/packages/react-dev-utils/README.md @@ -110,6 +110,29 @@ clearConsole(); console.log('Just cleared the screen!'); ``` +#### `FileSizeReporter` + +##### `measureFileSizesBeforeBuild(buildFolder: string): Promise` + +Captures JS and CSS asset sizes inside the passed `buildFolder`. Save the result value to compare it after the build. + +#### `printFileSizesAfterBuild(webpackStats: WebpackStats, previousFileSizes: OpaqueFileSizes)` + +Prints the JS and CSS asset sizes after the build, and includes a size comparison with `previousFileSizes` that were captured earlier using `measureFileSizesBeforeBuild()`. + +```js +var { + measureFileSizesBeforeBuild, + printFileSizesAfterBuild, +} = require('react-dev-utils/FileSizeReporter'); + +measureFileSizesBeforeBuild(buildFolder).then(previousFileSizes => { + return cleanAndRebuild().then(webpackStats => { + printFileSizesAfterBuild(webpackStats, previousFileSizes); + }); +}); +``` + #### `formatWebpackMessages({errors: Array, warnings: Array}): {errors: Array, warnings: Array}` Extracts and prettifies warning and error messages from webpack [stats](/~https://github.com/webpack/docs/wiki/node.js-api#stats) object. diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 147ecf7bd16..35c490be84d 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -11,17 +11,17 @@ "node": ">=4" }, "files": [ - "checkRequiredFiles.js", - "clearConsole.js", - "formatWebpackMessages.js", - "getProcessForPort.js", - "InterpolateHtmlPlugin.js", - "openBrowser.js", - "openChrome.applescript", - "printFileSizes.js", + "checkRequiredFiles.js", + "clearConsole.js", + "FileSizeReporter.js", + "formatWebpackMessages.js", + "getProcessForPort.js", + "InterpolateHtmlPlugin.js", + "openBrowser.js", + "openChrome.applescript", "prompt.js", - "removeFileNameHash.js", - "WatchMissingNodeModulesPlugin.js", + "removeFileNameHash.js", + "WatchMissingNodeModulesPlugin.js", "webpackHotDevClient.js" ], "dependencies": { @@ -29,9 +29,10 @@ "chalk": "1.1.3", "escape-string-regexp": "1.0.5", "filesize": "3.3.0", - "gzip-size": "^3.0.0", + "gzip-size": "3.0.0", "html-entities": "1.2.0", "opn": "4.0.2", + "recursive-readdir": "2.1.1", "sockjs-client": "1.1.2", "strip-ansi": "3.0.1" } diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index e0ae953e95f..17a1c2fe59a 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -46,9 +46,7 @@ "eslint-plugin-react": "6.4.1", "extract-text-webpack-plugin": "2.0.0", "file-loader": "0.10.0", - "filesize": "3.3.0", "fs-extra": "0.30.0", - "gzip-size": "3.0.0", "html-webpack-plugin": "2.28.0", "http-proxy-middleware": "0.17.3", "jest": "18.1.0", @@ -56,8 +54,6 @@ "postcss-loader": "1.3.1", "promise": "7.1.1", "react-dev-utils": "^0.5.1", - "recursive-readdir": "2.1.1", - "strip-ansi": "3.0.1", "style-loader": "0.13.1", "url-loader": "0.5.7", "webpack": "2.2.1", diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 34f5f23338f..1e54d38ef7c 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -22,15 +22,13 @@ var chalk = require('chalk'); var fs = require('fs-extra'); var path = require('path'); var url = require('url'); -var filesize = require('filesize'); -var gzipSize = require('gzip-size').sync; var webpack = require('webpack'); var config = require('../config/webpack.config.prod'); var paths = require('../config/paths'); var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); -var fileSizeReporter = require('react-dev-utils/fileSizeReporter'); -var recursive = require('recursive-readdir'); -var stripAnsi = require('strip-ansi'); +var FileSizeReporter = require('react-dev-utils/FileSizeReporter'); +var measureFileSizesBeforeBuild = FileSizeReporter.measureFileSizesBeforeBuild; +var printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild; var useYarn = fs.existsSync(paths.yarnLockFile); @@ -41,22 +39,16 @@ if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { // First, read the current file sizes in build directory. // This lets us display how much they changed later. -recursive(paths.appBuild, (err, fileNames) => { - var previousSizeMap = fileSizeReporter.measureFileSizesBeforeBuild(paths.appBuild, fileNames); - +measureFileSizesBeforeBuild(paths.appBuild).then(previousFileSizes => { // Remove all content but keep the directory so that // if you're in it, you don't end up in Trash fs.emptyDirSync(paths.appBuild); // Start the webpack build - build(previousSizeMap); + build(previousFileSizes); // Merge with the public folder - copyPublicFolder( - paths.appPublic, - paths.appBuild, - paths.appHtml - ); + copyPublicFolder(); }); // Print out errors @@ -70,7 +62,7 @@ function printErrors(summary, errors) { } // Create the production build and print the deployment instructions. -function build(previousSizeMap) { +function build(previousFileSizes) { console.log('Creating an optimized production build...'); var compiler; @@ -102,7 +94,7 @@ function build(previousSizeMap) { console.log('File sizes after gzip:'); console.log(); - fileSizeReporter.printFileSizesAfterBuild(paths.appBuild, stats, previousSizeMap); + printFileSizesAfterBuild(stats, previousFileSizes); console.log(); var openCommand = process.platform === 'win32' ? 'start' : 'open'; @@ -178,10 +170,10 @@ function build(previousSizeMap) { }); } -function copyPublicFolder(appPublic, appBuild, appHtml) { - fs.copySync(appPublic, appBuild, { +function copyPublicFolder() { + fs.copySync(paths.appPublic, paths.appBuild, { dereference: true, - filter: file => file !== appHtml + filter: file => file !== paths.appHtml }); }; From 4ddfed9d4bc322f6dd22602337717add898bdb31 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:18:38 +0000 Subject: [PATCH 15/21] Fix heading --- packages/react-dev-utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-dev-utils/README.md b/packages/react-dev-utils/README.md index 0245c7e8086..26d762c3597 100644 --- a/packages/react-dev-utils/README.md +++ b/packages/react-dev-utils/README.md @@ -116,7 +116,7 @@ console.log('Just cleared the screen!'); Captures JS and CSS asset sizes inside the passed `buildFolder`. Save the result value to compare it after the build. -#### `printFileSizesAfterBuild(webpackStats: WebpackStats, previousFileSizes: OpaqueFileSizes)` +##### `printFileSizesAfterBuild(webpackStats: WebpackStats, previousFileSizes: OpaqueFileSizes)` Prints the JS and CSS asset sizes after the build, and includes a size comparison with `previousFileSizes` that were captured earlier using `measureFileSizesBeforeBuild()`. From 0d265095d1d0f7fcec59f82860e8280686fb7bf7 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:20:29 +0000 Subject: [PATCH 16/21] Remove missing file --- packages/react-dev-utils/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 35c490be84d..cabb2a33ce5 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -20,7 +20,6 @@ "openBrowser.js", "openChrome.applescript", "prompt.js", - "removeFileNameHash.js", "WatchMissingNodeModulesPlugin.js", "webpackHotDevClient.js" ], From 4282ab8ecbcf467e0a40d51c3679eb3ad91f2b7e Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:20:58 +0000 Subject: [PATCH 17/21] Newline --- packages/react-dev-utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index cabb2a33ce5..95f0c9177d6 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -35,4 +35,4 @@ "sockjs-client": "1.1.2", "strip-ansi": "3.0.1" } -} \ No newline at end of file +} From 3a01e4158f72fa9d541b7fb2aa9215791289b17b Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:21:39 +0000 Subject: [PATCH 18/21] Newline --- packages/react-scripts/scripts/build.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index 1e54d38ef7c..cdc13989534 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -176,4 +176,3 @@ function copyPublicFolder() { filter: file => file !== paths.appHtml }); }; - From ac850b70a218ecadc5232fb8774757fdb6847574 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:23:01 +0000 Subject: [PATCH 19/21] Trailing space --- packages/react-dev-utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-dev-utils/package.json b/packages/react-dev-utils/package.json index 95f0c9177d6..b983d80b008 100644 --- a/packages/react-dev-utils/package.json +++ b/packages/react-dev-utils/package.json @@ -10,7 +10,7 @@ "engines": { "node": ">=4" }, - "files": [ + "files": [ "checkRequiredFiles.js", "clearConsole.js", "FileSizeReporter.js", From 8e155595ffcc11761f97331b6630bca03766f6d0 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:49:24 +0000 Subject: [PATCH 20/21] Update FileSizeReporter.js --- packages/react-dev-utils/FileSizeReporter.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react-dev-utils/FileSizeReporter.js b/packages/react-dev-utils/FileSizeReporter.js index 44c972c4546..08273673f92 100644 --- a/packages/react-dev-utils/FileSizeReporter.js +++ b/packages/react-dev-utils/FileSizeReporter.js @@ -107,4 +107,3 @@ module.exports = { measureFileSizesBeforeBuild: measureFileSizesBeforeBuild, printFileSizesAfterBuild: printFileSizesAfterBuild, }; - From a6e9f06f0e156530bbbd07933f2721a352365419 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 Mar 2017 01:49:36 +0000 Subject: [PATCH 21/21] Update build.js --- packages/react-scripts/scripts/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-scripts/scripts/build.js b/packages/react-scripts/scripts/build.js index cdc13989534..b57a9dfb821 100644 --- a/packages/react-scripts/scripts/build.js +++ b/packages/react-scripts/scripts/build.js @@ -175,4 +175,4 @@ function copyPublicFolder() { dereference: true, filter: file => file !== paths.appHtml }); -}; +}