简体   繁体   English

如何从命令行检查 Angular *.html 文件?

[英]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.

相关问题 如何让 ESLint 在 VSCode 中对 HTML 文件进行 lint? - How can I get ESLint to lint HTML files in VSCode? 如何在Ubuntu上从命令行运行此html-minifier? - How to run this html-minifier from the command-line on Ubuntu? ESLint 不会对命令行中的所有错误进行 lint - ESLint doesn't lint ALL errors in command-line 如何在 Angular 8 的 FileReader 中从 2 个不同的 HTML 输入中获取 2 个文件? - How to get 2 files from 2 different HTML inputs in FileReader in Angular 8? 是否可以从命令行运行JavaScript文件? - Is it possible to run JavaScript files from the command line? 如何使用 javascript 在 HTML 中运行命令行? - How to run a command line in HTML using javascript? 如何从命令行在html内运行javascript并包含参数? - How do I run a javascript within html from the command line and include arguments? 未捕获的 ReferenceError:需要未定义(如何从命令行或 html 文件使用 javascript 库) - Uncaught ReferenceError: require is not defined (How to use a javascript library from the command line, or an html file) 如何在 Angular HTML 文件中执行 javascript? - How to execute javascript in Angular HTML files? gruntjs:从命令行配置要连接的文件 - gruntjs: Configure which files to concat from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM