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

fix: ignored file #266

Merged
merged 1 commit into from
Mar 21, 2022
Merged
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 jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
collectCoverageFrom: ['src/**/*.js'],
collectCoverage: true,
testEnvironment: 'node',
testTimeout: 60000,
transformIgnorePatterns: ['node_modules/(?!(arrify)/)'],
};
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 {import('jest-worker').Worker & {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
38 changes: 29 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,46 @@ 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 = compiler.modifiedFiles
? Array.from(compiler.modifiedFiles).filter(
(file) =>
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true })
)
: globby.sync(wanted, { dot: true, ignore: exclude });
compilation.hooks.finishModules.tapPromise(this.key, async () => {
const files = (
await Promise.all(
(compiler.modifiedFiles
? Array.from(compiler.modifiedFiles).filter(
(file) =>
isMatch(file, wanted, { dot: true }) &&
!isMatch(file, exclude, { dot: true })
)
: globby.sync(wanted, { dot: true, ignore: 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 @@ -6,6 +6,7 @@ import { arrify } from './utils';

/** @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 @@ -24,12 +25,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}}
*/
export default function linter(key, options, compilation) {
/** @type {Stylelint} */
let stylelint;

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

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

Expand All @@ -45,13 +49,17 @@ export default 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