简体   繁体   English

如何让 stylelint 在 Visual Studio Code 中工作?

[英]How to get stylelint to work in Visual Studio Code?

I am trying to get the stylelint extension to work in visual studio code.我正在尝试让 stylelint 扩展在 Visual Studio 代码中工作。 However, with no success so far.但是,到目前为止还没有成功。 The extension with a short explanation is here:带有简短解释的扩展在这里:

https://marketplace.visualstudio.com/items?itemName=shinnn.stylelint https://marketplace.visualstudio.com/items?itemName=shinnn.stylelint

What I did so far: Add到目前为止我做了什么:添加

"stylelint.enable": true,
"css.validate": false,
"scss.validate": false,

to my settings.到我的设置。 But this only deactivates the usual linting without activating stylelint.但这只会停用通常的 linting,而不会激活 stylelint。 I also read at我也读过

Stylelint VScode doesn't work Stylelint VScode 不起作用

in a comment that one needs a stylelint.config.js file.在需要一个stylelint.config.js文件的评论中。 However, I don't have one and don't know how to create or put it (in Windows).但是,我没有,也不知道如何创建或放置它(在 Windows 中)。

Edit编辑

I found an "example configuration" file of stylelint:我找到了一个 stylelint 的“示例配置”文件:

https://github.com/stylelint/stylelint/blob/master/docs/user-guide/example-config.md https://github.com/stylelint/stylelint/blob/master/docs/user-guide/example-config.md

So, I guess part of that needs to go into the stylelint.config.js .所以,我想其中一部分需要进入stylelint.config.js But which are necessary to get it to work and where do I put it?但是哪些是让它工作所必需的,我应该把它放在哪里?

From the stylelint configuration documentation:stylelint 配置文档:

The linter expects a configuration object . linter需要一个配置对象 You can either craft your own config or extend an existing one.您可以制作自己的配置或扩展现有配置。

The " Getting started " section of the stylelint website has some suggestions on how to do this eg using the example config, crafting your own config or extending an existing config. stylelint 网站的“入门”部分提供了一些关于如何执行此操作的建议,例如使用示例配置、制作您自己的配置或扩展现有配置。

I believe the quickest way to hit-the-ground-running, so to speak, is to create a configuration object that extends either stylelint-config-standard or stylelint-config-recommended .我相信,可以这么说,最快的方法就是创建一个扩展stylelint-config-standardstylelint-config-recommended的配置对象。 Use the standard config if you want the linter to enforce stylistic conventions, and use the recommended config if you don't.如果您希望 linter 强制执行样式约定,请使用标准配置,如果不希望,请使用推荐的配置。

To use the recommended config:要使用推荐的配置:

  1. install the config into your project using npm:使用 npm 将配置安装到您的项目中:

npm install --save-dev stylelint-config-recommended

  1. Create a .stylelintrc file that extends the config in the root of your project:创建一个.stylelintrc文件来扩展项目根目录中的配置:

{ "extends": "stylelint-config-recommended" }

A simple response to answer the simple question, the stylelint.config.js file is a module export and looks like so:回答这个简单问题的简单回答, stylelint.config.js文件是一个模块导出,如下所示:

module.exports = {
  extends: [
    "stylelint-config-standard",
  ]
};

If you are using Yarn 2, you need to do an extra configuration, just run:如果您使用的是 Yarn 2,则需要进行额外的配置,只需运行:

yarn dlx @yarnpkg/pnpify --sdk vscode

https://yarnpkg.com/getting-started/editor-sdks#vscode https://yarnpkg.com/getting-started/editor-sdks#vscode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM