[英]Webpack can't find import needed for Typescript type resolution. How to fix this?
我的项目使用 typescript 并使用 webpack 构建。
我有一个来自位于node_modules/@types/libname/custom.d.ts
下的第三方库的类型文件。 该文件有一个命名空间声明,其中包含几种类型:
declare namespace MyNamespace {
export type A = ...;
}
然后为了能够在我的 typescript 代码中使用该类型,如下所示:
const x: MyNamespace.A;
...我需要在应用程序的某处添加如下导入声明:
import 'libname'
哪个 tsc 正确解析为node_modules/@types/libname/custom.d.ts
所以如果我用 tsc 编译一切正常。
但是 webpack 然后无法构建。 它无法理解这个导入:
ERROR in ./Index.tsx
Module not found: Error: Can't resolve 'libname' in 'C:\<path_to_index_file>'
@ ./Index.tsx 44:0-32
我试图在我的 webpack.config 中添加一个别名,如下所示:
resolve: {
alias: Object.assign({
'libname$': path.resolve(__dirname, 'node_modules/@types/libname/custom.d.ts'),
}
},
但随后它失败了,因为它无法理解该文件的语法并询问我是否缺少加载程序。
我该如何解决这个问题,以便 webpack 能够理解这个分型导入语句?
我在 webpack 版本4.41.6
谢谢!
您应该为此导入使用null-loader ,因此 webpack 将忽略它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.