简体   繁体   English

为什么 VSCode 的“转到定义”在我使用 Webpack 别名的项目上失败?

[英]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 的最新版本...

  1. ... have a Go to definition feature, triggered by pressing F12 on a symbol... ...具有转到定义功能,通过在符号上按F12触发...
  2. ... and also support (via 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 :路径自动完成有效,但不能“查看”或“转到定义”。

  • Skim through the js files to check I'm not asking you to run anything nefarious.浏览 js 文件以检查我并没有要求您运行任何邪恶的东西。
  • 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]
  • Now, run code /path/to/project and open src/index.js On line 5, try to F12 on getLinksNo definition found for 'getLinks' 😢现在,运行code /path/to/project并打开src/index.js在第 5 行,尝试在getLinksgetLinks F12No 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';

Also please check this and this.也请检查这个这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM