Fix binary file size calculation (readFile without encoding)#94
Merged
Conversation
`test-file-1.jpg` file size is 38962 bytes:
```
__testdata__# ls -la test-file-1.jpg
-rw-r--r-- 1 root root 38962 Mar 22 14:42 test-file-1.jpg
```
Reading this file as `fs.readFile(path, 'utf8')` leads to incorrect result:
```js
const data = fs.readFileSync('./__testdata__/test-file-1.jpg', 'utf8')
Buffer.byteLength(data)
// 70469
```
Right way to read as raw buffer `fs.readFile(path)`:
```js
const data = fs.readFileSync('./__testdata__/test-file-1.jpg')
Buffer.byteLength(data)
// 38962
```
Test snapshots was wrong, you could compare real file size to reported.
Contributor
Author
|
CircleCI passed, however, bundlewatch report can't be sent: |
Contributor
Author
|
Any thoughts on this? The same issue exists in https://github.com/siddharthkp/bundlesize (siddharthkp/bundlesize#281). PR with fix I believe this issue quite important. Many projects use webpack |
Contributor
|
@jakebolam this seems like a proper bugfix 🙂 |
iamogbz
approved these changes
Apr 26, 2020
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Fix binary file size calculation.
Did you add tests for your changes?
Test snapshots were updated.
Previous snapshots were wrong; you could compare reported with real file size.
If relevant, link to documentation update:
N/A
Summary
We using standard https://github.com/webpack-contrib/compression-webpack-plugin for our builds.
CompressionPlugingeneratesgzipandbrotlicompressed files. So, we want to use these files and avoid compression of JS files once again in bundlewatch (viagzip-sizeorbrotli-size).A simplified version of our config:
{ "files": [ { "name": "JS vendors chunk", "maxSize": "1.0 MB", "path": "./packs/js/vendors.chunk.js.br", "compression": "none" } ] }But, using
"compression": "none"setting we found bundlewatch reports incorrect file size (twice lager). This issue relevant to any binary file.Example from bundlewatch test data:
test-file-1.jpgfile size is 38962 bytes:Reading this file as
fs.readFile(path, 'utf8')leads to incorrect result:Right way to read as raw buffer
fs.readFile(path):The same issue exists in https://github.com/siddharthkp/bundlesize (siddharthkp/bundlesize#281) and similar fix in their PRs:
siddharthkp/bundlesize#267.
Does this PR introduce a breaking change?
No. But users will receive proper file size (that would differ from past results for binary files).
Other information