Skip to content

Commit

Permalink
Single source of thruth for compressed size helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dcsaszar committed Apr 13, 2021
1 parent 912aa8c commit 6a4a361
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = {
function getViewerData(bundleStats, bundleDir, opts) {
const {
logger = new Logger(),
excludeAssets = null
excludeAssets = null,
compressedSize = gzipSize
} = opts || {};

const isAssetIncluded = createAssetsFilter(excludeAssets);
Expand Down Expand Up @@ -102,7 +103,7 @@ function getViewerData(bundleStats, bundleDir, opts) {

if (assetSources) {
asset.parsedSize = Buffer.byteLength(assetSources.src);
asset.gzipSize = gzipSize(assetSources.src);
asset.gzipSize = compressedSize(assetSources.src);
}

// Picking modules from current bundle script
Expand Down Expand Up @@ -143,7 +144,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
}

asset.modules = assetModules;
asset.tree = createModulesTree(asset.modules);
asset.tree = createModulesTree(asset.modules, {compressedSize});
return result;
}, {});

Expand Down Expand Up @@ -203,8 +204,8 @@ function isRuntimeModule(statModule) {
return statModule.moduleType === 'runtime';
}

function createModulesTree(modules) {
const root = new Folder('.');
function createModulesTree(modules, opts) {
const root = new Folder('.', opts);

modules.forEach(module => root.addModule(module));
root.mergeNestedFolders();
Expand Down
10 changes: 7 additions & 3 deletions src/tree/Folder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash';
import {gzipSize} from '../sizeUtils';

import Module from './Module';
import BaseFolder from './BaseFolder';
Expand All @@ -8,13 +7,18 @@ import {getModulePathParts} from './utils';

export default class Folder extends BaseFolder {

constructor(name, opts) {
super(name);
this.opts = opts;
}

get parsedSize() {
return this.src ? this.src.length : 0;
}

get gzipSize() {
if (!_.has(this, '_gzipSize')) {
this._gzipSize = this.src ? gzipSize(this.src) : 0;
this._gzipSize = this.src ? this.opts.compressedSize(this.src) : 0;
}

return this._gzipSize;
Expand Down Expand Up @@ -42,7 +46,7 @@ export default class Folder extends BaseFolder {
// See `test/stats/with-invalid-dynamic-require.json` as an example.
!(childNode instanceof Folder)
) {
childNode = currentFolder.addChildFolder(new Folder(folderName));
childNode = currentFolder.addChildFolder(new Folder(folderName, this.opts));
}

currentFolder = childNode;
Expand Down

0 comments on commit 6a4a361

Please sign in to comment.