ts-loader:从其他 Yarn 工作区导入 .ts 模块

ts-loader: import .ts module from other Yarn workspace

我的 Yarn 工作区结构如下:

/project
  package.json
  /packages
    /app
      package.json
      webpack.config.js
      tsconfig.json
      /src
        index.ts
    /api-adapter
      package.json
      /src
        api.ts

为了构建 /app 包,我将 Webpack 与 ts-loader 结合使用。这是配置:

// /packages/app/webpack.config.js
module.exports = {
    entry: path.resolve(__dirname, "src/client/index.ts"),
    mode: "development",
    devtool: "inline-source-map",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader",
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"]
    },
    output: {...}
};

tsconfig.json

{
  "compilerOptions": {
    "module": "es6",
    "target": "es6",
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true,
    "noImplicitAny": true,
    "outDir": "./dist/",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": ["dom", "es6", "webworker"]
  }
}

当我想从其他工作区导入模块时:

// /packages/app/src/index.ts
import { fetchSomething } from "api-adapter/src/api";

发生错误:

ERROR in ../api-adapter/src/api.ts
Module build failed (from /home/user/project/node_modules/ts-loader/index.js):
Error: Typescript emitted no output for /home/user/project/packages/api-adapter/src/api.ts.

但是当我添加导入扩展时,一切正常:

// /packages/app/src/index.ts
import { fetchSomething } from "api-adapter/src/api.ts";

如何修复 webpack 配置以将任何工作区模块导入为不带扩展名的 Typescript?

我已经试过了:

rules: [{
  test: /\.tsx?$/,
  use: "ts-loader",
  exclude: /node_modules/,
  include: [
    __dirname,
    path.resolve(__dirname, "../packages")
  ],
}]

与:

ERROR in ../api-adapter/src/api.ts 3:34
Module parse failed: Unexpected token (3:34)
You may need an appropriate loader to handle this file type.

Webpack 配置正确。 ts-loader找到一个文件,但编译器拒绝了它:Error: Typescript emitted no output 因为路径:

// tsconfig.json
{
    "compilerOptions": {
      ...
    },
    "include": [
        "src/**/*",
        "../../packages/**/*"
    ]
}