简体   繁体   English

ViteJS 别名导入没有解析

[英]ViteJS aliased imports are not resolving

*Converting a large monorepo ejected react app to vitejs, at least i am attempting to do so. *将大型 monorepo 弹出的 React 应用程序转换为 vitejs,至少我正在尝试这样做。 However we use several aliased imports throughout the current app and i would like to leave that functionality there.但是,我们在整个当前应用程序中使用了多个别名导入,我想将该功能留在那里。 We are currently using webpack.我们目前使用的是 webpack。

ts.config file ts.config文件

  {
  "compilerOptions": {
    "target": "esnext",
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "noEmit": true,
    "jsx": "preserve",
    "lib": ["esnext", "dom"],
    "baseUrl": ".",
    "paths": {
      "src/*": ["src/*"],
      "@src/*": ["src/*"],
      "@status/*": ["src/packages/status/*"],
      "@cli/*": ["src/packages/cli/*"],
    }
  },
  "include": [
    "./declarations.d.ts",
    "src",
  ],
}

vite.config file: vite.config 文件:


    import react from '@vitejs/plugin-react';
    import legacy from '@vitejs/plugin-legacy'
    import path from 'path';
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      root: 'src',
      publicDir: './src/assets',
      plugins: [
        react({
          // Use React plugin in all *.jsx and *.tsx files
          include: '**/*.{jsx,tsx}',
          babel: {
            configFile: true,
          },
        }),
        legacy({
          targets: ['defaults', 'not IE 11']
        })
      ],
      resolve: {
        alias: {
          '@src': path.resolve(__dirname, './src'),
          '@status': path.resolve(__dirname, './src/packages/status'),
          '@cli': path.resolve(__dirname, './src/packages/cli')
        },
    
      },
      build: {
        outDir: 'build',
      },
      css: {
        preprocessorOptions: {
          scss: {
            quietDeps: true,
            javascriptEnabled: true,
          },
          sass: {
            quietDeps: true,
            javascriptEnabled: true,
          },
        },
      },
    });

Project structure is as follows:项目结构如下:

 package.json
   vite.config
   ts.config
   src/
      packages/
      pages/
      index.html
      app/
         app.tsx

The specific error i am seeing in the console when i run vite serve src is当我运行 vite serve src 时,我在控制台中看到的具体错误是

6:33:25 PM [vite] Internal server error: Failed to resolve import "@cli/constants/ReviewText" from "src/packages/cli/pages/review/Review.tsx".下午 6:33:25 [vite] 内部服务器错误:无法从“src/packages/cli/pages/review/Review.tsx”解析导入“@cli/constants/ReviewText”。 Does the file exist?该文件是否存在?

Have scoured the interned and tried pretty much every variation of declaring the aliases in the vite.config and ts.config files.搜索了实习生并尝试了几乎所有在 vite.config 和 ts.config 文件中声明别名的变体。 What is weird is that it seems that the ts compiler is at least recognizing the aliased path as it shows me it the full path when hovering over import statements, so i believe my problem is with the vite.config aliasing...奇怪的是,ts 编译器似乎至少识别了别名路径,因为它在将鼠标悬停在导入语句上时向我显示了完整路径,所以我相信我的问题是 vite.config 别名...

I also came across this issue and I found a solution.我也遇到了这个问题,并找到了解决方案。 It turns out Vite makes use of absolute paths when resolving modules and Vite also treats "src/*" as a package in node_modules and this is what causes this import resolution issues.事实证明,Vite 在解析模块时使用了绝对路径,并且 Vite 还在 node_modules 中将"src/*"视为node_modules ,这就是导致此导入解析问题的原因。

To fix it, What I did was make this change to my tsconfig.json要修复它,我所做的是对我的 tsconfig.json 进行此更改

    // tsconfig.json
    "baseUrl": ".",
        "paths": {
            "/src/*": ["src/*"]
        }

Now I can import any module I want as below现在我可以导入我想要的任何模块,如下所示

// App.tsx
import { Home } from "/src/pages/Home";

The key here is to prefix all your paths with a / .这里的关键是在所有路径前加上/ In your case it would be在你的情况下是

   "paths": {
      "/src/*": ["src/*"],
      "/@src/*": ["src/*"],
      "/@status/*": ["src/packages/status/*"],
      "/@cli/*": ["src/packages/cli/*"],
    }

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

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