Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

静态资源的编译路径问题 #117

Closed
4 tasks done
fwjyttbtx opened this issue Feb 24, 2023 · 9 comments
Closed
4 tasks done

静态资源的编译路径问题 #117

fwjyttbtx opened this issue Feb 24, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@fwjyttbtx
Copy link

Describe the bug

outDir被设置后,编译后静态资源的引用地址会变成相对地址加全路径的样式
outDir那怕设置和默认的输出地址out/main一致也会出现此现象
image

Electron-Vite Version

1.0.19

Electron Version

23.1.0

Vite Version

4.1.4

Validations

@fwjyttbtx fwjyttbtx added the bug Something isn't working label Feb 24, 2023
@alex8088
Copy link
Owner

如何引入静态资源?

@fwjyttbtx
Copy link
Author

使用的文档的?asset引入的资源

import logo from '../../resources/icons/logo.ico?asset';
import logoTemplate from '../../resources/logoTemplate.png?asset';

配置如下:

import { defineConfig } from 'electron-vite';

export default defineConfig(({ mode }) => {
  return {
    main: {
      build: {
        outDir: 'dist/main',
        lib: {
          name: 'mdos',
          entry: 'src/main/index.ts',
        },
      },
    },
})

@fwjyttbtx
Copy link
Author

fwjyttbtx commented Feb 24, 2023

如何引入静态资源?

脚手架下载的项目做配置也会有这样的问题

import { defineConfig, externalizeDepsPlugin } from 'electron-vite'

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      outDir: 'dist/main'
    }
  },
  preload: {
    plugins: [externalizeDepsPlugin()]
  },
  renderer: {}
})

index.js

"use strict";
const electron = require("electron");
const path = require("path");
const utils = require("@electron-toolkit/utils");
const icon = path.join(__dirname, "../../E:/Workspace/VSCodeProjects/electron-vite-app4/resources/icon.png");

@alex8088
Copy link
Owner

初始化项目是否正常?

@alex8088
Copy link
Owner

可重现这个问题,独立修改某个配置outDir,会有此问题。

处理方式有两种:

  1. main/preload/renderer 的 outDir 都配置
  2. 通过CLI处理 electron-vite dev/build/preview --outDir=dist

@alex8088
Copy link
Owner

alex8088 commented Feb 24, 2023

通常要确保main、preload和renderer都要输出到一个目录中。各项独立配置时需要确保一级目录是一致的即是这里的dist, 推荐第二种处理方式, 无需太多配置。此问题待后续版本优化处理。

谢谢反馈问题 🤝

@fwjyttbtx
Copy link
Author

ok, 感谢解答

@alex8088
Copy link
Owner

@fwjyttbtx 提醒一下,因为electron-builder默认构建输出目录是dist,因此将代码输出目录改为dist时,需要修改electron-builder的输出目录(查阅文档),不然会有冲突,会导致打包包裹上一次打包的文件。

@fwjyttbtx
Copy link
Author

@fwjyttbtx 提醒一下,因为electron-builder默认构建输出目录是dist,因此将代码输出目录改为dist时,需要修改electron-builder的输出目录(查阅文档),不然会有冲突,会导致打包包裹上一次打包的文件。

因为改动了构建的输出目录为release所以才会出现这样的问题 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants