angular - 使用 esnext 模块升级 Angular 9 后,Typescript 文件停止转换

标签 angular angular9

我在项目的根位置有一个 typescript 文件(git-version.ts),它在 Jenkins CI 管道中用于读取触发 CI 构建的最新提交/修订(它使用 ts-node 运行这个 ts 文件)。下面是我的项目结构的一瞥:

root
 |_src/
 |_angular.json
 |_git-version.ts
 |_package.json
 |_tsconfig.json

早些时候,它在 Angular 7 上工作得非常好,但似乎被 Angular 9 的默认 esnext 模块加载器破坏了。当我在当前的 Angular 9 tsconfig 设置中将我的模块更改为“commonjs”时,它工作得非常好。但是,我失去了差异加载功能的优势,因为 commnjs 倾向于为我的整个应用程序创建一个单独的包。所以,我不得不切换回“esnext”。以下是我当前的 tsconfig.json 设置:
{
  "compileOnSave": false,
  "compilerOptions": {
    "downlevelIteration": true,
    "importHelpers": true,
    "module": "esnext",
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableIvy": true
  }
}

我还尝试将上述 ts 文件的扩展名更改为 js,但随后它开始抛出另一个异常,“SyntaxError:无法在模块外使用 import 语句”。显然,我需要一种方法来使用“commonjs”加载这个 ts 文件,并且在整个应用程序的其余部分仍然使用“esnext”。

最佳答案

由于有问题的文件位于您的 src 文件夹之外,并且正如您所提到的,它与 commonjs 一起工作正常,很可能这似乎是 typescript 导入的问题。

如果您愿意使用 ts-node 加载 typescript 文件,您可以为特定文件定义不同的加载器。像这样的东西:

ts-node -O '{\"module\": \"commonjs\"}' git.version.ts

您可以将其添加为 npm 脚本,并在 Jenkinsfile 中为您的 CI 管道使用它。

关于angular - 使用 esnext 模块升级 Angular 9 后,Typescript 文件停止转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61461500/

相关文章:

Angular 9 mat-progress-buttons 错误 : ERROR in Failed to compile entry-point mat-progress-buttons (es2015 as esm2015) due to compilation errors

html - 如何设置BootStrap Navbar页面右侧下拉宽度只占用必要空间?

angular - 架构验证失败,出现以下错误 : Data path ".builders[' app-shell' ]"should have required property ' class'

angular - 在 Angular 库模块中导入 SCSS 变量文件

使用动态翻译ID的Angular $ localize

angular - 提供平台与根

javascript - 在 Angular 2 中创建自定义表单控件

html - iframe src 属性动态地作为 URL Angular2

css - Angular Css 样式溢出滚动

drag-and-drop - 通过 CSS-property “transform: scale(0.5)” 缩放的 Angular CDK 拖放无法按预期工作