Skip to content

A nice way to lint .vue files with Stylelint? #303

@dan-gamble

Description

@dan-gamble

I noticed this was attempted: vuejs-templates/webpack#154 and closed (For good reason)

But i wondered if anyone has a setup for this currently?

Activity

xmoki

xmoki commented on Sep 5, 2016

@xmoki

I think is possible to do it with htmlhint, but currently only supports jshint and csslint, maybe if someone can make a htmlhint rule for stylelint.

JounQin

JounQin commented on Sep 5, 2016

@JounQin

Actually we could use <style src="/path/of/style/file"/> then just lint the style file as usual.

yisibl

yisibl commented on Nov 9, 2016

@yisibl
  1. Touch .stylelintrc

    {
      "processors": ["stylelint-processor-html"],
      "extends": "stylelint-config-standard"
    }
  2. Install

  npm i stylelint -D
  npm i stylelint-processor-html -D
  npm i stylelint-config-standard -D
  1. Add lint:css to package.json

    "scripts": {
      "lint:css": "stylelint '**/*.vue' --syntax scss"
    },
  2. npm run lint:css 👻

dan-gamble

dan-gamble commented on Nov 13, 2016

@dan-gamble
Author

@yisibl that works for me, swear i tried this before!

scottsandersdev

scottsandersdev commented on Dec 5, 2016

@scottsandersdev

I'm wondering if there's a way to get stylelint to report this to sublimelinter, although it works great from the command line, it doesn't work actively while typing in sublime. Have you or anyone else had any luck with that? Also, @yisibl thanks for posting this, helped me get started.

bartkozal

bartkozal commented on Feb 12, 2017

@bartkozal

For the record, this is working configuration for webpack:

  1. Install:
npm i --save-dev stylelint stylelint-processor-html stylelint-config-standard stylelint-webpack-plugin
  1. Create .stylelintrc
{
 "processors": ["stylelint-processor-html"],
 "extends": "stylelint-config-standard"
}
  1. Load stylelint-webpack-plugin (in build/webpack.dev.conf.js for Vue webpack template)
//...
var StylelintPlugin = require('stylelint-webpack-plugin')

{
  //...
  plugins: [
    //...
    new StylelintPlugin({
      files: ['**/*.vue']
    })
  ]
}

I published an article which includes both solutions and explains two additional things (configuration rules and errors when starting the dev server).

KangdaOOLin

KangdaOOLin commented on Feb 14, 2017

@KangdaOOLin

@bkzl i follow your config, use the stylelint-webpack-plugin, it can work, but the line number of error reporter is incorrect for .vue file.

qq 20170214151913

bartkozal

bartkozal commented on Feb 15, 2017

@bartkozal

@WenKangLin I immediately fix these errors while I code so I didn't even notice that. There is a corresponding issue: ccbikai/stylelint-processor-html/issues/1

repsac-by

repsac-by commented on Feb 23, 2017

@repsac-by

try @mapbox/stylelint-processor-arbitrary-tags instead stylelint-processor-html

jing2si

jing2si commented on May 25, 2017

@jing2si

"scripts": {
"lint:css": "stylelint '**/*.vue' --syntax scss"
},

Can not add single quotation marks in windows. Otherwise it will report the following error.

Error: '**/*.vue' does not match any files
at globby.then.filePaths

The following setting worked well in windows 10

"scripts": {
  "lint:css": "stylelint **/*.vue"
},
gucong3000

gucong3000 commented on Jul 28, 2017

@gucong3000

stylelint-webpack-plugin or stylelint-processor-html not support automatically fixes.
Related:

#839
stylelint/stylelint#2596

8 remaining items

equinusocio

equinusocio commented on Aug 9, 2018

@equinusocio

Using the @yisibl solution i can't make it works using postcss.

gucong3000

gucong3000 commented on Aug 13, 2018

@gucong3000

@equinusocio The latest version of stylelint can directly support *.vue files, but you need to delete processors in the configuration file.
If you need to use other postcss tool, not just stylelint, you can use this way:

npm i --save postcss postcss-syntax postcss-html
postcss(plugins).process(source, {
  syntax: require('postcss-syntax')
}).then((result) => {
	// An alias for the result.css property. Use it with syntaxes that generate non-CSS output.
	result.content
});
equinusocio

equinusocio commented on Aug 13, 2018

@equinusocio

@gucong3000 Thank you man. This did the trick. There is a documentation somewhere about that?

gucong3000

gucong3000 commented on Aug 14, 2018

@gucong3000

There is a documentation somewhere about that?

Not at the moment.

hiendv

hiendv commented on Nov 20, 2018

@hiendv

@gucong3000 Hi, I cannot get stylelint autofix to work with .vue files? Any idea why?

gucong3000

gucong3000 commented on Dec 4, 2018

@gucong3000

@hiendv please update stylelint >= v9 then try this command:

stylelint --fix `src/**/*.vue`
gucong3000

gucong3000 commented on Dec 4, 2018

@gucong3000

There is a documentation somewhere about that?

https://vue-loader.vuejs.org/guide/linting.html#stylelint

equinusocio

equinusocio commented on Dec 19, 2018

@equinusocio

@gucong3000 This solution doesn't works anymore
#303 (comment)

postcss.config.js

module.exports = {
  syntax: require("postcss-syntax"),
  plugins: {
    "postcss-preset-env": {
      stage: 0,
      autoprefixer: {
        grid: true
      }
    }
  }
};

Defining the syntax package break all the plugins. Removing it stylelint will throw errors parsing the html template. I'm using latest versions of all.

gucong3000

gucong3000 commented on Dec 25, 2018

@gucong3000

@equinusocio
Maybe you can upload a zip package that contains these errors so I could help you debug them.

mudin

mudin commented on Aug 3, 2019

@mudin

After trying many things, I got stylelint autofix to work with .vue files in vscode using SaveOnRun extension.
added below config in the settting.json:

"emeraldwalk.runonsave": {
    "commands": [
      {
        "match": ".vue",
        "cmd": "stylelint --fix ${file}"
      }
    ]
  },

Better solution would be appreciated!

humoyun

humoyun commented on Aug 6, 2019

@humoyun

@mudin good job, saved the day

VictorPulzz

VictorPulzz commented on Jan 25, 2022

@VictorPulzz

Hey guys, can you help me?
I setup stylelint, and try fix my files use this command:
"lint:style": "stylelint --fix **/*.{vue,css,scss,sass}"
I got this message:
Autofix is incompatible with processors and will be disabled. Are you sure you need a processor?
my config:

module.exports = {
  "processors": ["stylelint-processor-html"],
  "extends": [
    "stylelint-config-standard-scss",
    "stylelint-config-prettier-scss"
  ],
  "rules": {
    "no-empty-source": null,
    "block-no-empty": null,
    "color-no-invalid-hex": true,
    "comment-empty-line-before": [
      "always", {
        "ignore": [
          "stylelint-commands",
          "after-comment"
        ]
      }
    ],
    "declaration-colon-space-after": "always",
    "indentation": [
      "tab", {
        "except": [ "value" ]
      }
    ],
    "max-empty-lines": 2,
    "rule-empty-line-before": [
      "always",
      {
        "except": [ "first-nested" ],
        "ignore": [ "after-comment" ]
      }
    ],
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bartkozal@yyx990803@gucong3000@xmoki@oe

        Issue actions

          A nice way to lint `.vue` files with Stylelint? · Issue #303 · vuejs/vue-loader