Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dan-gamble opened this issue Aug 5, 2016 · 24 comments
Closed

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

dan-gamble opened this issue Aug 5, 2016 · 24 comments

Comments

@dan-gamble
Copy link

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?

@xmoki
Copy link

xmoki commented Sep 5, 2016

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
Copy link

JounQin commented Sep 5, 2016

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

@yisibl
Copy link

yisibl commented Nov 9, 2016

  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
Copy link
Author

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

@scottsandersdev
Copy link

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
Copy link

bartkozal commented Feb 12, 2017

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
Copy link

@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
Copy link

bartkozal commented Feb 15, 2017

@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
Copy link

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

@jing2si
Copy link

jing2si commented May 25, 2017

"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
Copy link

gucong3000 commented Jul 28, 2017

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

#839
stylelint/stylelint#2596

@oe
Copy link

oe commented Oct 10, 2017

Issue moved to vuejs/011.vuejs.org #6 via ZenHub

@equinusocio
Copy link

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

@gucong3000
Copy link

gucong3000 commented Aug 13, 2018

@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
Copy link

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

@gucong3000
Copy link

gucong3000 commented Aug 14, 2018

There is a documentation somewhere about that?

Not at the moment.

@hiendv
Copy link

hiendv commented Nov 20, 2018

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

@gucong3000
Copy link

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

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

@gucong3000
Copy link

There is a documentation somewhere about that?

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

@equinusocio
Copy link

equinusocio commented Dec 19, 2018

@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
Copy link

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

@mudin
Copy link

mudin commented Aug 3, 2019

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
Copy link

humoyun commented Aug 6, 2019

@mudin good job, saved the day

@VictorPulzz
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests