代码之家  ›  专栏  ›  技术社区  ›  Ahmet Can Güven

Typescript没有为“/path/to”发出输出/文件.ts'

  •  1
  • Ahmet Can Güven  · 技术社区  · 7 年前

    我有一个用打字机写的图书馆。我可以用 tsc

    {
      "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "lib": [
          "es6",
          "es5",
          "dom",
          "es2017"
        ],
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "./dist",
        "strict": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "listEmittedFiles": true
      },
      "exclude": [
        "./dist",
        "./test",
        "./bin"
      ],
      "include": [
        "./lib"
      ]
    }
    

    但是当另一个项目试图从链接的npm包中使用这个库时,它无法使用 webpack ts-loader

    错误:Typescript没有为/library/path/to发出输出/文件.ts

    注: webpack尝试从链接的目标加载它,而不是从 node_modules 因为它与npm相连 .


    项目的网页包配置使用库如下。

    module.exports = (entry, dist) => Object.assign({
      entry,
      mode: "production",
      output: {
        filename: "index.js",
        path: dist,
      },
      resolve: {
        extensions: [".js", ".ts"]
      },
      module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: 'ts-loader',
            exclude: /node_modules/
          }
        ]
      },
      stats: 'verbose'
    });
    

    tsconfig.json文件该项目的使用库

    {
      "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "lib": [
          "es6",
          "es5",
          "dom",
          "es2017"
        ],
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "./dist",
        "strict": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
      },
      "exclude": [
        "./dist"
      ],
      "include": [
        "./index.ts"
      ]
    }
    

    import {Ctor, Injector} from "./injector";
    import {ERROR_CODES, PuzzleError} from "./errors";
    import {Route} from "./server";
    
    
    export interface ApiEvents {}
    
    export interface ApiConfig {
      route: Route;
      subApis?: Array<Ctor<Api>>;
    }
    
    interface ApiBase {
    }
    
    export function PuzzleApi<T>(config: ApiConfig) {
      return Injector.decorate((constructor: () => void) => {
        console.log(`Registering Api: ${constructor.name}`);
    
      }, config);
    }
    
    export class Api implements ApiBase {
      config: ApiConfig;
    
      constructor() {
        const config = (this.constructor as any).config as ApiConfig;
    
        if (!config) {
          throw new PuzzleError(ERROR_CODES.CLASS_IS_NOT_DECORATED, this.constructor.name);
        } else {
          this.config = config;
        }
      }
    }
    

    我找不到它不为那个项目输出的任何原因。我可以毫无问题地传送图书馆。有人能帮我吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Matt McCutchen    7 年前

    如果应用程序试图从库(即从 lib 子目录)。尝试从中导入生成的JavaScript文件 dist 而不是子目录。如果您真的想将库作为应用程序的一部分重新编译,那么您可能需要将它添加到应用程序中 "include" 列表中的 tsconfig.json 应用程序的名称。

    推荐文章