微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ts-loader 在导入 .json 文件时输出子目录中的声明文件

如何解决ts-loader 在导入 .json 文件时输出子目录中的声明文件

我的 ts-loader 有一个奇怪的行为:当从 node_modules 导入 *.json 文件时,声明文件是在 dist/ 的子文件夹中生成的,但我希望它们直接在 {{1 }} 文件夹。

我有以下项目结构:

dist/

运行 Project/ ├── src/ │ ├── components/ │ │ └── PhoneNumberInput/ │ │ ├── index.ts │ │ └── utils.ts │ └── index.ts ├── package.json ├── tsconfig.json └── webpack.config.js 时(使用 webpack)我期望以下输出文件夹结构:

ts-loader

直到我开始在我的 Project/ ├── dist/ │ ├── components/ │ │ └── PhoneNumberInput/ │ │ ├── index.d.ts │ │ └── utils.d.ts │ ├── index.d.ts │ └── index.js └── ... 中导入 .json(来自 node_modules/文件之前都是这种情况:

utils.ts

声明文件现在在 import * as de from "react-phone-number-input/locale/de.json"; 生成

dist/src/

对资产的任何相关导入现在都已损坏。 如果我使用 Project/ ├── dist/ │ ├── src/ │ │ ├── components/ │ │ │ └── PhoneNumberInput/ │ │ │ ├── index.d.ts │ │ │ └── utils.d.ts │ │ └── index.d.ts │ └── index.js └── ... 或从项目中导入 .json

awesome-typescript-loader

我没有找到 import * as de from "./countries.json"; 文件夹。

有人知道这种行为的来源吗?我做错了什么?或者它可能是 src/ 中的一个错误? 示例项目可以在 https://github.com/psalchow/ts-path-test

找到

这里是相关的配置文件

package.json

ts-loader

tsconfig.json

{
  "name": "ts-path-test","main": "dist/index.js","types": "dist/index.d.ts","scripts": {
    "build": "webpack --progress --mode=production"
  },"dependencies": {
    "react-phone-number-input": "^3.1.19"
  },"devDependencies": {
    "@types/react-phone-number-input": "^3.0.6","@types/webpack": "^5.28.0","awesome-typescript-loader": "^5.2.1","ts-loader": "^8.1.0","typescript": "^4.2.4","webpack": "^4.46.0","webpack-cli": "^4.6.0"
  }
}

webpack.config.js

{
  "compilerOptions": {
    "allowJs": false,"baseUrl": "src","declaration": true,"esModuleInterop": true,"module": "commonjs","moduleResolution": "node","noImplicitAny": true,"noImplicitReturns": true,"noImplicitThis": true,"outDir": "dist","resolveJsonModule": true,"skipLibCheck": true,"sourceMap": true,"strict": true,"suppressImplicitAnyIndexErrors": true,"target": "es2019"
  },"files": ["src/index.ts"]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。