[英]Why does VSCode's “Go to definition” fail on my project using Webpack alias?
This is a followup to vscode issue #16320 , to which vscode dev Matt Bierner suggests I ask here.这是vscode 问题 #16320的后续,vscode dev Matt Bierner 建议我在这里问。
Recent versions of VS Code... VS Code 的最新版本...
jsconfig.json
configuration) webpack aliases, a webpack feature enabling prefixes for import lines for friendlier imports (see vscode documentation for Webpack aliases ). jsconfig.json
配置)webpack 别名,这是一个 webpack 特性,为更友好的导入启用导入行的前缀(请参阅vscode 文档以获取 Webpack 别名)。 For example, aliasing /src/api
to *
will let me type import foo from */users
instead of import foo from ../../../../src/api/users
./src/api
到*
会让我输入import foo from */users
而不是import foo from ../../../../src/api/users
。 But in some uses cases, they fail together.但在某些用例中,它们一起失败。 I built a minimal broken test project (zip, 3kB) for my use case.
我为我的用例构建了一个最小的损坏测试项目(zip,3kB) 。 Can anyone give a look at it and see if I'm doing anything wrong or if it looks like a bug?
任何人都可以看看它,看看我是否做错了什么,或者它看起来像一个错误吗?
Extract the zip and see README.md
: path autocompletion works, but not "peek" or "Go to definition".解压缩 zip 并查看
README.md
:路径自动完成有效,但不能“查看”或“转到定义”。
npm install && npm run build && node dist/index.js
→ Install & build both succeed, indicating Webpack is happy. npm install && npm run build && node dist/index.js
→ Install & build 都成功了,说明Webpack很开心。 Run logs [1, 2, 3]
.[1, 2, 3]
。code /path/to/project
and open src/index.js
On line 5, try to F12 on getLinks
→ No definition found for 'getLinks'
😢code /path/to/project
并打开src/index.js
在第 5 行,尝试在getLinks
上getLinks
F12 → No definition found for 'getLinks'
😢 Am I still doing something wrong in my jsconfig.json
, or is this a bug?我的
jsconfig.json
仍然做错了什么,还是这是一个错误? (the multiple levels of exports, maybe?) (可能是多个级别的出口?)
Accepted answer is correct, except my solution was simply:接受的答案是正确的,除了我的解决方案很简单:
import something from '~/something';
jsconfig.json
looks like this: jsconfig.json
看起来像这样:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./*"]
}
}
}
Change src/index.js
like below:如下更改
src/index.js
:
import { Links } from 'api/resources';
Change jsconfig.json
file paths like below:更改
jsconfig.json
文件路径,如下所示:
"paths": {
"api/*": ["./src/api/*"]
}
in src/api/resourcers/index.js
change your import export like below:在
src/api/resourcers/index.js
更改您的导入导出,如下所示:
import * as Links from './links';
export {Links};
also install this package and use --save-dev
option也安装这个包并使用
--save-dev
选项
npm install --save-dev babel-plugin-module-resolver
after this add the plugin to babel plugins
in package.json
I don't know if you need the root prefix plugin but I removed it.在此之后将插件添加到
package.json
babel plugins
我不知道你是否需要根前缀插件,但我删除了它。
"plugins": [
"transform-runtime",
"transform-export-extensions",
"transform-object-rest-spread",
[
"module-resolver", {
"root": ["./src"],
"alias": {
"api": "./src/api"
}
}
]
you can also change api to resources but in that case you need to import like below:您也可以将 api 更改为资源,但在这种情况下,您需要像下面这样导入:
import {Links} from 'resources/index';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.