如何在 VSCode 中智能感知别名模块路径

Den*_*ash 11 javascript intellisense visual-studio-code vscode-settings

我希望 VSCode 能够智能感知模块路径,以便我可以通过单击访问它。

例如,配置后jsconfig.json我可以./src/styled/index通过导入其全局路径来访问。

但我不知道如何使它与别名一起工作 @styles

// VSCode Intellisene Works
import { mixins, theme } from 'styles';

// VSCode Intellisene Doesn't work
import { mixins, theme } from '@styles';
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我的当前jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "jsx": "react",
    "paths": {
      "@styles": ["src/styles/index"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Son*_*yen 69

在 settings.json 文件中,添加以下行:

"typescript.preferences.importModuleSpecifier": "non-relative"
Run Code Online (Sandbox Code Playgroud)

如果删除此属性,则丑陋的相对自动导入将成为默认选项。如果您当前正在使用 JS,请将“typescript”更改为“javascript”。要了解有关此设置选项的更多信息,请将鼠标悬停在其上,如下所示:

在 VSCode 中自动导入 JS/TS


(额外提示)使用以下编译器选项~/为所有内部导入路径添加前缀tsconfig.json

"typescript.preferences.importModuleSpecifier": "non-relative"
Run Code Online (Sandbox Code Playgroud)


Den*_*ash 15

似乎我不得不重新启动 VSCode。


Javascript ( javascript, javascriptreactVSCode 中的文件类型)

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"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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';
Run Code Online (Sandbox Code Playgroud)

将允许导入(使用 IntelliSense):

import { COLORS, mixins, GlobalStyle } from '@styles';
Run Code Online (Sandbox Code Playgroud)

额外的好处是: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;
};
Run Code Online (Sandbox Code Playgroud)

打字稿 ( typescript,typescriptreact文件)

tsconfig.json使用该compilerOptions.paths选项时,请注意路径相对于baseUrl

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@components/*": ["components/*"],
      "@config": ["config"],
      "@constants": ["constants"],
      "@hooks": ["hooks"],
      "@styles": ["styles"],
      "$types/*": ["types/*"],
      "@utils": ["utils"]
    }
}
Run Code Online (Sandbox Code Playgroud)

这允许别名(使用 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';
Run Code Online (Sandbox Code Playgroud)