From 9fcd8a90c7283a13ad7ec51b1940697688ee984a Mon Sep 17 00:00:00 2001 From: Ramiro Silveyra d'Avila Date: Mon, 10 Apr 2017 23:43:59 -0300 Subject: [PATCH] Add unoptimized rule --- README.md | 2 ++ src/rules/index.js | 4 +++- src/rules/unoptimized/README.md | 12 ++++++++++++ src/rules/unoptimized/index.js | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/rules/unoptimized/README.md create mode 100644 src/rules/unoptimized/index.js diff --git a/README.md b/README.md index ecab970..cd20213 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Create the `.stylelintrc.json` config file (or open the existing one), add `styl "rules": { "images/broken": true, "images/prefer-data-uri": 256, + "images/unoptimized": true, ... } } @@ -53,6 +54,7 @@ Please refer to [stylelint docs](http://stylelint.io/user-guide/) for the detail - [`broken`](./src/rules/broken/README.md): Checks if the images are broken. - [`prefer-data-uri`](./src/rules/prefer-data-uri/README.md): Suggest using data-URIs instead of an external image if their file size (in bytes) exceeds the limit. +- [`unoptimized`](./src/rules/unoptimized/README.md): Checks if the images are unoptimized. ## Contribute diff --git a/src/rules/index.js b/src/rules/index.js index cfc0eff..75ff5a0 100644 --- a/src/rules/index.js +++ b/src/rules/index.js @@ -1,7 +1,9 @@ import broken from './broken'; import preferDataURI from './prefer-data-uri'; +import unoptimized from './unoptimized'; export default { broken, - 'prefer-data-uri': preferDataURI + 'prefer-data-uri': preferDataURI, + unoptimized }; diff --git a/src/rules/unoptimized/README.md b/src/rules/unoptimized/README.md new file mode 100644 index 0000000..750fa4f --- /dev/null +++ b/src/rules/unoptimized/README.md @@ -0,0 +1,12 @@ +# unoptimized + +Checks if the images are unoptimized (uses [imagemin](https://github.com/imagemin/imagemin)). + +## Options + +### `true` + +The following patterns are considered warnings: + + +The following patterns are not considered warnings: diff --git a/src/rules/unoptimized/index.js b/src/rules/unoptimized/index.js new file mode 100644 index 0000000..bdbebd1 --- /dev/null +++ b/src/rules/unoptimized/index.js @@ -0,0 +1,25 @@ +import { utils } from 'stylelint'; +import { namespace, generateListOfImagesURLsAndNodes, getImage } from '../../utils'; + +export const ruleName = namespace('unoptimized'); +export const messages = utils.ruleMessages(ruleName, { + unexpected: imageURL => `Unexpected broken image "${imageURL}"` +}); + +export default function unoptimizedRule(enabled) { + return (root, result) => { + const validOptions = utils.validateOptions(result, ruleName, { + actual: enabled, + possible: [true, false] + }); + + if (!validOptions) { + return null; + } + + const list = generateListOfImagesURLsAndNodes(root); + + return checkIfImagesAreUnoptimized(list, result) + .then(results => reportUnoptimizedImages(results, result)); + }; +}