如何在Angular 5+中导入带有未编译TypeScript的repo(并排除.spec.ts文件)

时间:2018-01-25 18:52:31

标签: angular typescript tsconfig

最近升级到Angular 5.x不允许从node_modules仓库导入未编译的TypeScript。看起来解决方法是使用.ts通过tsconfig.json包含"include": ["node_modules/example/**/*.ts"]个文件;但是,这包括导致错误的.spec.ts个文件。

回溯一下,如果您的仓库未编译,则会出现如下错误:

ERROR in ./node_modules/example/lib/core/example.ts
Module build failed: Error: /Users/example/Work/example/example/node_modules/example/lib/core/example.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
The missing file seems to be part of a third party library. TS files in published libraries are often a sign of a badly packaged library. Please open an issue in the library repository to alert its author and ask them to package the library using the Angular Package Format (goo.gl/jB3GVv).
    at AngularCompilerPlugin.getCompiledFile (/Users/example/Work/example/example/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:656:23)
    at plugin.done.then (/Users/example/Work/example/example/node_modules/@angular/cli/node_modules/@ngtools/webpack/src/loader.js:467:39)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
 @ ./lib/visualization/visualization-widget/visualization-widget/visualization-widget.component.ts 25:24-79
 @ ./lib/visualization/visualization-widget/visualization-widget.module.ts
 @ ./lib/visualization/visualization.module.ts
 @ ./lib/example.module.ts
 @ ./src/demo-app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts

我理解未编译的TS不会像过去那样导入,因为无法控制哪个编译器用于库。然而,这对我们来说并不重要,因为它是在我们可以控制的内部回购中。因此,导入未编译的TS会为我们节省一步(并且可能会无意中省略此步骤)。

问题在于我们不确定如何包含.ts个文件并排除 .spec.ts个文件。

tsconfig.json我们有:

  "include": ["node_modules/example/**/*.ts"],
  "exclude": ["node_modules/example/**/*.spec.ts"],

这不起作用。正如我所理解的那样,仍然包含.spec.ts文件,第一个include规则“胜过”第二个exclude规则。

我们如何导入.ts个文件并排除.spec.ts个文件?

以下是我今天早些时候提出的问题:How to exclude files ending in '.spec.ts' in tsconfig.json 以下是关于此问题的主题:https://github.com/angular/angular-cli/issues/8284

1 个答案:

答案 0 :(得分:9)

打开你的tsconfig.json,然后改成这个

{
  ...
  "compilerOptions": {
   ...
  },      
  "include": [
    "./src",
    "node_modules/example"
  ],
  "exclude" : ...
}