Skip to content

Commit

Permalink
improve asset size check
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Nov 27, 2019
1 parent 353dd0c commit 47c18d6
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions bin/asset-size-tracking/src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const TablePads = {
const BROTLI_OPTIONS = {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
},
};

Expand All @@ -29,6 +30,7 @@ class Library {
if (!pkg) {
pkg = this._packageMap[name] = new Package(name, this);
this.packages.push(pkg);
this._compressedSize = null;
}

return pkg;
Expand Down Expand Up @@ -100,6 +102,7 @@ class Package {
this.modules.push(mod);
this._concatModule += code;
this.library._concatModule += code;
this.library._compressedSize = null;
return mod;
}
get concatModule() {
Expand All @@ -109,10 +112,8 @@ class Package {
return byteCount(this.concatModule);
}
get compressedSize() {
return (
Math.floor((this.concatModule.length / this.library.concatModule.length) * this.library.compressedSize * 100) /
100
);
const ratio = this.absoluteSize / this.library.absoluteSize;
return Math.floor(ratio * this.library.compressedSize * 100) / 100;
}
get percentOfLibrary() {
return getRelativeSizeOf(this.library, this);
Expand Down Expand Up @@ -155,17 +156,12 @@ class Module {
this.package = pkg;
this.code = code;
}
get size() {
return this.code.length;
}
get absoluteSize() {
return byteCount(this.code);
}
get compressedSize() {
return (
Math.floor((this.size / this.package.library.concatModule.length) * this.package.library.compressedSize * 100) /
100
);
const ratio = this.absoluteSize / this.package.library.absoluteSize;
return Math.floor(ratio * this.package.library.compressedSize * 100) / 100;
}
get bytes() {
return formatBytes(this.absoluteSize);
Expand Down Expand Up @@ -231,7 +227,7 @@ function formatBytes(b) {
}

function byteCount(s) {
return encodeURI(s).split(/%..|./).length - 1;
return Buffer.byteLength(s, 'utf8');
}

module.exports = Library;

0 comments on commit 47c18d6

Please sign in to comment.