Skip to content

Commit 999efe8

Browse files
fix: ignore file (#282)
1 parent 1ccd8bf commit 999efe8

File tree

12 files changed

+86
-8
lines changed

12 files changed

+86
-8
lines changed

src/getStylelint.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ const cache = {};
1515
/** @typedef {import('./options').Options} Options */
1616
/** @typedef {() => Promise<void>} AsyncTask */
1717
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
18-
/** @typedef {{stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
18+
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
1919
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
2020

2121
/**
2222
* @param {Options} options
2323
* @returns {Linter}
2424
*/
2525
function loadStylelint(options) {
26-
const stylelint = setup(options, getStylelintOptions(options));
26+
const stylelintOptions = getStylelintOptions(options);
27+
const stylelint = setup(options, stylelintOptions);
2728

2829
return {
2930
stylelint,
31+
api: stylelint.createLinter(stylelintOptions),
3032
lintFiles,
3133
cleanup: async () => {},
3234
threads: 1,

src/index.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,43 @@ class StylelintWebpackPlugin {
9595
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
9696
/** @type {import('./linter').Linter} */
9797
let lint;
98+
99+
/** @type {import('stylelint').InternalApi} */
100+
let api;
101+
98102
/** @type {import('./linter').Reporter} */
99103
let report;
104+
100105
/** @type number */
101106
let threads;
102107

103108
try {
104-
({ lint, report, threads } = linter(this.key, options, compilation));
109+
({ lint, api, report, threads } = linter(
110+
this.key,
111+
options,
112+
compilation
113+
));
105114
} catch (e) {
106115
compilation.errors.push(e);
107116
return;
108117
}
109118

110-
compilation.hooks.finishModules.tap(this.key, () => {
111-
const files = this.getFiles(compiler, wanted, exclude);
119+
compilation.hooks.finishModules.tapPromise(this.key, async () => {
120+
/** @type {string[]} */
121+
// @ts-ignore
122+
const files = (
123+
await Promise.all(
124+
this.getFiles(compiler, wanted, exclude).map(
125+
async (/** @type {string | undefined} */ file) => {
126+
try {
127+
return (await api.isPathIgnored(file)) ? false : file;
128+
} catch (e) {
129+
return file;
130+
}
131+
}
132+
)
133+
)
134+
).filter((file) => file !== false);
112135

113136
if (threads > 1) {
114137
for (const file of files) {

src/linter.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const getStylelint = require('./getStylelint');
77

88
/** @typedef {import('stylelint')} Stylelint */
99
/** @typedef {import('stylelint').LintResult} LintResult */
10+
/** @typedef {import('stylelint').InternalApi} InternalApi */
1011
/** @typedef {import('webpack').Compiler} Compiler */
1112
/** @typedef {import('webpack').Compilation} Compilation */
1213
/** @typedef {import('./options').Options} Options */
@@ -25,12 +26,15 @@ const resultStorage = new WeakMap();
2526
* @param {string|undefined} key
2627
* @param {Options} options
2728
* @param {Compilation} compilation
28-
* @returns {{lint: Linter, report: Reporter, threads: number}}
29+
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
2930
*/
3031
function linter(key, options, compilation) {
3132
/** @type {Stylelint} */
3233
let stylelint;
3334

35+
/** @type {InternalApi} */
36+
let api;
37+
3438
/** @type {(files: string|string[]) => Promise<LintResult[]>} */
3539
let lintFiles;
3640

@@ -46,13 +50,17 @@ function linter(key, options, compilation) {
4650
const crossRunResultStorage = getResultStorage(compilation);
4751

4852
try {
49-
({ stylelint, lintFiles, cleanup, threads } = getStylelint(key, options));
53+
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
54+
key,
55+
options
56+
));
5057
} catch (e) {
5158
throw new StylelintError(e.message);
5259
}
5360

5461
return {
5562
lint,
63+
api,
5664
report,
5765
threads,
5866
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ignore.scss
2+
3+
# comment
4+
5+
noop.scss
6+
7+
ignore.scss
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#stuff {
2+
display: "block"; // error
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('file-loader!./test.scss');
2+
require('file-loader!./ignore.scss');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
display: block;
3+
}

test/mock/stylelint/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ module.exports = {
2424
],
2525
};
2626
},
27+
28+
createLinter() {
29+
return {
30+
isPathIgnored() {
31+
return false;
32+
},
33+
};
34+
},
2735
};

test/stylelint-ignore.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pack from './utils/pack';
2+
3+
describe('stylelint ignore', () => {
4+
it('should ignore file', (done) => {
5+
const compiler = pack('stylelintignore');
6+
7+
compiler.run((err, stats) => {
8+
expect(stats.hasWarnings()).toBe(false);
9+
expect(stats.hasErrors()).toBe(false);
10+
done();
11+
});
12+
});
13+
});

test/stylelint-lint.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe('stylelint lint', () => {
99
jest.mock('stylelint', () => {
1010
return {
1111
lint: mockLintFiles,
12+
createLinter: () => {
13+
return {
14+
isPathIgnored: () => false,
15+
};
16+
},
1217
};
1318
});
1419
});

0 commit comments

Comments
 (0)