Skip to content

Commit

Permalink
Reduce the usage of lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Mar 31, 2022
1 parent 7d6039e commit f266ffb
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 28 deletions.
3 changes: 2 additions & 1 deletion client/components/ModuleItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export default class ModuleItem extends PureComponent {
}

get invisibleHint() {
return `${_.upperFirst(this.itemType)} is not rendered in the treemap because it's too small.`;
const itemType = this.itemType.charAt(0).toUpperCase() + this.itemType.slice(1);
return `${itemType} is not rendered in the treemap because it's too small.`;
}

get isVisible() {
Expand Down
13 changes: 9 additions & 4 deletions src/analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ function getViewerData(bundleStats, bundleDir, opts) {
continue;
}

bundlesSources[statAsset.name] = _.pick(bundleInfo, 'src', 'runtimeSrc');
bundlesSources[statAsset.name] = {
src: bundleInfo.src,
runtimeSrc: bundleInfo.runtimeSrc
};
Object.assign(parsedModules, bundleInfo.modules);
}

Expand All @@ -96,8 +99,10 @@ function getViewerData(bundleStats, bundleDir, opts) {
// If asset is a childAsset, then calculate appropriate bundle modules by looking through stats.children
const assetBundles = statAsset.isChild ? getChildAssetBundles(bundleStats, statAsset.name) : bundleStats;
const modules = assetBundles ? getBundleModules(assetBundles) : [];
const asset = result[statAsset.name] = _.pick(statAsset, 'size');
const assetSources = bundlesSources && _.has(bundlesSources, statAsset.name) ?
const asset = result[statAsset.name] = {
size: statAsset.size
};
const assetSources = bundlesSources && Object.prototype.hasOwnProperty.call(bundlesSources, statAsset.name) ?
bundlesSources[statAsset.name] : null;

if (assetSources) {
Expand Down Expand Up @@ -157,7 +162,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
statSize: asset.tree.size || asset.size,
parsedSize: asset.parsedSize,
gzipSize: asset.gzipSize,
groups: _.invokeMap(asset.tree.children, 'toChartData')
groups: asset.tree.children.map(i => i.toChartData())
}));
}

Expand Down
5 changes: 1 addition & 4 deletions src/parseUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require('fs');
const _ = require('lodash');
const acorn = require('acorn');
const walk = require('acorn-walk');

Expand Down Expand Up @@ -143,9 +142,7 @@ function parseBundle(bundlePath) {
let modules;

if (walkState.locations) {
modules = _.mapValues(walkState.locations,
loc => content.slice(loc.start, loc.end)
);
modules = walkState.locations.map(loc => content.slice(loc.start, loc.end));
} else {
modules = {};
}
Expand Down
8 changes: 3 additions & 5 deletions src/tree/BaseFolder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash';

import Node from './Node';

export default class BaseFolder extends Node {
Expand All @@ -10,15 +8,15 @@ export default class BaseFolder extends Node {
}

get src() {
if (!_.has(this, '_src')) {
if (!Object.prototype.hasOwnProperty.call(this, '_src')) {
this._src = this.walk((node, src) => (src += node.src || ''), '', false);
}

return this._src;
}

get size() {
if (!_.has(this, '_size')) {
if (!Object.prototype.hasOwnProperty.call(this, '_size')) {
this._size = this.walk((node, size) => (size + node.size), 0, false);
}

Expand Down Expand Up @@ -111,7 +109,7 @@ export default class BaseFolder extends Node {
label: this.name,
path: this.path,
statSize: this.size,
groups: _.invokeMap(this.children, 'toChartData')
groups: this.children.map(i => i.toChartData())
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/tree/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ export default class ConcatenatedModule extends Module {
}

mergeNestedFolders() {
_.invokeMap(this.children, 'mergeNestedFolders');
this.children.forEach(child => child.mergeNestedFolders());
}

toChartData() {
return {
...super.toChartData(),
concatenated: true,
groups: _.invokeMap(this.children, 'toChartData')
groups: this.children.map(i => i.toChartData())
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/tree/Folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Folder extends BaseFolder {
}

get gzipSize() {
if (!_.has(this, '_gzipSize')) {
if (!Object.prototype.hasOwnProperty.call(this, '_gzipSize')) {
this._gzipSize = this.src ? gzipSize.sync(this.src) : 0;
}

Expand Down
3 changes: 1 addition & 2 deletions src/tree/Module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import gzipSize from 'gzip-size';

import Node from './Node';
Expand Down Expand Up @@ -32,7 +31,7 @@ export default class Module extends Node {
}

get gzipSize() {
if (!_.has(this, '_gzipSize')) {
if (!Object.prototype.hasOwnProperty.call(this, '_gzipSize')) {
this._gzipSize = this.src ? gzipSize.sync(this.src) : undefined;
}

Expand Down
11 changes: 5 additions & 6 deletions test/analyzer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const chai = require('chai');
chai.use(require('chai-subset'));
const {expect} = chai;
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const del = require('del');
Expand Down Expand Up @@ -107,7 +106,7 @@ describe('Analyzer', function () {
it('should gracefully parse invalid chunks', async function () {
generateReportFrom('with-invalid-chunk/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
const invalidChunk = chartData.find(i => i.label === 'invalid-chunk.js');
expect(invalidChunk.groups).to.containSubset([
{
id: 1,
Expand All @@ -123,14 +122,14 @@ describe('Analyzer', function () {
it('should gracefully process missing chunks', async function () {
generateReportFrom('with-missing-chunk/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
const invalidChunk = chartData.find(i => i.label === 'invalid-chunk.js');
expect(invalidChunk).to.exist;
expect(invalidChunk.statSize).to.equal(24);
forEachChartItem([invalidChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(item.parsedSize).to.be.undefined;
});
const validChunk = _.find(chartData, {label: 'valid-chunk.js'});
const validChunk = chartData.find(i => i.label === 'valid-chunk.js');
forEachChartItem([validChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(typeof item.parsedSize).to.equal('number');
Expand All @@ -140,14 +139,14 @@ describe('Analyzer', function () {
it('should gracefully process missing chunks', async function () {
generateReportFrom('with-missing-module-chunks/stats.json');
const chartData = await getChartData();
const invalidChunk = _.find(chartData, {label: 'invalid-chunk.js'});
const invalidChunk = chartData.find(i => i.label === 'invalid-chunk.js');
expect(invalidChunk).to.exist;
expect(invalidChunk.statSize).to.equal(568);
forEachChartItem([invalidChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(item.parsedSize).to.be.undefined;
});
const validChunk = _.find(chartData, {label: 'valid-chunk.js'});
const validChunk = chartData.find(i => i.label === 'valid-chunk.js');
forEachChartItem([validChunk], item => {
expect(typeof item.statSize).to.equal('number');
expect(typeof item.parsedSize).to.equal('number');
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getAvailableWebpackVersions = _.memoize(() =>
function forEachWebpackVersion(versions, cb) {
const availableVersions = getAvailableWebpackVersions();

if (_.isFunction(versions)) {
if (typeof versions === 'function') {
cb = versions;
versions = availableVersions;
} else {
Expand Down
3 changes: 1 addition & 2 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ chai.use(require('chai-subset'));
const {expect} = chai;
const fs = require('fs');
const del = require('del');
const _ = require('lodash');
const path = require('path');
const puppeteer = require('puppeteer');
const BundleAnalyzerPlugin = require('../lib/BundleAnalyzerPlugin');
Expand Down Expand Up @@ -113,7 +112,7 @@ describe('Plugin', function () {
await webpackCompile(config);

const chartData = await getChartDataFromReport();
expect(_.map(chartData, 'label'))
expect(chartData.map(i => i.label))
.to
.deep
.equal(['bundle.js']);
Expand Down

0 comments on commit f266ffb

Please sign in to comment.