Skip to content

Commit faed359

Browse files
authored
Merge pull request #130 from Genarito/remove-lodash-optimizations
Remove lodash dependencies + Optimization
2 parents 4d0a2f8 + 10e309e commit faed359

File tree

6 files changed

+47
-62
lines changed

6 files changed

+47
-62
lines changed

lib/index.js

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// @ts-check
2-
/** @typedef {import("lodash.defaults")} defaults */
3-
/** @typedef {import("lodash.assign")} assign */
42
/** @typedef {import("webpack").Compiler} Compiler */
53
/** @typedef {import("webpack").Stats} Stats */
64
/** @typedef {import("webpack").compilation.Compilation} Compilation */
@@ -13,11 +11,6 @@ const path = require('path');
1311
const fs = require('fs');
1412
const crypto = require('crypto');
1513

16-
const defaults = require('lodash.defaults');
17-
const assign = require('lodash.assign');
18-
const each = require('lodash.foreach');
19-
const fromPairs = require('lodash.frompairs');
20-
const toPairs = require('lodash.topairs');
2114
const stripAnsi = require('./utils/stripAnsi');
2215

2316
function getAssetPath(compilation, name) {
@@ -32,16 +25,19 @@ function getSource(compilation, name) {
3225
/**
3326
* Merges the provided objects, ensuring that the resulting object has its properties in sorted order.
3427
* @template T
35-
* @param {T} obj1
36-
* @param {Partial<T> | undefined} obj2
37-
* @returns {T}
28+
* @param {T} obj1 First object to merge.
29+
* @param {Partial<T> | undefined} obj2 Second object to merge, can be undefined.
30+
* @returns {*} A new object containing the merged properties of obj1 and obj2, with keys sorted.
3831
*/
39-
function mergeObjects(obj1, obj2) {
40-
const mergedObj = assign({}, obj1, obj2);
41-
const sortedPairs = toPairs(mergedObj).sort((e1, e2) => e1[0].localeCompare(e2[0]));
42-
// @ts-ignore: 2322 The Lodash typedefs aren't smart enough to be able to tell TS that we're
43-
// regenerating the object from the original key-value pairs.
44-
return fromPairs(sortedPairs);
32+
function mergeObjectsAndSortKeys(obj1, obj2) {
33+
const mergedObj = Object.assign({}, obj1, obj2);
34+
35+
// Generates a new object with the same keys and values as mergedObj but in sorted order
36+
const sortedKeys = Object.keys(mergedObj).sort();
37+
return sortedKeys.reduce((acc, key) => {
38+
acc[key] = mergedObj[key];
39+
return acc;
40+
}, {});
4541
}
4642

4743
class BundleTrackerPlugin {
@@ -70,17 +66,21 @@ class BundleTrackerPlugin {
7066
* @returns this
7167
*/
7268
_setParamsFromCompiler(compiler) {
73-
this.options = defaults({}, this.options, {
74-
path: compiler.options.output?.path ?? process.cwd(),
75-
publicPath: compiler.options.output?.publicPath ?? '',
76-
filename: 'webpack-stats.json',
77-
logTime: false,
78-
relativePath: false,
79-
integrity: false,
80-
indent: 2,
81-
// https://www.w3.org/TR/SRI/#cryptographic-hash-functions
82-
integrityHashes: ['sha256', 'sha384', 'sha512'],
83-
});
69+
this.options = Object.assign(
70+
{},
71+
{
72+
path: compiler.options.output?.path ?? process.cwd(),
73+
publicPath: compiler.options.output?.publicPath ?? '',
74+
filename: 'webpack-stats.json',
75+
logTime: false,
76+
relativePath: false,
77+
integrity: false,
78+
indent: 2,
79+
// https://www.w3.org/TR/SRI/#cryptographic-hash-functions
80+
integrityHashes: ['sha256', 'sha384', 'sha512'],
81+
},
82+
this.options,
83+
);
8484

8585
if (this.options.filename?.includes('/')) {
8686
throw Error(
@@ -91,7 +91,9 @@ class BundleTrackerPlugin {
9191
}
9292

9393
// Set output directories
94-
this.outputChunkDir = path.resolve(compiler.options.output?.path ?? process.cwd());
94+
const outputPath = compiler.options.output?.path ?? process.cwd();
95+
this.outputChunkDir = path.resolve(outputPath);
96+
9597
// @ts-ignore: TS2345 this.options.path can't be undefined here because we set a default value above
9698
// @ts-ignore: TS2345 this.options.filename can't be undefined here because we set a default value above
9799
this.outputTrackerFile = path.resolve(path.join(this.options.path, this.options.filename));
@@ -102,13 +104,13 @@ class BundleTrackerPlugin {
102104
/**
103105
* Write bundle tracker stats file
104106
*
105-
* @param {Compiler} compiler
107+
* @param {Compiler} _compiler
106108
* @param {Partial<Contents>} contents
107109
*/
108-
_writeOutput(compiler, contents) {
109-
assign(this.contents, contents, {
110-
assets: mergeObjects(this.contents.assets, contents.assets),
111-
chunks: mergeObjects(this.contents.chunks, contents.chunks),
110+
_writeOutput(_compiler, contents) {
111+
Object.assign(this.contents, contents, {
112+
assets: mergeObjectsAndSortKeys(this.contents.assets, contents.assets),
113+
chunks: mergeObjectsAndSortKeys(this.contents.chunks, contents.chunks),
112114
});
113115

114116
if (this.options.publicPath) {
@@ -159,7 +161,7 @@ class BundleTrackerPlugin {
159161
const error = findError(stats.compilation);
160162
this._writeOutput(compiler, {
161163
status: 'error',
162-
error: error.name ?? 'unknown-error',
164+
error: error?.name ?? 'unknown-error',
163165
message: stripAnsi(error['message']),
164166
});
165167

@@ -168,7 +170,7 @@ class BundleTrackerPlugin {
168170

169171
/** @type {Contents} */
170172
const output = { status: 'done', assets: {}, chunks: {} };
171-
each(stats.compilation.assets, (file, assetName) => {
173+
Object.entries(stats.compilation.assets).map(([assetName, _]) => {
172174
const fileInfo = {
173175
name: assetName,
174176
path: getAssetPath(stats.compilation, assetName),
@@ -198,7 +200,7 @@ class BundleTrackerPlugin {
198200

199201
output.assets[assetName] = fileInfo;
200202
});
201-
each(stats.compilation.chunkGroups, chunkGroup => {
203+
stats.compilation.chunkGroups.forEach(chunkGroup => {
202204
if (!chunkGroup.isInitial()) return;
203205

204206
output.chunks[chunkGroup.name] = chunkGroup.getFiles();

lib/utils/stripAnsi.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* This code is based on the strip-ansi library by Chalk.
33
* Source: https://github.com/chalk/strip-ansi
44
*/
5-
65
function ansiRegex({ onlyFirst = false } = {}) {
76
const pattern = [
87
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',

package.json

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,26 @@
2727
"pretty": "prettier --loglevel warn --write lib/*.js tests/*.js",
2828
"pretty-lint": "prettier --check lib/*.js tests/*.js",
2929
"pretest": "npm run pretty-lint",
30-
"test": "NODE_OPTIONS=--openssl-legacy-provider jest --runInBand --env node",
31-
"test-debug": "NODE_OPTIONS=--openssl-legacy-provider node --inspect-brk=0.0.0.0 node_modules/jest/bin/jest --runInBand --env node",
30+
"test": "cross-env NODE_OPTIONS=--openssl-legacy-provider jest --runInBand --env node",
31+
"test-debug": "cross-env NODE_OPTIONS=--openssl-legacy-provider node --inspect-brk=0.0.0.0 node_modules/jest/bin/jest --runInBand --env node",
3232
"posttest": "tsc",
3333
"test-watch": "jest --runInBand --env node --watchAll",
34-
"ci": "NODE_OPTIONS=--openssl-legacy-provider npm run pretest && jest --runInBand --coverage --env node && npm run posttest"
34+
"ci": "cross-env NODE_OPTIONS=--openssl-legacy-provider npm run pretest && jest --runInBand --coverage --env node && npm run posttest"
3535
},
3636
"jest": {
3737
"setupFilesAfterEnv": [
3838
"jest-extended"
3939
]
4040
},
41-
"dependencies": {
42-
"lodash.assign": "^4.2.0",
43-
"lodash.defaults": "^4.2.0",
44-
"lodash.foreach": "^4.5.0",
45-
"lodash.frompairs": "^4.0.1",
46-
"lodash.topairs": "^4.3.0"
47-
},
4841
"devDependencies": {
4942
"@types/babel__traverse": "7.0.6",
50-
"@types/lodash": "4.14.173",
51-
"@types/lodash.assign": "^4.2.7",
52-
"@types/lodash.defaults": "^4.2.7",
53-
"@types/lodash.foreach": "^4.5.7",
54-
"@types/lodash.frompairs": "^4.0.7",
55-
"@types/lodash.topairs": "^4.3.7",
5643
"@types/node": "^13.13.52",
5744
"@types/webpack": "^4.41.33",
5845
"@typescript-eslint/eslint-plugin": "^2.34.0",
5946
"@typescript-eslint/parser": "^2.34.0",
6047
"commitizen": "^4.3.0",
6148
"compression-webpack-plugin": "^6.1.1",
49+
"cross-env": "^7.0.3",
6250
"css-loader": "^5.2.7",
6351
"cz-conventional-changelog": "3.3.0",
6452
"eslint": "^6.8.0",

tests/base.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
'use strict';
33

44
const fs = require('fs');
5-
const toPairs = require('lodash.topairs');
65
const zlib = require('zlib');
76
const path = require('path');
87
const rimraf = require('rimraf');
@@ -761,8 +760,8 @@ describe('BundleTrackerPlugin bases tests', () => {
761760
() => {
762761
const statsStr = fs.readFileSync(path.join(OUTPUT_DIR, 'webpack-stats.json'), 'utf8');
763762
const stats = JSON.parse(statsStr);
764-
const assetsKeys = toPairs(stats.assets).map(pair => pair[0]);
765-
const chunksKeys = toPairs(stats.chunks).map(pair => pair[0]);
763+
const assetsKeys = Object.entries(stats.assets).map(pair => pair[0]);
764+
const chunksKeys = Object.entries(stats.chunks).map(pair => pair[0]);
766765

767766
expect(assetsKeys).toEqual(['css/appA.css', 'js/1.js', 'js/appA.js', 'js/appZ.js', 'js/commons.js']);
768767
expect(chunksKeys).toEqual(['appA', 'appZ']);

tests/fixtures/commons.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22

3-
const assign = require('lodash.assign');
4-
53
const output = { name: '' };
64

7-
assign(output, { name: 'common' });
5+
Object.assign(output, { name: 'common' });
86

97
module.exports = output;

tests/webpack5.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
'use strict';
33

44
const fs = require('fs');
5-
const toPairs = require('lodash.topairs');
65
const zlib = require('zlib');
76
const path = require('path');
87
const rimraf = require('rimraf');
@@ -807,8 +806,8 @@ describe('BundleTrackerPlugin bases tests', () => {
807806
() => {
808807
const statsStr = fs.readFileSync(path.join(OUTPUT_DIR, 'webpack-stats.json'), 'utf8');
809808
const stats = JSON.parse(statsStr);
810-
const assetsKeys = toPairs(stats.assets).map(pair => pair[0]);
811-
const chunksKeys = toPairs(stats.chunks).map(pair => pair[0]);
809+
const assetsKeys = Object.entries(stats.assets).map(pair => pair[0]);
810+
const chunksKeys = Object.entries(stats.chunks).map(pair => pair[0]);
812811

813812
expect(assetsKeys).toEqual(['css/appA.css', 'js/75.js', 'js/appA.js', 'js/appZ.js', 'js/commons.js']);
814813
expect(chunksKeys).toEqual(['appA', 'appZ']);

0 commit comments

Comments
 (0)