Skip to content
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ If `true`, `stylelint` will fix as many errors as possible. The fixes are made t

### `formatter`

- Type: `Function`
- Default: `require('stylelint').formatters.string`
- Type: `String|Function`
- Default: `'string'`

Specify the formatter that you would like to use to format your results.
Specify the formatter that you would like to use to format your results. See [formatter option](https://stylelint.io/user-guide/node-api#formatter).

### `lintDirtyModulesOnly`

Expand Down
38 changes: 28 additions & 10 deletions src/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@ import validateOptions from 'schema-utils';

import schema from './options.json';

export default function getOptions(options) {
export default function getOptions(pluginOptions) {
const options = {
files: '**/*.s?(c|a)ss',
formatter: 'string',
stylelintPath: 'stylelint',
...pluginOptions,
};

validateOptions(schema, options, {
name: 'Stylelint Webpack Plugin',
baseDataPath: 'options',
});

const stylelintPath = options.stylelintPath || 'stylelint';

// eslint-disable-next-line
const { formatters } = require(stylelintPath);
const stylelint = require(options.stylelintPath);

return {
files: '**/*.s?(c|a)ss',
formatter: formatters.string,
stylelintPath,
...options,
};
options.formatter = getFormatter(stylelint, options.formatter);

return options;
}

function getFormatter({ formatters }, formatter) {
if (typeof formatter === 'function') {
return formatter;
}

// Try to get oficial formatter
if (
typeof formatter === 'string' &&
typeof formatters[formatter] === 'function'
) {
return formatters[formatter];
}

return formatters.string;
}
2 changes: 1 addition & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"formatter": {
"description": "Specify the formatter that you would like to use to format your results.",
"instanceof": "Function"
"anyOf": [{ "type": "string" }, { "instanceof": "Function" }]
},
"lintDirtyModulesOnly": {
"description": "Lint only changed files, skip lint on start.",
Expand Down
53 changes: 53 additions & 0 deletions test/formatter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { formatters } from 'stylelint';

import pack from './utils/pack';

describe('formatter', () => {
it('should use default formatter', (done) => {
const compiler = pack('error');

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toBeTruthy();
done();
});
});

it('should use default formatter when invalid', (done) => {
const compiler = pack('error', { formatter: 'invalid' });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toBeTruthy();
done();
});
});

it('should use string formatter', (done) => {
const compiler = pack('error', { formatter: 'json' });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toBeTruthy();
done();
});
});

it('should use function formatter', (done) => {
const compiler = pack('error', { formatter: formatters.verbose });

compiler.run((err, stats) => {
expect(err).toBeNull();
expect(stats.hasWarnings()).toBe(false);
expect(stats.hasErrors()).toBe(true);
expect(stats.compilation.errors[0].message).toBeTruthy();
done();
});
});
});