[英]How to lint Angular *.html files from command line?
I would like to lint html angular template files via the command line, similar to:我想通过命令行检查 html angular 模板文件,类似于:
ng lint
I would like to validate the Angular html templates to check they are valid and the variables are correct, so I can run this in a CI pipeline like Travis or AWS Codebuild.我想验证 Angular html 模板以检查它们是否有效以及变量是否正确,因此我可以在 Travis 或 AWS Codebuild 等 CI 管道中运行它。
Visual studio code runs Angular Language Services and can do this: Visual Studio 代码运行 Angular 语言服务,可以执行以下操作:
What I want is to capture these errors, so that I don't let invalid Angular html templates get into releases.我想要的是捕获这些错误,这样我就不会让无效的 Angular html 模板进入版本。
How can I achieve this?我怎样才能做到这一点?
What you are seeing here is in fact a Typescript error, not a lint error.您在这里看到的实际上是 Typescript 错误,而不是 lint 错误。
There is an option fullTemplateTypeCheck
that allows you to capture such errors during build when AOT is activated:有一个选项fullTemplateTypeCheck
允许您在激活 AOT 时在构建期间捕获此类错误:
This option tells the compiler to enable the binding expression validation phase of the template compiler which uses TypeScript to validate binding expressions.此选项告诉编译器启用使用 TypeScript 验证绑定表达式的模板编译器的绑定表达式验证阶段。
This option is false by default.此选项默认为 false。
Note: It is recommended to set this to true because this option will default to true in the future.注意:建议将此设置为 true,因为此选项将来会默认为 true。
This can in fact slow down your build considerably, so what you can do if you only want to use this in the CI pipeline:这实际上会大大减慢您的构建速度,因此如果您只想在 CI 管道中使用它,您可以做什么:
Create a new tsconfig.strict.json next to your tsconfig.app.json like this:在 tsconfig.app.json 旁边创建一个新的 tsconfig.strict.json,如下所示:
{
"extends": "./tsconfig.app.json",
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
In angular.json add a new configuration "strictProd" under projects>YourProject>architect>build>configurations that looks like this:在 angular.json 中,在 projects>YourProject>architect>build>configurations 下添加一个新的配置“strictProd”,如下所示:
"strictProd": {
"tsConfig": "src/tsconfig.app.strict.json",
"aot": true
[...your other prod settings here],
}
Run it with ng build -c strictProd
使用ng build -c strictProd
运行它
The best thing I have found is the html hint node package.我发现的最好的东西是 html 提示节点包。 You can read about it here: https://htmlhint.com/docs/user-guide/getting-started你可以在这里阅读: https : //htmlhint.com/docs/user-guide/getting-started
Here is settings for the .htmlhintrc that work with Angular.这是与 Angular 一起使用的 .htmlhintrc 的设置。
{ "tagname-lowercase": true, "attr-lowercase": false, "attr-value-double-quotes": true, "attr-value-not-empty": false, "attr-no-duplication": true, "doctype-first": false, "tag-pair": true, "tag-self-close": true, "empty-tag-not-self-closed": true, "spec-char-escape": false, "id-unique": false, "src-not-empty": true, "title-require": true, "alt-require": true, "doctype-html5": true, "id-class-value": "true", "style-disabled": true, "inline-style-disabled": true, "inline-script-disabled": true, "space-tab-mixed-disabled": "true", "id-class-ad-disabled": true, "href-abs-or-rel": false, "attr-unsafe-chars": true, "head-script-disabled": true } { "tagname-lowercase": true, "attr-lowercase": false, "attr-value-double-quotes": true, "attr-value-not-empty": false, "attr-no-duplication": true , "doctype-first": false, "tag-pair": true, "tag-self-close": true, "empty-tag-not-self-closed": true, "spec-char-escape": false , "id-unique": false, "src-not-empty": true, "title-require": true, "alt-require": true, "doctype-html5": true, "id-class-value" : "true", "style-disabled": true, "inline-style-disabled": true, "inline-script-disabled": true, "space-tab-mixed-disabled": "true", "id- class-ad-disabled": true, "href-abs-or-rel": false, "attr-unsafe-chars": true, "head-script-disabled": true }
尝试运行“ npm run lint -- --fix ”这将自动解决项目中的所有lint错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.