简体   繁体   English

不能在模块外使用 import 语句(Bazel + Concatjs + NgRx)

[英]Cannot use import statement outside a module (Bazel + Concatjs + NgRx)

The Issue问题

I am using Bazel to build my Angular application.我正在使用Bazel构建我的 Angular 应用程序。

Everything works fine with NgRx version 9. NgRx版本 9 一切正常。

But after upgrading NgRx to v10 or greater, the Angular development server breaks with this error:但是将 NgRx 升级到 v10 或更高版本后,Angular 开发服务器中断并出现此错误:

Uncaught SyntaxError: Cannot use import statement outside a module
    at ts_scripts.js?v=32020367:12007

Minimal Repro duction最小复制

To reproduce this issue follow these steps:要重现此问题,请按照下列步骤操作:

  1. git clone https://github.com/flolu/bazel-concatjs-ngrx-issue
  2. cd bazel-concatjs-ngrx-issue
  3. yarn install
  4. yarn start (Open http://localhost:4200, works fine) yarn start (打开 http://localhost:4200,工作正常)
  5. yarn upgrade-ngrx (Upgrade NgRx to v11) yarn upgrade-ngrx (将 NgRx 升级到 v11)
  6. yarn start (Open http://localhost:4200, error in the browser console) yarn start (打开http://localhost:4200,浏览器控制台报错)

Note, that the production server yarn prod works fine on both versions of NgRx.请注意,生产服务器yarn prod在 NgRx 的两个版本上都可以正常工作。

Therefore, this is a problem with the development server (in this case: @bazel/concatjs )因此,这是开发服务器的问题(在这种情况下: @bazel/concatjs

This issue is caused by the ngfactory.js files included with NgRx, however I'm unclear if these files should be part of the library distribution or not.这个问题是由 NgRx 中包含的ngfactory.js文件引起的,但是我不清楚这些文件是否应该是库分发的一部分。 It seems perhaps in older versions of Angular they were?似乎在旧版本的 Angular 中它们是?

In rules_nodejs, the BUILD file generation for node_modules includes these ngfactory files in the srcs attribute of the generated js_library .在 rules_nodejs 中,node_modules 的 BUILD 文件生成将这些ngfactory文件包含在生成的js_librarysrcs属性中。 (I think that's incorrect now, so we can fix that), these sources include import statements which aren't allowed outside of the modules (as the error states) as we are loading UMD. (我认为现在这是不正确的,所以我们可以修复它),这些来源包括在我们加载 UMD 时不允许在模块之外的import语句(如错误状态)。

A workaround around here is to either:这里的解决方法是:

  • Patch rules_nodejs to not add those files to the generated js_library 修补 rules_nodejs以不将这些文件添加到生成的js_library
  • Add a postinstall script to remove the ngfactory.js files from NgRx which runs before BUILD file generation.添加一个安装后脚本以从 NgRx 中删除ngfactory.js文件,该文件在生成文件之前运行。 Example below:下面的例子:

//:package.json

diff --git a/package.json b/package.json
index 137c115..b322b55 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,7 @@
     "@angular/platform-browser": "^11.2.4",
     "@angular/platform-browser-dynamic": "^11.2.4",
     "@angular/router": "^11.2.4",
-    "@ngrx/store": "9.1.2",
+    "@ngrx/store": "^11.0.1",
     "core-js": "2.6.9",
     "rxjs": "^6.6.3",
     "systemjs": "6.1.2",
@@ -29,7 +29,7 @@
     "@bazel/rollup": "^3.2.0",
     "@bazel/terser": "^3.2.0",
     "@bazel/typescript": "^3.2.0",
-    "@ngrx/store-devtools": "9.1.2",
+    "@ngrx/store-devtools": "^11.0.1",
     "@types/jasmine": "^3.6.2",
     "@types/node": "^14.11.2",
     "@typescript-eslint/eslint-plugin": "^4.15.0",
@@ -53,6 +53,7 @@
     "rollup": "^2.28.2",
     "rollup-plugin-commonjs": "^10.1.0",
     "rollup-plugin-node-resolve": "^5.2.0",
+    "shelljs": "^0.8.4",
     "terser": "^5.3.4",
     "typescript": "^4.2.3"
   },
@@ -60,7 +61,7 @@
     "start": "ibazel run //services/client:dev_server",
     "prod": "bazelisk run //services/client:app_server",
     "upgrade-ngrx": "yarn add @ngrx/store -D @ngrx/store-devtools",
-    "postinstall": "patch-package && ngcc"
+    "postinstall": "node postinstall.js && patch-package && ngcc"

postinstall.js

require('shelljs').rm(['node_modules/@ngrx/**/*.ngfactory.js', 'node_modules/@ngrx/**/*.ngsummary.json']);

To have this run after via after the yarn install via bazel:要在通过 bazel 安装纱线后通过以下方式运行:

//:BUILD.bazel

diff --git a/BUILD.bazel b/BUILD.bazel
index b91e927..d7a8767 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -2,6 +2,8 @@ load("@npm//@bazel/typescript:index.bzl", "ts_config")

 package(default_visibility = ["//:__subpackages__"])

+exports_files(["postinstall.js"])
+
 ts_config(
     name = "tsconfig",
     src = "tsconfig.json",

//:WORKSPACE

diff --git a/WORKSPACE b/WORKSPACE
index 7fabecb..cd0e095 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -18,6 +18,9 @@ yarn_install(
     package_json = "//:package.json",
     quiet = False,
     yarn_lock = "//:yarn.lock",
+    data = [
+        "//:postinstall.js",
+    ],
 )

 load("@io_bazel_rules_sass//sass:sass_repositories.bzl", "sass_repositories")

Applying this to your repro produces the "Hello world" as expected with the updated NgRx deps.将它应用到您的复制品会产生“Hello world”,正如更新的 NgRx deps 所期望的那样。

暂无
暂无

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

相关问题 预呈现会导致 SyntaxError:无法在模块外使用 import 语句 - Prerendering causes a SyntaxError: Cannot use import statement outside a module 未捕获的语法错误:无法在 Angular 和 Firebase 中的模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in Angular & Firebase Angular Datatables Cannot use import statement outside a module 错误 - Angular Datatables Cannot use import statement outside a module Error WebdriverIO 和 Angular,在导入类的打字稿中编写 e2e 测试(不能在模块外使用 import 语句) - WebdriverIO and Angular, writing e2e tests in typescript that import classes (Cannot use import statement outside a module) SyntaxError:无法在 Angular/Nx/jest 应用程序中使用 @ionic-native/health 在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module using @ionic-native/health in Angular/Nx/jest app 未捕获的语法错误:无法在 Angular 12 项目中的模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in Angular 12 project 更新到 Angular 13 后,无法在 Jest 中使用带有 date-fns 的模块外使用 import 语句 - Cannot use import statement outside a module with date-fns in Jest after updating to Angular 13 Angular.ts 和 Electron:SyntaxError:不能在模块外使用导入语句 - Angular.ts and Electron: SyntaxError: Cannot use import statement outside a module Angular 从版本 8 升级到 15。错误“无法在模块外使用导入语句” - Angular upgrade from version 8 to 15. Error "Cannot use import statement outside a module" “不能在模块外使用 import 语句” - JavaScript 模块导入到 TypeScript 文件中,JavaScript 模块内有其他导入 - "Cannot use import statement outside of module" - JavaScript module imported into TypeScript file, JavaScript module has other imports within
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM