Skip to content

Commit f01056a

Browse files
authored
Replace all lodash.* packages (#612)
* Replace `lodash.escape` w/ `html-escaper` * Replace `lodash.flatten` with a small function * Repalce `lodash.debounce` w/ `debounce` * Replace `lodash.pullall` w/ Array.prototype.filter * Chore: add a TODO comment for debounce package replacement * Replace `lodash.uniqby` w/ Set+filter * Replace `lodash.invokemap` w/ Object.values * Fix invokemap replacement * Update CHANGELOG
1 parent 52bf2ea commit f01056a

File tree

9 files changed

+76
-60
lines changed

9 files changed

+76
-60
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+
* Make module much slimmer by replacing all `lodash.*` packages ([#612](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/612)) by [@sukkaw](https://github.com/sukkaw).
17+
1518
## 4.9.1
1619

1720
* **Internal**

client/components/ModuleItem.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import escapeRegExp from 'escape-string-regexp';
2-
import escape from 'lodash.escape';
2+
import {escape} from 'html-escaper';
33
import filesize from 'filesize';
44
import cls from 'classnames';
55

client/components/Search.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import debounce from 'lodash.debounce';
1+
// TODO: switch to a more modern debounce package once we drop Node.js 10 support
2+
import debounce from 'debounce';
23

34
import s from './Search.css';
45
import Button from './Button';

package-lock.json

Lines changed: 8 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"engines": {
1919
"node": ">= 10.13.0"
2020
},
21+
"packageManager": "[email protected]",
2122
"scripts": {
2223
"start": "gulp watch",
2324
"build": "gulp build",
@@ -36,15 +37,11 @@
3637
"acorn": "^8.0.4",
3738
"acorn-walk": "^8.0.0",
3839
"commander": "^7.2.0",
40+
"debounce": "^1.2.1",
3941
"escape-string-regexp": "^4.0.0",
4042
"gzip-size": "^6.0.0",
43+
"html-escaper": "^2.0.2",
4144
"is-plain-object": "^5.0.0",
42-
"lodash.debounce": "^4.0.8",
43-
"lodash.escape": "^4.0.1",
44-
"lodash.flatten": "^4.4.0",
45-
"lodash.invokemap": "^4.6.0",
46-
"lodash.pullall": "^4.2.0",
47-
"lodash.uniqby": "^4.7.0",
4845
"opener": "^1.5.2",
4946
"picocolors": "^1.0.0",
5047
"sirv": "^2.0.3",

src/analyzer.js

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
33

4-
const pullAll = require('lodash.pullall');
5-
const invokeMap = require('lodash.invokemap');
6-
const uniqBy = require('lodash.uniqby');
7-
const flatten = require('lodash.flatten');
8-
94
const gzipSize = require('gzip-size');
105
const {parseChunked} = require('@discoveryjs/json-ext');
116

@@ -119,7 +114,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
119114
}
120115

121116
// Picking modules from current bundle script
122-
const assetModules = modules.filter(statModule => assetHasModule(statAsset, statModule));
117+
let assetModules = modules.filter(statModule => assetHasModule(statAsset, statModule));
123118

124119
// Adding parsed sources
125120
if (parsedModules) {
@@ -143,7 +138,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
143138
unparsedEntryModules[0].parsedSrc = assetSources.runtimeSrc;
144139
} else {
145140
// If there are multiple entry points we move all of them under synthetic concatenated module.
146-
pullAll(assetModules, unparsedEntryModules);
141+
assetModules = assetModules.filter(mod => !unparsedEntryModules.includes(mod));
147142
assetModules.unshift({
148143
identifier: './entry modules',
149144
name: './entry modules',
@@ -171,7 +166,7 @@ function getViewerData(bundleStats, bundleDir, opts) {
171166
statSize: asset.tree.size || asset.size,
172167
parsedSize: asset.parsedSize,
173168
gzipSize: asset.gzipSize,
174-
groups: invokeMap(asset.tree.children, 'toChartData'),
169+
groups: Object.values(asset.tree.children).map(i => i.toChartData()),
175170
isInitialByEntrypoint: chunkToInitialByEntrypoint[filename] ?? {}
176171
}));
177172
}
@@ -191,15 +186,23 @@ function getChildAssetBundles(bundleStats, assetName) {
191186
}
192187

193188
function getBundleModules(bundleStats) {
194-
return uniqBy(
195-
flatten(
196-
((bundleStats.chunks?.map(chunk => chunk.modules)) || [])
197-
.concat(bundleStats.modules)
198-
.filter(Boolean)
199-
),
200-
'id'
201-
// Filtering out Webpack's runtime modules as they don't have ids and can't be parsed (introduced in Webpack 5)
202-
).filter(m => !isRuntimeModule(m));
189+
const seenIds = new Set();
190+
191+
return flatten(
192+
((bundleStats.chunks?.map(chunk => chunk.modules)) || [])
193+
.concat(bundleStats.modules)
194+
.filter(Boolean)
195+
).filter(mod => {
196+
// Filtering out Webpack's runtime modules as they don't have ids and can't be parsed (introduced in Webpack 5)
197+
if (isRuntimeModule(mod)) {
198+
return false;
199+
}
200+
if (seenIds.has(mod.id)) {
201+
return false;
202+
}
203+
seenIds.add(mod.id);
204+
return true;
205+
});
203206
}
204207

205208
function assetHasModule(statAsset, statModule) {
@@ -239,3 +242,34 @@ function getChunkToInitialByEntrypoint(bundleStats) {
239242
});
240243
return chunkToEntrypointInititalMap;
241244
};
245+
246+
/**
247+
* arr-flatten <https://github.com/jonschlinkert/arr-flatten>
248+
*
249+
* Copyright (c) 2014-2017, Jon Schlinkert.
250+
* Released under the MIT License.
251+
*
252+
* Modified by Sukka <https://skk.moe>
253+
*
254+
* Replace recursively flatten with one-level deep flatten to match lodash.flatten
255+
*
256+
* TODO: replace with Array.prototype.flat once Node.js 10 support is dropped
257+
*/
258+
function flatten(arr) {
259+
if (!arr) return [];
260+
const len = arr.length;
261+
if (!len) return [];
262+
263+
let cur;
264+
265+
const res = [];
266+
for (let i = 0; i < len; i++) {
267+
cur = arr[i];
268+
if (Array.isArray(cur)) {
269+
res.push(...cur);
270+
} else {
271+
res.push(cur);
272+
}
273+
}
274+
return res;
275+
}

src/template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require('path');
33
const fs = require('fs');
44

5-
const escape = require('lodash.escape');
5+
const {escape} = require('html-escaper');
66

77
const projectRoot = path.resolve(__dirname, '..');
88
const assetsRoot = path.join(projectRoot, 'public');

src/tree/BaseFolder.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import invokeMap from 'lodash.invokemap';
2-
31
import Node from './Node';
42

53
export default class BaseFolder extends Node {
@@ -111,7 +109,7 @@ export default class BaseFolder extends Node {
111109
label: this.name,
112110
path: this.path,
113111
statSize: this.size,
114-
groups: invokeMap(this.children, 'toChartData')
112+
groups: Object.values(this.children).map(child => child.toChartData())
115113
};
116114
}
117115

src/tree/ConcatenatedModule.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import invokeMap from 'lodash.invokemap';
21
import Module from './Module';
32
import ContentModule from './ContentModule';
43
import ContentFolder from './ContentFolder';
@@ -74,14 +73,18 @@ export default class ConcatenatedModule extends Module {
7473
}
7574

7675
mergeNestedFolders() {
77-
invokeMap(this.children, 'mergeNestedFolders');
76+
Object.values(this.children).forEach(child => {
77+
if (child.mergeNestedFolders) {
78+
child.mergeNestedFolders();
79+
}
80+
});
7881
}
7982

8083
toChartData() {
8184
return {
8285
...super.toChartData(),
8386
concatenated: true,
84-
groups: invokeMap(this.children, 'toChartData')
87+
groups: Object.values(this.children).map(child => child.toChartData())
8588
};
8689
}
8790

0 commit comments

Comments
 (0)