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

feat: support .stylelintignore file #257

Closed
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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ A string indicating the root of your files.
### `exclude`

- Type: `String|Array[String]`
- Default: `['node_modules', compiler.options.output.path]`
- Default: `['node_modules', compiler.options.output.path, '<.stylelintignore content>']`

Specify the files and/or directories to exclude. Must be relative to `options.context`.

Expand Down
18 changes: 13 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { isMatch } from 'micromatch';

import { getOptions } from './options';
import linter from './linter';
import { arrify, parseFiles, parseFoldersToGlobs } from './utils';
import {
arrify,
parseFiles,
parseFoldersToGlobs,
parseStyleLintIgnoreFile,
} from './utils';

/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Module} Module */
Expand Down Expand Up @@ -70,10 +75,13 @@ class StylelintWebpackPlugin {
}

const context = this.getContext(compiler);
const excludeDefault = [
'**/node_modules/**',
String(compiler.options.output.path),
];
const excludeDefault = Array.from(
new Set([
'**/node_modules/**',
String(compiler.options.output.path),
...parseStyleLintIgnoreFile(context),
])
);

const options = {
...this.options,
Expand Down
23 changes: 22 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'path';
import { statSync } from 'fs';
import { statSync, existsSync, readFileSync } from 'fs';

// @ts-ignore
import normalizePath from 'normalize-path';
Expand Down Expand Up @@ -110,3 +110,24 @@ export const jsonStringifyReplacerSortKeys = (_, value) => {
? Object.keys(value).sort().reduce(insert, {})
: value;
};

/**
* @param {string} context
* @returns {string[]}
*/
export const parseStyleLintIgnoreFile = (context) => {
const ignoreFilePath = resolve(context, '.stylelintignore');

if (!existsSync(ignoreFilePath)) {
return [];
}

const ignoreContent = readFileSync(ignoreFilePath, 'utf8').split('\n');

return ignoreContent.filter((lineRaw) => {
const line = lineRaw.trim();

// remove empty and comment line
return line !== '' && !line.startsWith('#');
});
};
7 changes: 7 additions & 0 deletions test/fixtures/stylelintignore/.stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ignore.scss

# comment

noop.scss

ignore.scss
3 changes: 3 additions & 0 deletions test/fixtures/stylelintignore/ignore.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#stuff {
display: 'block'; // error
}
2 changes: 2 additions & 0 deletions test/fixtures/stylelintignore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('file-loader!./test.scss');
require('file-loader!./ignore.scss');
3 changes: 3 additions & 0 deletions test/fixtures/stylelintignore/test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
display: block;
}
41 changes: 41 additions & 0 deletions test/stylelint-ignore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { join } from 'path';

import webpack from 'webpack';

import StylelintWebpackPlugin from '../src';

describe('stylelintignore', () => {
it('discern stylelintignore', (done) => {
const compiler = webpack({
context: join(__dirname, 'fixtures', 'stylelintignore'),
mode: 'development',
entry: './index',
plugins: [new StylelintWebpackPlugin()],
});

compiler.run((err, stats) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
});

it('not use .stylelintignore file', (done) => {
const compiler = webpack({
context: join(__dirname, 'fixtures', 'stylelintignore'),
mode: 'development',
entry: './index',
plugins: [
new StylelintWebpackPlugin({
exclude: [],
}),
],
});

compiler.run((err, stats) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
done();
});
});
});