Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unoptimized rule #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
...
}
}
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/rules/index.js
Original file line number Diff line number Diff line change
@@ -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
};
12 changes: 12 additions & 0 deletions src/rules/unoptimized/README.md
Original file line number Diff line number Diff line change
@@ -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:
25 changes: 25 additions & 0 deletions src/rules/unoptimized/index.js
Original file line number Diff line number Diff line change
@@ -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));
};
}