Skip to content

Commit

Permalink
fix: ignore file (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza authored May 20, 2022
1 parent 1ccd8bf commit 999efe8
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/getStylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ const cache = {};
/** @typedef {import('./options').Options} Options */
/** @typedef {() => Promise<void>} AsyncTask */
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
/** @typedef {{stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */

/**
* @param {Options} options
* @returns {Linter}
*/
function loadStylelint(options) {
const stylelint = setup(options, getStylelintOptions(options));
const stylelintOptions = getStylelintOptions(options);
const stylelint = setup(options, stylelintOptions);

return {
stylelint,
api: stylelint.createLinter(stylelintOptions),
lintFiles,
cleanup: async () => {},
threads: 1,
Expand Down
29 changes: 26 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,43 @@ class StylelintWebpackPlugin {
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
/** @type {import('./linter').Linter} */
let lint;

/** @type {import('stylelint').InternalApi} */
let api;

/** @type {import('./linter').Reporter} */
let report;

/** @type number */
let threads;

try {
({ lint, report, threads } = linter(this.key, options, compilation));
({ lint, api, report, threads } = linter(
this.key,
options,
compilation
));
} catch (e) {
compilation.errors.push(e);
return;
}

compilation.hooks.finishModules.tap(this.key, () => {
const files = this.getFiles(compiler, wanted, exclude);
compilation.hooks.finishModules.tapPromise(this.key, async () => {
/** @type {string[]} */
// @ts-ignore
const files = (
await Promise.all(
this.getFiles(compiler, wanted, exclude).map(
async (/** @type {string | undefined} */ file) => {
try {
return (await api.isPathIgnored(file)) ? false : file;
} catch (e) {
return file;
}
}
)
)
).filter((file) => file !== false);

if (threads > 1) {
for (const file of files) {
Expand Down
12 changes: 10 additions & 2 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getStylelint = require('./getStylelint');

/** @typedef {import('stylelint')} Stylelint */
/** @typedef {import('stylelint').LintResult} LintResult */
/** @typedef {import('stylelint').InternalApi} InternalApi */
/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Compilation} Compilation */
/** @typedef {import('./options').Options} Options */
Expand All @@ -25,12 +26,15 @@ const resultStorage = new WeakMap();
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{lint: Linter, report: Reporter, threads: number}}
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
*/
function linter(key, options, compilation) {
/** @type {Stylelint} */
let stylelint;

/** @type {InternalApi} */
let api;

/** @type {(files: string|string[]) => Promise<LintResult[]>} */
let lintFiles;

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

try {
({ stylelint, lintFiles, cleanup, threads } = getStylelint(key, options));
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
key,
options
));
} catch (e) {
throw new StylelintError(e.message);
}

return {
lint,
api,
report,
threads,
};
Expand Down
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;
}
8 changes: 8 additions & 0 deletions test/mock/stylelint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ module.exports = {
],
};
},

createLinter() {
return {
isPathIgnored() {
return false;
},
};
},
};
13 changes: 13 additions & 0 deletions test/stylelint-ignore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pack from './utils/pack';

describe('stylelint ignore', () => {
it('should ignore file', (done) => {
const compiler = pack('stylelintignore');

compiler.run((err, stats) => {
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(false);
done();
});
});
});
5 changes: 5 additions & 0 deletions test/stylelint-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ describe('stylelint lint', () => {
jest.mock('stylelint', () => {
return {
lint: mockLintFiles,
createLinter: () => {
return {
isPathIgnored: () => false,
};
},
};
});
});
Expand Down
1 change: 1 addition & 0 deletions types/getStylelint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare namespace getStylelint {
}
type Options = import('./options').Options;
type Linter = {
api: import('stylelint').InternalApi;
stylelint: Stylelint;
lintFiles: LintTask;
cleanup: AsyncTask;
Expand Down
5 changes: 4 additions & 1 deletion types/linter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ export = linter;
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{lint: Linter, report: Reporter, threads: number}}
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
*/
declare function linter(
key: string | undefined,
options: Options,
compilation: Compilation
): {
api: InternalApi;
lint: Linter;
report: Reporter;
threads: number;
Expand All @@ -19,6 +20,7 @@ declare namespace linter {
export {
Stylelint,
LintResult,
InternalApi,
Compiler,
Compilation,
Options,
Expand All @@ -33,6 +35,7 @@ declare namespace linter {
}
type Options = import('./options').Options;
type Compilation = import('webpack').Compilation;
type InternalApi = import('stylelint').InternalApi;
type Linter = (files: string | string[]) => void;
type Reporter = () => Promise<Report>;
type Stylelint = import('postcss').PluginCreator<
Expand Down

0 comments on commit 999efe8

Please sign in to comment.