Replace lodash with native methods and use its packages directly.

Dropped:

* `_.forEach`
* `_.map`
* `_.reduce`
This commit is contained in:
XhmikosR 2019-11-03 15:23:08 +02:00
parent 48db3d8c2e
commit d52be4228f
5 changed files with 73 additions and 45 deletions

79
cli.js
View File

@ -9,7 +9,8 @@ const chalk = require('chalk');
const indentString = require('indent-string');
const stdin = require('get-stdin');
const css = require('css');
const _ = require('lodash');
const escapeRegExp = require('lodash.escaperegexp');
const defaults = require('lodash.defaults');
const inlineCritical = require('.');
let ok;
@ -68,49 +69,45 @@ const cli = meow(help, {
});
// Cleanup cli flags
cli.flags = _.reduce(
cli.flags,
(res, val, key) => {
if (key.length <= 1) {
return res;
}
cli.flags = Object.entries(cli.flags).reduce((res, [key, val]) => {
if (key.length <= 1) {
return res;
}
switch (key) {
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (_) {}
switch (key) {
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (_) {}
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (!Array.isArray(val)) {
val = [val];
}
res.ignore = (val || []).map(ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(escapeRegExp(match[1]), match[2]);
}
res.ignore = _.map(val || [], ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
return ignore;
});
break;
default:
res[key] = val;
break;
}
if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
return ignore;
});
break;
default:
res[key] = val;
break;
}
return res;
},
{}
);
return res;
}, {});
function processError(err) {
process.stderr.write(chalk.red(indentString(`Error: ${err.message || err}`, 2)));
@ -128,7 +125,7 @@ function read(file) {
}
function run(data) {
const opts = _.defaults(cli.flags, {basePath: process.cwd()});
const opts = defaults(cli.flags, {basePath: process.cwd()});
ok = true;
if (data) {
@ -141,7 +138,7 @@ function run(data) {
}
}
_.forEach(cli.input, file => {
(cli.input || []).forEach(file => {
const tmp = read(file);
try {
css.parse(tmp);

View File

@ -13,8 +13,8 @@
const fs = require('fs');
const path = require('path');
const isString = require('lodash/isString');
const isRegExp = require('lodash/isRegExp');
const isString = require('lodash.isstring');
const isRegExp = require('lodash.isregexp');
const reaver = require('reaver');
const slash = require('slash');

20
package-lock.json generated
View File

@ -4966,12 +4966,22 @@
"integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=",
"dev": true
},
"lodash.defaults": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
"integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw="
},
"lodash.defaultsdeep": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/lodash.defaultsdeep/-/lodash.defaultsdeep-4.6.1.tgz",
"integrity": "sha512-3j8wdDzYuWO3lM3Reg03MuQR957t287Rpcxp1njpEa8oDrikb+FwGdW3n+FELh/A6qib6yPit0j/pv9G/yeAqA==",
"dev": true
},
"lodash.escaperegexp": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz",
"integrity": "sha1-ZHYsSGGAglGKw99Mz11YhtriA0c="
},
"lodash.get": {
"version": "4.4.2",
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
@ -4984,6 +4994,16 @@
"integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA=",
"dev": true
},
"lodash.isregexp": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isregexp/-/lodash.isregexp-4.0.1.tgz",
"integrity": "sha1-4T5kezDNVZdSoEzZEghvr32hwws="
},
"lodash.isstring": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
"integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
},
"lodash.kebabcase": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz",

View File

@ -39,7 +39,10 @@
"get-stdin": "^7.0.0",
"indent-string": "^4.0.0",
"jsdom": "^15.2.0",
"lodash": "^4.17.15",
"lodash.defaults": "^4.2.0",
"lodash.escaperegexp": "^4.1.2",
"lodash.isregexp": "^4.0.1",
"lodash.isstring": "^4.0.1",
"meow": "^5.0.0",
"normalize-newline": "^3.0.0",
"postcss": "^7.0.18",

View File

@ -5,7 +5,6 @@ const path = require('path');
const {JSDOM} = require('jsdom');
const resolve = require('resolve');
const detectIndent = require('detect-indent');
const flatten = require('lodash/flatten');
const UglifyJS = require('uglify-js');
/**
@ -21,6 +20,15 @@ function getScript() {
return UglifyJS.minify(loadCSS).code.trim();
}
/**
* Flattens an array
* @param {array} arr Input Array
* @returns {array} Flattened Array
*/
function flatten(arr) {
return arr.reduce((a, b) => a.concat(b), []);
}
/**
* Get all subsctings of of the passed tags
* Does not work with self closing tags