如何排除`node_modules/@ types/**/node_modules`? [英] How to exclude `node_modules/@types/**/node_modules`?
问题描述
我遇到了这样一种情况:node_modules/@types
中的类型定义正在安装自己的@types依赖项,而这些嵌套" @types与我的顶级@types冲突.
@types
|-angular //v1.5
|-angular-ui-bootstrap
|-node_modules
|-@types
|-angular //v1.6
如何在tsconfig中排除node_modules/@types/**/node_modules
?
一个警告-我正在使用awesome-typescript-loader,它可能有一些限制.
我尝试过的事情:
1-exclude
属性中的文件glob,以排除嵌套的node_modules
compilerOptions.exclude: '../node_modules/@types/**/node_modules'
2-明确声明types
compilerOptions.types: ['angular', 'angular-ui-bootstrap']
3-typeRoots
中的文件全局以排除嵌套的node_modules
compilerOptions.typeRoots: ['../node_modules/@types/**/!(node_modules)']
我学到的知识
1-排除似乎不适用于@types
2-包含具有类型"的类型意味着包含其依赖的@types
3-typeRoots似乎不适用于文件glob(或者我写的glob错误)
相关:
https://github.com/Microsoft/TypeScript/issues/9731
https://github.com/Microsoft/TypeScript/issues/11917
https://github.com/s-panferov/awesome- typescript-loader/issues/492
tsconfig-如何忽略@ types/whatever/node_modules用于特定目录?
我的环境详细信息
节点":"8.6.0", 打字稿:"2.8.3", "awesome-typescript-loader":"5.0.0", "webpack":"4.8.3",
为此,我发现一个解决方案是在tsconfig.json
paths
设置中指定node_modules/@types
目录.
这是我在tsconfig.json
中更改的代码段,您应该能够适应您的用例.我需要修改我的baseUrl
和paths
设置.
...
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"*": ["./node_modules/@types/*"]
}
...
在我的情况下,我在TS项目中使用绝对URL,因此我的本地文件都相对于@/
.我认为,如果您不使用这样的绝对网址,则应在配置中输入以下内容:
...
"baseUrl": ".",
"paths": {
"*": ["./src/*", "./node_modules/@types/*"]
}
...
这是我完整的tsconfig.json
,其中可能包含很多不相关的信息,仅供参考.
{
"compilerOptions": {
"outDir": "./build/",
"sourceMap": true,
"allowJs": true,
"checkJs": true,
"jsx": "react",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"*": ["./node_modules/@types/*"]
}
},
"include": ["**/*", "*"],
"exclude": ["build", "node_modules", "coverage"]
}
I have run into a situation where a type definition in node_modules/@types
is installing its own @types dependencies, and these "nested" @types conflict with my top level @types.
@types
|-angular //v1.5
|-angular-ui-bootstrap
|-node_modules
|-@types
|-angular //v1.6
How can I exclude node_modules/@types/**/node_modules
in my tsconfig?
One caveat - I am using awesome-typescript-loader, which may have some limitations.
What I've tried:
1 - file glob in the exclude
property to exclude the nested node_modules
compilerOptions.exclude: '../node_modules/@types/**/node_modules'
2 - declaring types
explicitly
compilerOptions.types: ['angular', 'angular-ui-bootstrap']
3 - file glob in the typeRoots
to exclude nested node_modules
compilerOptions.typeRoots: ['../node_modules/@types/**/!(node_modules)']
What I've learned
1 - exclude doesn't seem to work with @types
2 - including a type with "types" means including its dependent @types
3 - typeRoots doesn't seem to work with file globs (or I'm writing the glob wrong)
Related:
Exclude @types typings in installed dependencies
https://github.com/Microsoft/TypeScript/issues/9731
https://github.com/Microsoft/TypeScript/issues/11917
https://github.com/s-panferov/awesome-typescript-loader/issues/492
tsconfig - How to ignore @types/whatever/node_modules for a specific directory?
Details on my environment
"node": "8.6.0", "typescript: "2.8.3", "awesome-typescript-loader": "5.0.0", "webpack": "4.8.3",
A solution I found for this was to specify the node_modules/@types
directory in the tsconfig.json
paths
setting.
Here's the snippet I changed in my tsconfig.json
, which you should be able to adapt for your use case. I needed to modify my baseUrl
and my paths
settings.
...
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"*": ["./node_modules/@types/*"]
}
...
In my case I'm using absolute urls in my TS project, so my local files are all relative to @/
. I think if you are not using an absolute url like that, you should have something like the following for your config:
...
"baseUrl": ".",
"paths": {
"*": ["./src/*", "./node_modules/@types/*"]
}
...
Here's my complete tsconfig.json
, which probably has a lot of irrelevant information, for reference.
{
"compilerOptions": {
"outDir": "./build/",
"sourceMap": true,
"allowJs": true,
"checkJs": true,
"jsx": "react",
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"*": ["./node_modules/@types/*"]
}
},
"include": ["**/*", "*"],
"exclude": ["build", "node_modules", "coverage"]
}
这篇关于如何排除`node_modules/@ types/**/node_modules`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!