vite-plugin-electron icon indicating copy to clipboard operation
vite-plugin-electron copied to clipboard

How to import static resources in the Main process and keep them as URLs?

Open oubenruing opened this issue 1 year ago • 0 comments

As shown in the code below


import { app, Menu, nativeImage, Tray } from 'electron'
import icon from '../../assets/logo.png'

console.log(icon)
// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAA.....
const tray = new Tray(nativeImage.createFromPath(icon))


I used Vite's import to import an image, but after the build, it gets packaged as a base64 string. I want to keep it as a URL. What should I do? Also, I added assetsInlineLimit: 0 in the Electron plugin, but it didn't work.

export default defineConfig(({ command }) => {
  return {
    plugins: [
      // ...
      electron({
        main: {
          // Shortcut of `build.lib.entry`
          entry: 'electron/main/index.ts',
          onstart({ startup }) {
            if (process.env.VSCODE_DEBUG) {
              console.log(/* For `.vscode/.debug.script.mjs` */ '[startup] Electron App')
            } else {
              startup()
            }
          },
          vite: {
            build: {
              sourcemap,
              minify: isBuild,
              outDir: 'dist-electron/main',
+             assetsInlineLimit: 0,
              rollupOptions: {
                // Some third-party Node.js libraries may not be built correctly by Vite, especially `C/C++` addons,
                // we can use `external` to exclude them to ensure they work correctly.
                // Others need to put them in `dependencies` to ensure they are collected into `app.asar` after the app is built.
                // Of course, this is not absolute, just this way is relatively simple. :)
                // output: { format: 'es' },
                external: Object.keys('dependencies' in pkg ? pkg.dependencies : {})
              }
            }
          }
        },
      }
//...

It can be reproduced https://github.com/oubenruing/test-electron

oubenruing avatar May 20 '24 06:05 oubenruing