-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract generic build functions to react-dev-utils (#1726)
* Temp rename * Rename to change the case * extract generic build functions to react-dev-utils * tweak package json files and move removeFileNameHash * revert removeFileNameHash * use paths.appBuild in printFileSizes * use paths.appBuild in removeFileNameHash * change curried functions to regular functions * add fs-extra to react-dev-utils deps * move getDifferenceLabel inside printFileSizes * inline copyPublicFolder * combine printFileSizes and removeFileNameHash to fileSizeReporter * fix typo * Tweak APIs and fix issues * Fix heading * Remove missing file * Newline * Newline * Trailing space * Update FileSizeReporter.js * Update build.js
- Loading branch information
1 parent
3fa5e8e
commit fbdff9d
Showing
5 changed files
with
145 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* 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; | ||
|
||
// 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(path.join(root, asset.name)); | ||
var size = gzipSize(fileContents); | ||
var previousSize = sizes[removeFileNameHash(root, 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) | ||
); | ||
}); | ||
} | ||
|
||
function removeFileNameHash(buildFolder, fileName) { | ||
return fileName | ||
.replace(buildFolder, '') | ||
.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 ''; | ||
} | ||
} | ||
|
||
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters