简体   繁体   English

将 stylelint 与 angular 构建集成并使其因错误而失败?

[英]Integrate stylelint with angular build and make it failed on error?

I want to use stylelint in angular application to enforce class name for example (they should be in lowercase and dash if if necessary, not camel case or uppercase).例如,我想在 angular 应用程序中使用stylelint来强制执行 class 名称(如果需要,它们应该是小写和破折号,而不是驼峰式或大写)。

How to configure stylelint to run in Angular build time (just like Angular run TSLint).如何配置stylelint以在 Angular 构建时运行(就像 Angular 运行 TSLint)。 Or can TSLint enforce style rules?或者 TSLint 可以强制执行样式规则吗?

The propose is when I have an error in stylelint rule then Angular build will fail.建议是当我在stylelint规则中有错误时,Angular 构建将失败。

According to the documentation of stylelint you can use selector-class-pattern with the kebab-case pattern ^([az][az]*)(-[az]+)*$ .根据 stylelint 的文档,您可以将selector-class-pattern与 kebab-case 模式一起使用^([az][az]*)(-[az]+)*$ This pattern allows only lower case letters and dashes between those letters.此模式仅允许小写字母和这些字母之间的破折号。 If you want to allow also digits you can use this one ^([az][a-z0-9]*)(-[a-z0-9]+)*$ with every alphanumeric group starting with a letter.如果您还想允许数字,您可以使用这个^([az][a-z0-9]*)(-[a-z0-9]+)*$每个字母数字组以字母开头。

You can also have a look at resolveNestedSelectors option of selector-class-pattern if you need it.如果需要,您还可以查看selector-class-patternresolveNestedSelectors选项。

You can use it in your stylelint-config.json file somehow like this ,您可以像这样在您的stylelint-config.json文件中使用它,

"selector-class-pattern": ["^([a-z][a-z]*)(-[a-z]+)*$", {
   "resolveNestedSelectors": false
}

or just,要不就,

"selector-class-pattern": "^([a-z][a-z]*)(-[a-z]+)*$"

I hope it helped:)我希望它有所帮助:)

EDIT: According to this article , for integrating with angular build, in your package.json you can make your scripts look like this:编辑:根据这篇文章,为了与 angular 构建集成,在 package.json 中,您可以使脚本如下所示:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "npm run lint && ng build",
    "test": "ng test",
    "lint": "ng lint && npm run lint:styles",
    "lint:styles": "stylelint \"apps/**/*.scss\" && stylelint \"libs/**/*.scss\"",
    "e2e": "ng e2e"
  },

Angular provides a command for linting ng lint . Angular 提供了一个 linting ng lint的命令。

There is a third party CLI builder available to integrate both ESLint and stylelint.有一个第三方 CLI 构建器可用于集成 ESLint 和 stylelint。 You can then just run ng lint and it will lint with ESLint and stylelint.然后,您可以只运行 ng lint,它会使用 ESLint 和 stylelint 进行 lint。

https://github.com/krema/angular-eslint-stylelint-builder https://github.com/krema/angular-eslint-stylelint-builder

PS: I am the author of this package. PS:我是这个package的作者。

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

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