简体   繁体   English

如何为使用Jest的NgRx项目配置Bazel?

[英]How do I configure Bazel for an NgRx project that uses Jest?

I have an existing Angular project that uses NgRx and Jest. 我有一个使用NgRx和Jest的现有Angular项目。 I would like to transition the project to use the new opt-in Bazel preview for Angular CLI ( https://next.angular.io/guide/bazel ). 我想将项目转换为对Angular CLI使用新的可选的Bazel预览版( https://next.angular.io/guide/bazel )。 Unfortunately, the configuration generated by running ng add @angular/bazel does not build my project correctly. 不幸的是,通过运行ng add @angular/bazel生成的配置无法正确构建我的项目。

This is an Angular 8 project using Angular Material, NgRx, and Jest. 这是一个使用Angular Material,NgRx和Jest的Angular 8项目。 It was generated using the nrwl/nx Angular CLI helpers, so the folder structure is slightly different than a standard Angular project. 它是使用nrwl / nx Angular CLI帮助器生成的,因此文件夹结构与标准Angular项目略有不同。 I have also opted into Ivy according to these instructions: https://angular.io/guide/ivy 我还根据以下说明选择了常春藤: https : //angular.io/guide/ivy

I have perused the example angular + bazel project here: https://github.com/angular/angular-bazel-example However, that project differs from mine in that it uses Karma + Jasmine, not Jest. 我在这里仔细阅读了angular + bazel项目的示例: https : //github.com/angular/angular-bazel-example但是,该项目与我的项目不同,因为它使用的是Karma + Jasmine,而不是Jest。 (I don't know if this difference is related to my problem or not.) (我不知道这种差异是否与我的问题有关。)

After running yarn ng add @angular/bazel , I also ran yarn add parse5 to fix the error that one of the comments here describes: https://github.com/lukasgeiter/gettext-extractor/issues/17 在运行yarn ng add @angular/bazel ,我还运行yarn add parse5来修复此处评论之一所描述的错误: https : //github.com/lukasgeiter/gettext-extractor/issues/17

Because of the nrwl/nx folder structure, the automatic bazel configuration doesn't work for me (it expects your src folder to be at the root of your workspace, but mine is under apps/angularchatclient/). 由于nrwl / nx文件夹的结构,自动bazel配置对我不起作用(它期望您的src文件夹位于工作空间的根目录,但我的文件夹位于apps / angularchatclient /下)。 I've made some progress by changing "targetLabel": "//src:prodapp", to "targetLabel": "//apps/angularchatclient/src:prodapp", in several locations of my angular.json file. 通过在angular.json文件的多个位置"targetLabel": "//apps/angularchatclient/src:prodapp", "targetLabel": "//src:prodapp",更改为"targetLabel": "//apps/angularchatclient/src:prodapp",我已经取得了一些进展。 I also used the --leaveBazelFilesOnDisk option with ng build so that I could move the BUILD.bazel file to its correct directory and add my app's dependencies to it. 我还在ng build使用了--leaveBazelFilesOnDisk选项,以便可以将BUILD.bazel文件移动到其正确的目录并向其中添加应用程序的依赖项。

Now, when I run yarn build , I get the following error: 现在,当我运行yarn build ,出现以下错误:

yarn run v1.16.0
$ ng build
INFO: Analyzed target //apps/angularchatclient/src:prodapp (1 packages loaded, 1 target configured).
INFO: Found 1 target...
ERROR: /home/user/devbox/angularchatclient/apps/angularchatclient/src/BUILD.bazel:24:1: Compiling Angular templates (ngc) //apps/angularchatclient/src:sr
c failed (Exit 1)
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'NxModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'StoreModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'EffectsModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'StoreDevtoolsModule' was called.
: Unexpected value 'undefined' imported by the module 'AppModule in /home/user/.cache/bazel/_bazel_user/730bcc0be51d4201a9cf443607d64bce/execroot/project/apps/angularchatclient/src/app/app.module.ts'
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'NxModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'StoreModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'EffectsModule' was called.
Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'StoreDevtoolsModule' was called.

Target //apps/angularchatclient/src:prodapp failed to build
Use --verbose_failures to see the command lines of failed build steps.

I have read through this related GitHub issue , but the suggestions to change my tsconfig rules or declare const variables for the forRoot() calls haven't helped. 我已经阅读了相关的GitHub问题 ,但是更改tsconfig规则或为forRoot()调用声明const变量的建议并没有帮助。 Additionally, this question is related What am I missing? 此外, 此问题与我缺少什么有关

Thanks! 谢谢!

Jest should be working but I was getting the same error for ngrx with Angular 8 and Bazel. 开玩笑应该工作,但与Angular 8和Bazel的ngrx出现了相同的错误。 Try updating your angular-metadata.tsconfig.json to look like: 尝试将您的angular-metadata.tsconfig.json更新为:

// WORKAROUND https://github.com/angular/angular/issues/18810
//
// This file is required to run ngc on 3rd party libraries such as @ngrx,
// to write files like node_modules/@ngrx/store/store.ngsummary.json.
//
{
    "compilerOptions": {
        "lib": [
            "dom",
            "es2015"
        ],
        "experimentalDecorators": true,
        "types": [],
        "module": "amd",
        "moduleResolution": "node"
    },
    "angularCompilerOptions": {
        "enableSummariesForJit": true
    },
    "include": [
        "node_modules/@angular/**/*",
        "node_modules/@ngrx/**/*"
    ],
    "exclude": [
        "node_modules/@ngrx/store/migrations/**",
        "node_modules/@ngrx/store/schematics/**",
        "node_modules/@ngrx/store/schematics-core/**",
        "node_modules/@angular/cdk/schematics/**",
        "node_modules/@angular/cdk/typings/schematics/**",
        "node_modules/@angular/material/schematics/**",
        "node_modules/@angular/material/typings/schematics/**",
        "node_modules/@angular/common/upgrade*",
        "node_modules/@angular/router/upgrade*",
        "node_modules/@angular/bazel/**",
        "node_modules/@angular/compiler-cli/**",
        "node_modules/@angular/**/testing/**"

    ]
}

I think the key here is to import the ngrx modules and the enableSummariesForJit . 我认为这里的关键是导入ngrx模块和enableSummariesForJit

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

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