alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.57k stars 153 forks source link

开发环境正常,预览/打包的时候提示 Failed to resolve module specifier "vue" #584

Closed JaguarJack closed 3 months ago

JaguarJack commented 3 months ago

Describe the bug

我想通过咱这个框架,将目前的一个后台管理系统导入(基于 Vue3 和 Vite 的项目,组件都是自动导入的)。在开发环境可以成功,但是打包之后出现了下面的错误

 Failed to resolve module specifier "vue".Relative references must start with either "/", "./", or "../".

查找了相关 issue,没有找到相关解决方案。暂时摸不着头绪,是否可以看下

这是 vite config 配置

import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import alias from '@rollup/plugin-alias'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
// mock server
import Icons from 'unplugin-icons/vite'
const rootPath = resolve(__dirname)

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {
    plugins: [
      externalizeDepsPlugin(),
      vue({
        script: {
          defineModel: true
        }
      }),
      vueJsx(),
      alias({
        entries: [
          {
            find: '@',
            replacement: resolve(rootPath, './src/renderer/src')
          }
        ]
      }),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia', '@vueuse/core']
        // resolvers: [ ElementPlusResolver({importStyle: 'sass'}) ]
      }),
      Components({
        dirs: ['src/components/', 'src/layout/'],
        extensions: ['vue'],
        // directoryAsNamespace: true,
        deep: true,
        dts: true,
        include: [/\.vue$/, /\.vue\?vue/],
        exclude: [
          /[\\/]node_modules[\\/]/,
          /[\\/]\.git[\\/]/,
          /[\\/]\.nuxt[\\/]/,
          'src/components/catchForm/'
        ]
        // resolvers: [ ElementPlusResolver({ importStyle: 'sass'}) ]
      }),
      Icons({
        compiler: 'vue3',
        autoInstall: true
      })
    ],
    publicDir: './src/renderer/src/public',
    define: {
      // BASE_URL: env.BASE_URL
    },
    server: {
      host: '127.0.0.1',
      port: 10086,
      open: true, // 自动打开浏览器
      cors: true, // 允许跨域
      strictPort: true // 端口占用直接退出
    },
    build: {
      chunkSizeWarningLimit: 2000,
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: false,
          pure_funcs: ['console.log', 'console.info'],
          drop_debugger: true
        }
      },
      assetsDir: 'assets',
      rollupOptions: {
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',

          entryFileNames: 'assets/js/[name]-[hash].js',

          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
        }
      }
    }
  }
})

Electron-Vite Version

latest

Electron Version

latest

Vite Version

latest

Validations

alex8088 commented 3 months ago

应是代码问题,自行检查是否都使用es语法。或者自行检索,有很多类似问题。

JaguarJack commented 3 months ago

应是代码问题,自行检查是否都使用es语法。或者自行检索,有很多类似问题。

谢谢,已经解决了