Skip to content

Commit c309788

Browse files
refactor: avoid lodash in favor native implementations
1 parent 0ba8c66 commit c309788

File tree

5 files changed

+74
-40
lines changed

5 files changed

+74
-40
lines changed

package-lock.json

+43-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"dependencies": {
4545
"icss-utils": "^4.1.0",
4646
"loader-utils": "^1.2.3",
47-
"lodash": "^4.17.11",
47+
"camelcase": "^5.2.0",
4848
"postcss": "^7.0.14",
4949
"postcss-modules-extract-imports": "^2.0.0",
5050
"postcss-modules-local-by-default": "^2.0.6",

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import {
1717
getCurrentRequest,
1818
stringifyRequest,
1919
} from 'loader-utils';
20-
import camelCase from 'lodash/camelCase';
2120

2221
import schema from './options.json';
2322
import { importParser, icssParser, urlParser } from './plugins';
2423
import {
2524
getLocalIdent,
2625
getImportPrefix,
2726
placholderRegExps,
27+
camelCase,
2828
dashesCamelCase,
2929
getFilter,
3030
} from './utils';

src/plugins/postcss-url-parser.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
3-
import _ from 'lodash';
43

54
const pluginName = 'postcss-url-parser';
65

@@ -87,14 +86,29 @@ function walkDeclsWithUrl(css, result, filter) {
8786
return items;
8887
}
8988

89+
function uniqWith(array, comparator) {
90+
return array.reduce(
91+
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
92+
[]
93+
);
94+
}
95+
96+
function flatten(array) {
97+
return array.reduce((a, b) => a.concat(b), []);
98+
}
99+
100+
function isEqual(value, other) {
101+
return value.url === other.url && value.needQuotes === other.needQuotes;
102+
}
103+
90104
export default postcss.plugin(
91105
pluginName,
92106
(options = {}) =>
93107
function process(css, result) {
94108
const traversed = walkDeclsWithUrl(css, result, options.filter);
95-
const paths = _.uniqWith(
96-
_.flatten(traversed.map((item) => item.urls)),
97-
_.isEqual
109+
const paths = uniqWith(
110+
flatten(traversed.map((item) => item.urls)),
111+
isEqual
98112
);
99113

100114
if (paths.length === 0) {
@@ -118,7 +132,11 @@ export default postcss.plugin(
118132

119133
traversed.forEach((item) => {
120134
walkUrls(item.parsed, (node, url, needQuotes) => {
121-
const value = _.find(placeholders, { path: { url, needQuotes } });
135+
const value = placeholders.find(
136+
(placeholder) =>
137+
placeholder.path.url === url &&
138+
placeholder.path.needQuotes === needQuotes
139+
);
122140

123141
if (!value) {
124142
return;

src/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import path from 'path';
66

7+
import cc from 'camelcase';
78
import loaderUtils from 'loader-utils';
89

910
/* eslint-disable line-comment-position */
@@ -30,6 +31,10 @@ function getImportPrefix(loaderContext, importLoaders) {
3031
return `-!${loadersRequest}!`;
3132
}
3233

34+
function camelCase(str) {
35+
return cc(str);
36+
}
37+
3338
function dashesCamelCase(str) {
3439
return str.replace(/-+(\w)/g, (match, firstLetter) =>
3540
firstLetter.toUpperCase()
@@ -105,6 +110,7 @@ export {
105110
getImportPrefix,
106111
getLocalIdent,
107112
placholderRegExps,
113+
camelCase,
108114
dashesCamelCase,
109115
getFilter,
110116
};

0 commit comments

Comments
 (0)