Skip to content

Commit

Permalink
fix: path ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Dec 30, 2022
1 parent 7d634cf commit 5a24f5b
Show file tree
Hide file tree
Showing 16 changed files with 1,784 additions and 11,708 deletions.
13,347 changes: 1,689 additions & 11,658 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,45 @@
},
"dependencies": {
"globby": "^11.1.0",
"jest-worker": "^29.2.1",
"jest-worker": "^29.3.1",
"micromatch": "^4.0.5",
"normalize-path": "^3.0.0",
"schema-utils": "^4.0.0"
},
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"@commitlint/cli": "^17.2.0",
"@commitlint/config-conventional": "^17.2.0",
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.7",
"@babel/preset-env": "^7.20.2",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
"@types/file-entry-cache": "^5.0.2",
"@types/fs-extra": "^9.0.13",
"@types/micromatch": "^4.0.2",
"@types/normalize-path": "^3.0.0",
"@types/webpack": "^5.28.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^29.2.2",
"babel-jest": "^29.3.1",
"chokidar": "^3.5.3",
"cross-env": "^7.0.3",
"cspell": "^6.14.3",
"cspell": "^6.18.1",
"del": "^6.1.1",
"del-cli": "^4.0.1",
"eslint": "^8.26.0",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"file-loader": "^6.2.0",
"fs-extra": "^10.1.0",
"husky": "^8.0.1",
"jest": "^29.2.2",
"lint-staged": "^13.0.3",
"husky": "^8.0.2",
"jest": "^29.3.1",
"lint-staged": "^13.1.0",
"npm-run-all": "^4.1.5",
"postcss-scss": "^4.0.5",
"prettier": "^2.7.1",
"postcss-scss": "^4.0.6",
"prettier": "^2.8.1",
"standard-version": "^9.5.0",
"stylelint": "^14.14.0",
"typescript": "^4.8.4",
"webpack": "^5.74.0"
"stylelint": "^14.16.1",
"typescript": "^4.9.4",
"webpack": "^5.75.0"
},
"keywords": [
"stylelint",
Expand Down
19 changes: 17 additions & 2 deletions src/getStylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const cache = {};
/** @typedef {import('stylelint')} Stylelint */
/** @typedef {import('stylelint').LintResult} LintResult */
/** @typedef {import('./options').Options} Options */
/** @typedef {(stylelint: Stylelint, filePath: string) => Promise<boolean>} isPathIgnored */
/** @typedef {() => Promise<void>} AsyncTask */
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
/** @typedef {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lintFiles: LintTask, cleanup: AsyncTask, threads: number }} Linter */
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */

/**
Expand All @@ -26,9 +27,23 @@ function loadStylelint(options) {
const stylelintOptions = getStylelintOptions(options);
const stylelint = setup(options, stylelintOptions);

/** @type {isPathIgnored} */
let isPathIgnored;

try {
isPathIgnored = require(`${options.stylelintPath}/lib/isPathIgnored`);
} catch (e) {
try {
// @ts-ignore
isPathIgnored = require('stylelint/lib/isPathIgnored');
} catch (_) {
isPathIgnored = () => Promise.resolve(false);
}
}

return {
stylelint,
api: stylelint.createLinter(stylelintOptions),
isPathIgnored,
lintFiles,
cleanup: async () => {},
threads: 1,
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ class StylelintWebpackPlugin {
}

compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
/** @type {import('stylelint')} */
let stylelint;

/** @type {import('./linter').Linter} */
let lint;

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

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

try {
({ lint, api, report, threads } = linter(
({ stylelint, lint, isPathIgnored, report, threads } = linter(
this.key,
options,
compilation
Expand All @@ -127,7 +130,7 @@ class StylelintWebpackPlugin {
: globby.sync(wanted, { dot: true, ignore: exclude })
).map(async (file) => {
try {
return (await api.isPathIgnored(file)) ? false : file;
return (await isPathIgnored(stylelint, file)) ? false : file;
} catch (e) {
return file;
}
Expand Down
13 changes: 7 additions & 6 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { arrify } = require('./utils');
/** @typedef {import('stylelint')} Stylelint */
/** @typedef {import('stylelint').LintResult} LintResult */
/** @typedef {import('stylelint').LinterResult} LinterResult */
/** @typedef {import('stylelint').InternalApi} InternalApi */
/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').FormatterType} FormatterType */
/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Compilation} Compilation */
/** @typedef {import('./options').Options} Options */
/** @typedef {import('./getStylelint').isPathIgnored} isPathIgnored */
/** @typedef {(compilation: Compilation) => Promise<void>} GenerateReport */
/** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport}} Report */
/** @typedef {() => Promise<Report>} Reporter */
Expand All @@ -26,14 +26,14 @@ const resultStorage = new WeakMap();
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
*/
function linter(key, options, compilation) {
/** @type {Stylelint} */
let stylelint;

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

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

try {
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
({ stylelint, isPathIgnored, lintFiles, cleanup, threads } = getStylelint(
key,
options
));
Expand All @@ -59,8 +59,9 @@ function linter(key, options, compilation) {
}

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

createLinter() {
return {
isPathIgnored() {
return false;
},
};
},
};
12 changes: 12 additions & 0 deletions test/mock/stylelint/lib/isPathIgnored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {import('stylelint')} stylelint
* @param {string} filePath
* @returns {Promise<boolean>}
*/
function isPathIgnored(stylelint, filePath) {
return new Promise((resolve) => {
resolve(filePath.endsWith('ignore.scss'));
});
}

module.exports = isPathIgnored;
9 changes: 4 additions & 5 deletions test/stylelint-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ describe('stylelint lint', () => {
jest.mock('stylelint', () => {
return {
lint: mockLintFiles,
createLinter: () => {
return {
isPathIgnored: () => false,
};
},
};
});

jest.mock('stylelint/lib/isPathIgnored', () => {
throw new Error();
});
});

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/stylelint-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pack from './utils/pack';
describe('stylelint path', () => {
it('should use another instance of stylelint via stylelintPath config', (done) => {
const stylelintPath = join(__dirname, 'mock/stylelint');
const compiler = pack('good', { stylelintPath });
const compiler = pack('stylelint-path', { stylelintPath });

compiler.run((err, stats) => {
expect(err).toBeNull();
Expand Down
9 changes: 8 additions & 1 deletion types/getStylelint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare namespace getStylelint {
Stylelint,
LintResult,
Options,
isPathIgnored,
AsyncTask,
LintTask,
Linter,
Expand All @@ -22,8 +23,8 @@ declare namespace getStylelint {
}
type Options = import('./options').Options;
type Linter = {
api: import('stylelint').InternalApi;
stylelint: Stylelint;
isPathIgnored: isPathIgnored;
lintFiles: LintTask;
cleanup: AsyncTask;
threads: number;
Expand Down Expand Up @@ -78,6 +79,8 @@ type Stylelint = import('postcss').PluginCreator<
ruleName: string;
ruleSettings: import('stylelint').ConfigRuleSettings<T_1, O>;
root: import('postcss').Root;
result?: import('stylelint').PostcssResult | undefined;
context?: import('stylelint').RuleContext | undefined;
},
callback: (warning: import('postcss').Warning) => void
) => void;
Expand All @@ -87,6 +90,10 @@ type Stylelint = import('postcss').PluginCreator<
};
};
type LintResult = import('stylelint').LintResult;
type isPathIgnored = (
stylelint: Stylelint,
filePath: string
) => Promise<boolean>;
type AsyncTask = () => Promise<void>;
type LintTask = (files: string | string[]) => Promise<LintResult[]>;
type Worker = JestWorker & {
Expand Down
16 changes: 10 additions & 6 deletions types/linter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ export = linter;
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
*/
declare function linter(
key: string | undefined,
options: Options,
compilation: Compilation
): {
api: InternalApi;
stylelint: Stylelint;
isPathIgnored: getStylelint.isPathIgnored;
lint: Linter;
report: Reporter;
threads: number;
Expand All @@ -21,12 +22,12 @@ declare namespace linter {
Stylelint,
LintResult,
LinterResult,
InternalApi,
Formatter,
FormatterType,
Compiler,
Compilation,
Options,
isPathIgnored,
GenerateReport,
Report,
Reporter,
Expand All @@ -36,9 +37,6 @@ 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<
import('stylelint').PostcssPluginOptions
> & {
Expand Down Expand Up @@ -89,6 +87,8 @@ type Stylelint = import('postcss').PluginCreator<
ruleName: string;
ruleSettings: import('stylelint').ConfigRuleSettings<T_1, O>;
root: import('postcss').Root;
result?: import('stylelint').PostcssResult | undefined;
context?: import('stylelint').RuleContext | undefined;
},
callback: (warning: import('postcss').Warning) => void
) => void;
Expand All @@ -97,11 +97,15 @@ type Stylelint = import('postcss').PluginCreator<
longhandSubPropertiesOfShorthandProperties: import('stylelint').LonghandSubPropertiesOfShorthandProperties;
};
};
type Linter = (files: string | string[]) => void;
type Reporter = () => Promise<Report>;
import getStylelint = require('./getStylelint');
type LintResult = import('stylelint').LintResult;
type LinterResult = import('stylelint').LinterResult;
type Formatter = import('stylelint').Formatter;
type FormatterType = import('stylelint').FormatterType;
type Compiler = import('webpack').Compiler;
type isPathIgnored = import('./getStylelint').isPathIgnored;
type GenerateReport = (compilation: Compilation) => Promise<void>;
type Report = {
errors?: StylelintError;
Expand Down
2 changes: 2 additions & 0 deletions types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export type stylelint = import('postcss').PluginCreator<
ruleName: string;
ruleSettings: import('stylelint').ConfigRuleSettings<T_1, O>;
root: import('postcss').Root;
result?: import('stylelint').PostcssResult | undefined;
context?: import('stylelint').RuleContext | undefined;
},
callback: (warning: import('postcss').Warning) => void
) => void;
Expand Down
2 changes: 2 additions & 0 deletions types/worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export type Stylelint = import('postcss').PluginCreator<
ruleName: string;
ruleSettings: import('stylelint').ConfigRuleSettings<T_1, O>;
root: import('postcss').Root;
result?: import('stylelint').PostcssResult | undefined;
context?: import('stylelint').RuleContext | undefined;
},
callback: (warning: import('postcss').Warning) => void
) => void;
Expand Down

0 comments on commit 5a24f5b

Please sign in to comment.