gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:00 30 4
gpt4 key购买 nike

我的 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 because of path:

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

关于typescript - ts-loader:从其他 Yarn 工作区导入 .ts 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52534829/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com