UNPKG

1.95 kBJavaScriptView Raw
1const StyleLintPlugin = require('stylelint-webpack-plugin');
2const CodeframeFormatter = require('stylelint-codeframe-formatter');
3
4const lint = require('./lint');
5
6module.exports = (api, projectOptions) => {
7 const { pluginOptions: { lintStyleOnBuild } } = projectOptions;
8 if (lintStyleOnBuild) {
9 api.chainWebpack((webpackConfig) => {
10 /* eslint-disable indent */
11 webpackConfig
12 .plugin('stylelint')
13 .use(StyleLintPlugin, [{
14 failOnError: lintStyleOnBuild === 'error',
15 files: ['src/**/*.{vue,htm,html,css,sss,less,scss}'],
16 formatter: CodeframeFormatter,
17 }])
18 .end()
19 .plugin('friendly-errors')
20 .tap(([options]) => {
21 ['Transformers', 'Formatters'].forEach((name) => {
22 const optKey = `additional${name}`;
23 let plugins;
24 if (Array.isArray(options[optKey])) {
25 plugins = options[optKey];
26 } else {
27 plugins = [];
28 Object.assign(options, { [optKey]: plugins });
29 }
30
31 let plugin;
32 try {
33 const pluginName = name.toLowerCase().slice(0, -1);
34 // eslint-disable-next-line global-require
35 plugin = require('./stylelintError')[pluginName];
36 } catch (e) {
37 return;
38 }
39
40 plugin && plugins.push(plugin);
41 });
42 return [options];
43 });
44 /* eslint-enable indent */
45 });
46 }
47
48 api.registerCommand('lint:style', {
49 description: 'lint and fix source files',
50 usage: 'vue-cli-service lint [options] [...files]',
51 options: {
52 '--no-fix': 'do not fix errors',
53 '--options': 'display stylelint options',
54 },
55 details: 'Autofixing is an experimental feature, see https://stylelint.io/user-guide/cli/#autofixing-errors',
56 }, (args) => { lint(api, args); });
57};