Skip to content

Commit 08a2a1f

Browse files
fix: path ignored
1 parent 7d634cf commit 08a2a1f

File tree

12 files changed

+65
-33
lines changed

12 files changed

+65
-33
lines changed

src/getStylelint.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const cache = {};
1313
/** @typedef {import('stylelint')} Stylelint */
1414
/** @typedef {import('stylelint').LintResult} LintResult */
1515
/** @typedef {import('./options').Options} Options */
16+
/** @typedef {(stylelint: Stylelint, filePath: string) => Promise<boolean>} isPathIgnored */
1617
/** @typedef {() => Promise<void>} AsyncTask */
1718
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
18-
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
19+
/** @typedef {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lintFiles: LintTask, cleanup: AsyncTask, threads: number }} Linter */
1920
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
2021

2122
/**
@@ -26,9 +27,23 @@ function loadStylelint(options) {
2627
const stylelintOptions = getStylelintOptions(options);
2728
const stylelint = setup(options, stylelintOptions);
2829

30+
/** @type {isPathIgnored} */
31+
let isPathIgnored;
32+
33+
try {
34+
isPathIgnored = require(`${options.stylelintPath}/lib/isPathIgnored`);
35+
} catch (e) {
36+
try {
37+
// @ts-ignore
38+
isPathIgnored = require('stylelint/lib/isPathIgnored');
39+
} catch (_) {
40+
isPathIgnored = () => Promise.resolve(false);
41+
}
42+
}
43+
2944
return {
3045
stylelint,
31-
api: stylelint.createLinter(stylelintOptions),
46+
isPathIgnored,
3247
lintFiles,
3348
cleanup: async () => {},
3449
threads: 1,

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ class StylelintWebpackPlugin {
9090
}
9191

9292
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
93+
/** @type {import('stylelint')} */
94+
let stylelint;
95+
9396
/** @type {import('./linter').Linter} */
9497
let lint;
9598

96-
/** @type {import('stylelint').InternalApi} */
97-
let api;
99+
/** @type {import('./linter').isPathIgnored} */
100+
let isPathIgnored;
98101

99102
/** @type {import('./linter').Reporter} */
100103
let report;
@@ -103,7 +106,7 @@ class StylelintWebpackPlugin {
103106
let threads;
104107

105108
try {
106-
({ lint, api, report, threads } = linter(
109+
({ stylelint, lint, isPathIgnored, report, threads } = linter(
107110
this.key,
108111
options,
109112
compilation
@@ -127,7 +130,7 @@ class StylelintWebpackPlugin {
127130
: globby.sync(wanted, { dot: true, ignore: exclude })
128131
).map(async (file) => {
129132
try {
130-
return (await api.isPathIgnored(file)) ? false : file;
133+
return (await isPathIgnored(stylelint, file)) ? false : file;
131134
} catch (e) {
132135
return file;
133136
}

src/linter.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const { arrify } = require('./utils');
77
/** @typedef {import('stylelint')} Stylelint */
88
/** @typedef {import('stylelint').LintResult} LintResult */
99
/** @typedef {import('stylelint').LinterResult} LinterResult */
10-
/** @typedef {import('stylelint').InternalApi} InternalApi */
1110
/** @typedef {import('stylelint').Formatter} Formatter */
1211
/** @typedef {import('stylelint').FormatterType} FormatterType */
1312
/** @typedef {import('webpack').Compiler} Compiler */
1413
/** @typedef {import('webpack').Compilation} Compilation */
1514
/** @typedef {import('./options').Options} Options */
15+
/** @typedef {import('./getStylelint').isPathIgnored} isPathIgnored */
1616
/** @typedef {(compilation: Compilation) => Promise<void>} GenerateReport */
1717
/** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport}} Report */
1818
/** @typedef {() => Promise<Report>} Reporter */
@@ -26,14 +26,14 @@ const resultStorage = new WeakMap();
2626
* @param {string|undefined} key
2727
* @param {Options} options
2828
* @param {Compilation} compilation
29-
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
29+
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
3030
*/
3131
function linter(key, options, compilation) {
3232
/** @type {Stylelint} */
3333
let stylelint;
3434

35-
/** @type {InternalApi} */
36-
let api;
35+
/** @type {isPathIgnored} */
36+
let isPathIgnored;
3737

3838
/** @type {(files: string|string[]) => Promise<LintResult[]>} */
3939
let lintFiles;
@@ -50,7 +50,7 @@ function linter(key, options, compilation) {
5050
const crossRunResultStorage = getResultStorage(compilation);
5151

5252
try {
53-
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
53+
({ stylelint, isPathIgnored, lintFiles, cleanup, threads } = getStylelint(
5454
key,
5555
options
5656
));
@@ -59,8 +59,9 @@ function linter(key, options, compilation) {
5959
}
6060

6161
return {
62+
stylelint,
6263
lint,
63-
api,
64+
isPathIgnored,
6465
report,
6566
threads,
6667
};
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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,4 @@ module.exports = {
2424
],
2525
};
2626
},
27-
28-
createLinter() {
29-
return {
30-
isPathIgnored() {
31-
return false;
32-
},
33-
};
34-
},
3527
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {import('stylelint')} stylelint
3+
* @param {string} filePath
4+
* @returns {Promise<boolean>}
5+
*/
6+
function isPathIgnored(stylelint, filePath) {
7+
return new Promise((resolve) => {
8+
resolve(filePath.endsWith('ignore.scss'));
9+
});
10+
}
11+
12+
module.exports = isPathIgnored;

test/stylelint-lint.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ describe('stylelint lint', () => {
99
jest.mock('stylelint', () => {
1010
return {
1111
lint: mockLintFiles,
12-
createLinter: () => {
13-
return {
14-
isPathIgnored: () => false,
15-
};
16-
},
1712
};
1813
});
14+
15+
jest.mock('stylelint/lib/isPathIgnored', () => {
16+
throw new Error();
17+
});
1918
});
2019

2120
beforeEach(() => {

test/stylelint-path.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import pack from './utils/pack';
55
describe('stylelint path', () => {
66
it('should use another instance of stylelint via stylelintPath config', (done) => {
77
const stylelintPath = join(__dirname, 'mock/stylelint');
8-
const compiler = pack('good', { stylelintPath });
8+
const compiler = pack('stylelint-path', { stylelintPath });
99

1010
compiler.run((err, stats) => {
1111
expect(err).toBeNull();

0 commit comments

Comments
 (0)