已经配置了tsconfig.json的alias路径,但是渲染进程使用时候为何还是报错?

在electron 的demo中:
有如下的tsconfig.json的配置,目的是为了在renderer中可以引入使用src/shared/的文件:
image.png

tsconfig.json:

{
   "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@main/*": ["src/main/*"],
        "@renderer/*": ["src/renderer/*"],
        "@shared/*": ["src/shared/*"]
      },

tsconfig.web.json

{
  "extends": "@electron-toolkit/tsconfig/tsconfig.web.json",
  "include": [
    "src/renderer/src/env.d.ts",
    "src/renderer/src/**/*",
    "src/renderer/src/**/*.tsx",
    "src/shared/**/*",
    "src/shared/**/*.ts",
    "src/preload/*.d.ts"
  ],
  "compilerOptions": {
    "composite": true,
    "jsx": "react-jsx",
    "baseUrl": ".",
    "paths": {
      "@renderer/*": [
        "src/renderer/src/*"
      ],
      "@shared/*": ["src/shared/*"]
    }
  }
}

但是在renderer引入使用时候,还是报错:
src/renderer/src/App.tsx

17 |  import { ConfigEntities } from "@shared/db-entities/Config";
   |                                  ^

image.png

===

编辑:1

现有 electron-vite.config.ts 配置如下:

import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import react from '@vitejs/plugin-react'
import path, { resolve } from 'path'

export default defineConfig({
  main: {
    resolve: {
      alias: {
        '@main': path.resolve(__dirname, 'src/main')
      }
    },
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    resolve: {
      alias: {
        '@preload': path.resolve(__dirname, 'src/preload')
      }
    },
    plugins: [externalizeDepsPlugin()]
  },
  shared: {
    resolve: {
      alias: {
        '@shared': path.resolve(__dirname, 'src/shared')
      }
    },
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    resolve: {
      alias: {
        '@renderer': path.resolve(__dirname, 'src/main'),
      }
    },
    plugins: [react()]
  }
})

但是还是会报错:

image.png

阅读 659
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏