简体   繁体   English

如何在Lerna Monorepo中修复VSCode导入路径建议?

[英]How Can I Fix VSCode Import Path Suggestions In Lerna Monorepo?

VSCode does a great job with autosuggesting imports, however inside a (Lerna) monorepo it only suggests relative paths from one package to another, for example: VSCode在自动提取导入方面做得很好,但是在(Lerna)monorepo中它只建议从一个包到另一个包的相对路径,例如:

import example from '../../../@scope/example/lib/index.html'

I need to refer to other packages using their package names: 我需要使用他们的包名来引用其他包:

import example from '@scope/example';

My jsconfig.json which is at the root of my monorepo: 我的jsconfig.json是我的jsconfig.json的根源:

{
  "compilerOptions": {
    "target": "es6",
    "jsx": "react"
  },
  "include": ["**/src/**/*.js"],
  "exclude": [
    "**/node_modules/*",
    "**/dist/*",
    "**/coverage/*",
    "**/demo/*",
    "**/lib/*",
    "**/public/*"
  ]
}

Is there any way to get the correct autocompletion in VSCode? 有没有办法在VSCode中获得正确的自动完成功能?

Note: There is a plugin available, but it only works with .ts files. 注意:有一个插件可用,但它只适用于.ts文件。

You can configure paths in a jsconfig.json to let VS Code's tooling know how to resolve @/ paths. 您可以在jsconfig.json配置paths ,以使VS Code的工具知道如何解析@/ paths。

In your jsconfig.json , add: 在你的jsconfig.json ,添加:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
       "@scope/example/*": [ "./path/to/scope/example/*" ]
    }
  },
   "exclude": [
     "node_modules"
  ]
}

You can configure paths to map from any path prefix to a subdirectory in your workspace. 您可以配置路径以从任何路径前缀映射到工作空间中的子目录。 See the path mapping documentation for more details 有关详细信息,请参阅路径映射文档

Note that paths only effects imports of javascript or typescript files; 请注意,路径仅影响javascript或打字稿文件的导入; an .html import still won't work properly .html导入仍然无法正常工作

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

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