How to import repo with uncompiled TypeScript in A

2020-03-12 07:15发布

问题:

A recent upgrade to Angular 5.x is not allowing uncompiled TypeScript to import from a node_modules repo. It looks like the workaround is to include the .ts files using tsconfig.json via "include": ["node_modules/example/**/*.ts"]; however, this includes .spec.ts files, which are causing errors.

Backtracking a bit, if your repo is uncompiled, you get an error that looks like:

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

I understand uncompiled TS wont import as it used to, because there is no control over which compiler is used for a library. However, this is of no consequence for us, because it is in an internal repo where we DO have control over that. So importing uncompiled TS saves us a step (and potential for unintentionally omitting this step).

The problem is that we are not sure how to include .ts files and exclude .spec.ts files.

In tsconfig.json we have:

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

This does not work. .spec.ts files are still included as, as I understand, the first include rule 'wins' over the second exclude rule.

How can we import .ts files and exclude .spec.ts files?

Here is my question from earlier today: How to exclude files ending in '.spec.ts' in tsconfig.json Here's a thread about this issue: https://github.com/angular/angular-cli/issues/8284

回答1:

open your tsconfig.json, and change to this

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