Skip to content

Commit e120231

Browse files
authored
Reduce the usage of lodash (#505)
* Reduce the usage of lodash * Add missing comment back * Update unreleased changelog * Add missing comment back * Fix misplaced changelog * Fix edge cases * Fix viewer.js tests * Make ESLint Happy
1 parent ff7631d commit e120231

File tree

15 files changed

+44
-46
lines changed

15 files changed

+44
-46
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
1212

1313
## UNRELEASED
1414

15+
* **Internal**
16+
* Replace some lodash usages with JavaScript native API ([#505](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/505)) by [@sukkaw](https://github.com/sukkaw).
17+
1518
## 4.9.0
1619

1720
* **Improvement**

client/components/ModuleItem.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export default class ModuleItem extends PureComponent {
7373
}
7474

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

7980
get isVisible() {

src/analyzer.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function getViewerData(bundleStats, bundleDir, opts) {
2727
const isAssetIncluded = createAssetsFilter(excludeAssets);
2828

2929
// Sometimes all the information is located in `children` array (e.g. problem in #10)
30-
if (_.isEmpty(bundleStats.assets) && !_.isEmpty(bundleStats.children)) {
30+
if (
31+
(bundleStats.assets == null || bundleStats.assets.length === 0)
32+
&& bundleStats.children && bundleStats.children.length > 0
33+
) {
3134
const {children} = bundleStats;
3235
bundleStats = bundleStats.children[0];
3336
// Sometimes if there are additional child chunks produced add them as child assets,
@@ -38,7 +41,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
3841
bundleStats.assets.push(asset);
3942
});
4043
}
41-
} else if (!_.isEmpty(bundleStats.children)) {
44+
} else if (bundleStats.children && bundleStats.children.length > 0) {
4245
// Sometimes if there are additional child chunks produced add them as child assets
4346
bundleStats.children.forEach((child) => {
4447
child.assets.forEach((asset) => {
@@ -59,7 +62,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
5962
// See #22
6063
asset.name = asset.name.replace(FILENAME_QUERY_REGEXP, '');
6164

62-
return FILENAME_EXTENSIONS.test(asset.name) && !_.isEmpty(asset.chunks) && isAssetIncluded(asset.name);
65+
return FILENAME_EXTENSIONS.test(asset.name) && asset.chunks.length > 0 && isAssetIncluded(asset.name);
6366
});
6467

6568
// Trying to parse bundle assets and get real module sizes if `bundleDir` is provided
@@ -82,11 +85,14 @@ function getViewerData(bundleStats, bundleDir, opts) {
8285
continue;
8386
}
8487

85-
bundlesSources[statAsset.name] = _.pick(bundleInfo, 'src', 'runtimeSrc');
88+
bundlesSources[statAsset.name] = {
89+
src: bundleInfo.src,
90+
runtimeSrc: bundleInfo.runtimeSrc
91+
};
8692
Object.assign(parsedModules, bundleInfo.modules);
8793
}
8894

89-
if (_.isEmpty(bundlesSources)) {
95+
if (Object.keys(bundlesSources).length === 0) {
9096
bundlesSources = null;
9197
parsedModules = null;
9298
logger.warn('\nNo bundles were parsed. Analyzer will show only original module sizes from stats file.\n');
@@ -97,8 +103,10 @@ function getViewerData(bundleStats, bundleDir, opts) {
97103
// If asset is a childAsset, then calculate appropriate bundle modules by looking through stats.children
98104
const assetBundles = statAsset.isChild ? getChildAssetBundles(bundleStats, statAsset.name) : bundleStats;
99105
const modules = assetBundles ? getBundleModules(assetBundles) : [];
100-
const asset = result[statAsset.name] = _.pick(statAsset, 'size');
101-
const assetSources = bundlesSources && _.has(bundlesSources, statAsset.name) ?
106+
const asset = result[statAsset.name] = {
107+
size: statAsset.size
108+
};
109+
const assetSources = bundlesSources && Object.prototype.hasOwnProperty.call(bundlesSources, statAsset.name) ?
102110
bundlesSources[statAsset.name] : null;
103111

104112
if (assetSources) {

src/parseUtils.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const fs = require('fs');
2-
const _ = require('lodash');
32
const acorn = require('acorn');
43
const walk = require('acorn-walk');
54

@@ -140,14 +139,12 @@ function parseBundle(bundlePath) {
140139
}
141140
);
142141

143-
let modules;
142+
const modules = {};
144143

145144
if (walkState.locations) {
146-
modules = _.mapValues(walkState.locations,
147-
loc => content.slice(loc.start, loc.end)
148-
);
149-
} else {
150-
modules = {};
145+
Object.entries(walkState.locations).forEach(([id, loc]) => {
146+
modules[id] = content.slice(loc.start, loc.end);
147+
});
151148
}
152149

153150
return {

src/tree/BaseFolder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ export default class BaseFolder extends Node {
1010
}
1111

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

1717
return this._src;
1818
}
1919

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

src/tree/ConcatenatedModule.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ from 'lodash';
2-
32
import Module from './Module';
43
import ContentModule from './ContentModule';
54
import ContentFolder from './ContentFolder';
@@ -41,7 +40,7 @@ export default class ConcatenatedModule extends Module {
4140
return;
4241
}
4342

44-
const [folders, fileName] = [pathParts.slice(0, -1), _.last(pathParts)];
43+
const [folders, fileName] = [pathParts.slice(0, -1), pathParts[pathParts.length - 1]];
4544
let currentFolder = this;
4645

4746
folders.forEach(folderName => {

src/tree/Folder.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import gzipSize from 'gzip-size';
32

43
import Module from './Module';
@@ -13,7 +12,7 @@ export default class Folder extends BaseFolder {
1312
}
1413

1514
get gzipSize() {
16-
if (!_.has(this, '_gzipSize')) {
15+
if (!Object.prototype.hasOwnProperty.call(this, '_gzipSize')) {
1716
this._gzipSize = this.src ? gzipSize.sync(this.src) : 0;
1817
}
1918

@@ -27,7 +26,7 @@ export default class Folder extends BaseFolder {
2726
return;
2827
}
2928

30-
const [folders, fileName] = [pathParts.slice(0, -1), _.last(pathParts)];
29+
const [folders, fileName] = [pathParts.slice(0, -1), pathParts[pathParts.length - 1]];
3130
let currentFolder = this;
3231

3332
folders.forEach(folderName => {

src/tree/Module.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import _ from 'lodash';
21
import gzipSize from 'gzip-size';
32

43
import Node from './Node';
@@ -40,7 +39,7 @@ export default class Module extends Node {
4039
}
4140

4241
getGzipSize() {
43-
if (!_.has(this, '_gzipSize')) {
42+
if (!('_gzipSize' in this)) {
4443
this._gzipSize = this.src ? gzipSize.sync(this.src) : undefined;
4544
}
4645

src/tree/utils.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import _ from 'lodash';
2-
31
const MULTI_MODULE_REGEXP = /^multi /u;
42

53
export function getModulePathParts(moduleData) {
64
if (MULTI_MODULE_REGEXP.test(moduleData.identifier)) {
75
return [moduleData.identifier];
86
}
97

10-
const parsedPath = _
8+
const loaders = moduleData.name.split('!');
119
// Removing loaders from module path: they're joined by `!` and the last part is a raw module path
12-
.last(moduleData.name.split('!'))
10+
const parsedPath = loaders[loaders.length - 1]
1311
// Splitting module path into parts
1412
.split('/')
1513
// Removing first `.`

src/utils.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const {inspect, types} = require('util');
2-
const _ = require('lodash');
32
const opener = require('opener');
43

54
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
65

76
exports.createAssetsFilter = createAssetsFilter;
87

98
function createAssetsFilter(excludePatterns) {
10-
const excludeFunctions = _(excludePatterns)
11-
.castArray()
12-
.compact()
9+
const excludeFunctions = (Array.isArray(excludePatterns) ? excludePatterns : [excludePatterns])
10+
.filter(Boolean)
1311
.map(pattern => {
1412
if (typeof pattern === 'string') {
1513
pattern = new RegExp(pattern, 'u');
@@ -26,8 +24,7 @@ function createAssetsFilter(excludePatterns) {
2624
}
2725

2826
return pattern;
29-
})
30-
.value();
27+
});
3128

3229
if (excludeFunctions.length) {
3330
return (asset) => excludeFunctions.every(fn => fn(asset) !== true);

0 commit comments

Comments
 (0)