[英]How to intellisense alias module path in VSCode
I would like VSCode to IntelliSense the module path so I can access it by click.我希望 VSCode 对模块路径进行 IntelliSense,以便我可以通过单击访问它。
For example, after configurating jsconfig.json
I'm able to access ./src/styled/index
by importing its global path.例如,在配置
jsconfig.json
之后,我可以通过导入其全局路径来访问./src/styled/index
。
But I couldn't figure out how to make it work with an alias @styles
但是我不知道如何让它与别名
@styles
工作
// VSCode Intellisene Works
import { mixins, theme } from 'styles';
// VSCode Intellisene Doesn't work
import { mixins, theme } from '@styles';
My current jsconfig.json
:我当前的
jsconfig.json
:
{
"compilerOptions": {
"baseUrl": "./",
"jsx": "react",
"paths": {
"@styles": ["src/styles/index"]
}
}
}
Seems I had to restart VSCode.似乎我不得不重新启动 VSCode。
javascript
, javascriptreact
file types in VSCode) javascript
,VSCode 中的javascriptreact
文件类型) An example of jsconfig.json
file for reference: jsconfig.json
文件示例供参考:
{
"compilerOptions": {
"baseUrl": "./src",
"jsx": "react",
"paths": {
"@styles": ["styles/index"],
"@fonts": ["fonts/index"],
"@components": ["components/index"],
"@atoms": ["components/atoms/index"],
"@molecules": ["components/molecules/index"],
"@organisms": ["components/organisms/index"],
"@templates": ["components/templates/index"],
"@icons": ["components/atoms/Icons/index"],
"@config": ["config/index"],
"@utils": ["utils/index"],
"@hooks": ["hooks/index"],
"@constants": ["constants/index"],
"@queries": ["queries/index"],
"@reducers": ["state/store/reducers"],
"@actions": ["state/store/actions"],
"@slices": ["state/slices/"],
"@storybookHelpers": ["../.storybook/helpers"]
}
}
}
An example of how styles/index
looks like: styles/index
的示例如下:
export * from './colors';
export * from './GlobalStyle.styles';
export * from './mixins.styles';
// Or
export { COLORS } from './colors';
export { default as GlobalStyle } from './GlobalStyle.styles';
export { default as mixins } from './mixins.styles';
Will allow import (with IntelliSense):将允许导入(使用 IntelliSense):
import { COLORS, mixins, GlobalStyle } from '@styles';
For a bonus: aliases.js
, which is a helper which I use to define aliases in webpack
config files, it helps to not repeat yourself, for example when using the same aliases in storybook
and for the application itself.奖励:
aliases.js
,这是我用来在webpack
配置文件中定义别名的帮助程序,它有助于不要重复自己,例如在storybook
和应用程序本身使用相同的别名时。
// Remember to update `jsconfig.json`
const aliases = (prefix = `src`) => ({
'@actions': `${prefix}/state/store/actions`,
'@atoms': `${prefix}/components/atoms`,
'@molecules': `${prefix}/components/molecules`,
'@organisms': `${prefix}/components/organisms`,
'@templates': `${prefix}/components/templates`,
'@components': `${prefix}/components`,
'@config': `${prefix}/config`,
'@constants': `${prefix}/constants`,
'@hooks': `${prefix}/hooks`,
'@icons': `${prefix}/components/atoms/Icons`,
'@queries': `${prefix}/queries`,
'@reducers': `${prefix}/state/store/reducers`,
'@slices': `${prefix}/state/slices`,
'@styles': `${prefix}/styles`,
'@utils': `${prefix}/utils`,
'@storybookHelpers': `../.storybook/helpers`,
});
module.exports = aliases;
// usage example at .storybook/webpack.config.js file
const path = require("path");
const alias = require(`../src/config/aliases`);
const SRC = "../src";
const aliases = alias(SRC);
const resolvedAliases = Object.fromEntries(
Object.entries(aliases).map(([key, value]) => [
key,
path.resolve(__dirname, value),
])
);
module.exports = ({ config }) => {
config.resolve.modules.push(path.resolve(__dirname, SRC));
config.resolve.alias = resolvedAliases;
return config;
};
typescript
, typescriptreact
files) typescript
, typescriptreact
反应文件) At tsconfig.json
use the compilerOptions.paths
option, notice that the paths are relative to baseUrl
:在
tsconfig.json
使用compilerOptions.paths
选项,请注意路径是相对于baseUrl
:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@components/*": ["components/*"],
"@config": ["config"],
"@constants": ["constants"],
"@hooks": ["hooks"],
"@styles": ["styles"],
"$types/*": ["types/*"],
"@utils": ["utils"]
}
}
This allows aliases (with IntelliSense), for example:这允许别名(使用 IntelliSense),例如:
// Example of hooks/index.ts file
export * from './useLogin';
export * from './useLocalStorage';
export * from './useAuth';
// Usage examples
import {ROUTES} from '@constants';
import {Text} from '@components/atoms';
import {mixins} from '@styles';
import {useLocalStorage} from '@hooks';
In the settings.json file, add this line:在 settings.json 文件中,添加以下行:
"typescript.preferences.importModuleSpecifier": "non-relative"
If this property is removed, then the ugly relative auto-import is the default option.如果这个属性被移除,那么丑陋的相对自动导入是默认选项。 Simply change 'typescript' to 'javascript' if you're currently using JS.
如果您当前正在使用 JS,只需将“typescript”更改为“javascript”。 To know more about this setting option, just hover on it like this:
要了解有关此设置选项的更多信息,只需 hover 就可以了,如下所示:
I had the right configuration as described by the other answers.我有其他答案所描述的正确配置。 In VS code I restarted the TypeScript server using
Ctrl + Shift + P
-> TypeScript: Restart TS server
command and it fixed the editor highlighting the import path error.在 VS 代码中,我使用
Ctrl + Shift + P
-> TypeScript: Restart TS server
命令,它修复了突出显示导入路径错误的编辑器。
Just for completeness here is what my tsconfig.json looks like:为了完整起见,我的 tsconfig.json 看起来像这样:
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src/**/*"]
}
As a side note, make sure the include
in your jsconfig
/ tsconfig
is pointing to correct paths.作为旁注,请确保
jsconfig
/ tsconfig
中的include
指向正确的路径。
For anyone like me who the other answers aren't working for, these are the tsconfig bits that worked for me, in addition to the settings addition in the accepted answer and ensuring you're setting includes/excludes properly:对于像我这样其他答案不起作用的人,这些是对我有用的 tsconfig 位,除了在接受的答案中添加设置并确保您正确设置包含/排除:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
}
}
Full credit to this gist: https://gist.github.com/EmilyRosina/eef3aa0d66568754a98382121fefa154完全归功于这个要点: https://gist.github.com/EmilyRosina/eef3aa0d66568754a98382121fefa154
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.