Skip to content

Commit

Permalink
refactor: avoid lodash in favor native implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Mar 5, 2019
1 parent 0ba8c66 commit c309788
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 40 deletions.
76 changes: 43 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
"dependencies": {
"icss-utils": "^4.1.0",
"loader-utils": "^1.2.3",
"lodash": "^4.17.11",
"camelcase": "^5.2.0",
"postcss": "^7.0.14",
"postcss-modules-extract-imports": "^2.0.0",
"postcss-modules-local-by-default": "^2.0.6",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -17,14 +17,14 @@ import {
getCurrentRequest,
stringifyRequest,
} from 'loader-utils';
import camelCase from 'lodash/camelCase';

import schema from './options.json';
import { importParser, icssParser, urlParser } from './plugins';
import {
getLocalIdent,
getImportPrefix,
placholderRegExps,
camelCase,
dashesCamelCase,
getFilter,
} from './utils';
Expand Down
28 changes: 23 additions & 5 deletions src/plugins/postcss-url-parser.js
@@ -1,6 +1,5 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import _ from 'lodash';

const pluginName = 'postcss-url-parser';

Expand Down Expand Up @@ -87,14 +86,29 @@ function walkDeclsWithUrl(css, result, filter) {
return items;
}

function uniqWith(array, comparator) {
return array.reduce(
(acc, d) => (!acc.some((item) => comparator(d, item)) ? [...acc, d] : acc),
[]
);
}

function flatten(array) {
return array.reduce((a, b) => a.concat(b), []);
}

function isEqual(value, other) {
return value.url === other.url && value.needQuotes === other.needQuotes;
}

export default postcss.plugin(
pluginName,
(options = {}) =>
function process(css, result) {
const traversed = walkDeclsWithUrl(css, result, options.filter);
const paths = _.uniqWith(
_.flatten(traversed.map((item) => item.urls)),
_.isEqual
const paths = uniqWith(
flatten(traversed.map((item) => item.urls)),
isEqual
);

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

traversed.forEach((item) => {
walkUrls(item.parsed, (node, url, needQuotes) => {
const value = _.find(placeholders, { path: { url, needQuotes } });
const value = placeholders.find(
(placeholder) =>
placeholder.path.url === url &&
placeholder.path.needQuotes === needQuotes
);

if (!value) {
return;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Expand Up @@ -4,6 +4,7 @@
*/
import path from 'path';

import cc from 'camelcase';
import loaderUtils from 'loader-utils';

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

function camelCase(str) {
return cc(str);
}

function dashesCamelCase(str) {
return str.replace(/-+(\w)/g, (match, firstLetter) =>
firstLetter.toUpperCase()
Expand Down Expand Up @@ -105,6 +110,7 @@ export {
getImportPrefix,
getLocalIdent,
placholderRegExps,
camelCase,
dashesCamelCase,
getFilter,
};

0 comments on commit c309788

Please sign in to comment.