Skip to content

Commit 7d6b8c9

Browse files
fix: ignored file
1 parent 79fb44d commit 7d6b8c9

File tree

11 files changed

+85
-14
lines changed

11 files changed

+85
-14
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
2+
collectCoverageFrom: ['src/**/*.js'],
23
collectCoverage: true,
34
testEnvironment: 'node',
45
testTimeout: 60000,
5-
transformIgnorePatterns: ['node_modules/(?!(arrify)/)'],
66
};

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 {import('jest-worker').Worker & {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: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,46 @@ class StylelintWebpackPlugin {
8888
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
8989
/** @type {import('./linter').Linter} */
9090
let lint;
91+
92+
/** @type {import('stylelint').InternalApi} */
93+
let api;
94+
9195
/** @type {import('./linter').Reporter} */
9296
let report;
97+
9398
/** @type number */
9499
let threads;
95100

96101
try {
97-
({ lint, report, threads } = linter(this.key, options, compilation));
102+
({ lint, api, report, threads } = linter(
103+
this.key,
104+
options,
105+
compilation
106+
));
98107
} catch (e) {
99108
compilation.errors.push(e);
100109
return;
101110
}
102111

103-
compilation.hooks.finishModules.tap(this.key, () => {
104-
const files = compiler.modifiedFiles
105-
? Array.from(compiler.modifiedFiles).filter(
106-
(file) =>
107-
isMatch(file, wanted, { dot: true }) &&
108-
!isMatch(file, exclude, { dot: true })
109-
)
110-
: globby.sync(wanted, { dot: true, ignore: exclude });
112+
compilation.hooks.finishModules.tapPromise(this.key, async () => {
113+
const files = (
114+
await Promise.all(
115+
(compiler.modifiedFiles
116+
? Array.from(compiler.modifiedFiles).filter(
117+
(file) =>
118+
isMatch(file, wanted, { dot: true }) &&
119+
!isMatch(file, exclude, { dot: true })
120+
)
121+
: globby.sync(wanted, { dot: true, ignore: exclude })
122+
).map(async (/** @type {string | undefined} */ file) => {
123+
try {
124+
return (await api.isPathIgnored(file)) ? false : file;
125+
} catch (e) {
126+
return file;
127+
}
128+
})
129+
)
130+
).filter((file) => file !== false);
111131

112132
if (threads > 1) {
113133
for (const file of files) {

src/linter.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { arrify } from './utils';
66

77
/** @typedef {import('stylelint')} Stylelint */
88
/** @typedef {import('stylelint').LintResult} LintResult */
9+
/** @typedef {import('stylelint').InternalApi} InternalApi */
910
/** @typedef {import('webpack').Compiler} Compiler */
1011
/** @typedef {import('webpack').Compilation} Compilation */
1112
/** @typedef {import('./options').Options} Options */
@@ -24,12 +25,15 @@ const resultStorage = new WeakMap();
2425
* @param {string|undefined} key
2526
* @param {Options} options
2627
* @param {Compilation} compilation
27-
* @returns {{lint: Linter, report: Reporter, threads: number}}
28+
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
2829
*/
2930
export default function linter(key, options, compilation) {
3031
/** @type {Stylelint} */
3132
let stylelint;
3233

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

@@ -45,13 +49,17 @@ export default function linter(key, options, compilation) {
4549
const crossRunResultStorage = getResultStorage(compilation);
4650

4751
try {
48-
({ stylelint, lintFiles, cleanup, threads } = getStylelint(key, options));
52+
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
53+
key,
54+
options
55+
));
4956
} catch (e) {
5057
throw new StylelintError(e.message);
5158
}
5259

5360
return {
5461
lint,
62+
api,
5563
report,
5664
threads,
5765
};
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+
});

0 commit comments

Comments
 (0)