diff --git a/src/LintDirtyModulesPlugin.js b/src/LintDirtyModulesPlugin.js index e6570a8..708359a 100644 --- a/src/LintDirtyModulesPlugin.js +++ b/src/LintDirtyModulesPlugin.js @@ -1,9 +1,11 @@ import { isMatch } from 'micromatch'; import linter from './linter'; +import { replaceBackslashes } from './utils'; export default class LintDirtyModulesPlugin { - constructor(compiler, options) { + constructor(lint, compiler, options) { + this.lint = lint; this.compiler = compiler; this.options = options; this.startTime = Date.now(); @@ -22,14 +24,14 @@ export default class LintDirtyModulesPlugin { } const dirtyOptions = { ...this.options }; - const glob = dirtyOptions.files.join('|').replace(/\\/g, '/'); + const glob = replaceBackslashes(dirtyOptions.files.join('|')); const changedFiles = this.getChangedFiles(fileTimestamps, glob); this.prevTimestamps = fileTimestamps; if (changedFiles.length) { dirtyOptions.files = changedFiles; - linter(dirtyOptions, this.compiler, callback); + linter(this.lint, dirtyOptions, this.compiler, callback); } else { callback(); } @@ -53,7 +55,7 @@ export default class LintDirtyModulesPlugin { for (const [filename, timestamp] of fileTimestamps.entries()) { if (hasFileChanged(filename, timestamp) && isMatch(filename, glob)) { - changedFiles.push(filename.replace(/\\/g, '/')); + changedFiles.push(replaceBackslashes(filename)); } } diff --git a/src/index.js b/src/index.js index ed450f5..b2a48ff 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,9 @@ import { isAbsolute, join } from 'path'; -import arrify from 'arrify'; - import getOptions from './getOptions'; import LintDirtyModulesPlugin from './LintDirtyModulesPlugin'; import linter from './linter'; +import { parseFiles } from './utils'; class StylelintWebpackPlugin { constructor(options = {}) { @@ -12,17 +11,18 @@ class StylelintWebpackPlugin { } apply(compiler) { - const context = this.getContext(compiler); - const options = { ...this.options }; + const options = { + ...this.options, + files: parseFiles(this.options.files, this.getContext(compiler)), + }; - options.files = arrify(options.files).map((file) => - join(context, '/', file).replace(/\\/g, '/') - ); + // eslint-disable-next-line + const { lint } = require(options.stylelintPath); const plugin = { name: this.constructor.name }; if (options.lintDirtyModulesOnly) { - const lintDirty = new LintDirtyModulesPlugin(compiler, options); + const lintDirty = new LintDirtyModulesPlugin(lint, compiler, options); /* istanbul ignore next */ compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => { @@ -30,12 +30,12 @@ class StylelintWebpackPlugin { }); } else { compiler.hooks.run.tapAsync(plugin, (compilation, callback) => { - linter(options, compilation, callback); + linter(lint, options, compilation, callback); }); /* istanbul ignore next */ compiler.hooks.watchRun.tapAsync(plugin, (compilation, callback) => { - linter(options, compilation, callback); + linter(lint, options, compilation, callback); }); } } diff --git a/src/linter.js b/src/linter.js index 76cc719..70cb6a5 100644 --- a/src/linter.js +++ b/src/linter.js @@ -1,12 +1,9 @@ import StylelintError from './StylelintError'; -export default function linter(options, compiler, callback) { +export default function linter(lint, options, compiler, callback) { let errors = []; let warnings = []; - // eslint-disable-next-line - const { lint } = require(options.stylelintPath); - lint(options) .then(({ results }) => { ({ errors, warnings } = parseResults(options, results)); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..a9c84a0 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,13 @@ +import { join } from 'path'; + +import arrify from 'arrify'; + +export function parseFiles(files, context) { + return arrify(files).map((file) => + replaceBackslashes(join(context, '/', file)) + ); +} + +export function replaceBackslashes(str) { + return str.replace(/\\/g, '/'); +} diff --git a/test/LintDirtyModulesPlugin.test.js b/test/LintDirtyModulesPlugin.test.js index e7478e4..666db14 100644 --- a/test/LintDirtyModulesPlugin.test.js +++ b/test/LintDirtyModulesPlugin.test.js @@ -10,7 +10,9 @@ describe('lint dirty modules only', () => { beforeAll(() => { callback = jest.fn(); - plugin = new LintDirtyModulesPlugin(null, { files: ['**\\*.s?(c|a)ss'] }); + plugin = new LintDirtyModulesPlugin(null, null, { + files: ['**\\*.s?(c|a)ss'], + }); }); beforeEach(() => {