Last active
April 8, 2023 06:32
-
-
Save takumus/efea021ac8ef41dc26a70747cf5e8a8b to your computer and use it in GitHub Desktop.
electron-vue-viteのメインプロセスでesmoduleのみ対応のパッケージを使うときに必要なコンフィグ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { rmSync } from "node:fs"; | |
import { defineConfig } from "vite"; | |
import vue from "@vitejs/plugin-vue"; | |
import electron, { | |
Configuration as ElectronConfig, | |
} from "vite-plugin-electron"; | |
import renderer from "vite-plugin-electron-renderer"; | |
import pkg from "./package.json"; | |
import esmodule from "vite-plugin-esmodule"; | |
// https://vitejs.dev/config/ | |
export default defineConfig(({ command }) => { | |
rmSync("dist-electron", { recursive: true, force: true }); | |
const isServe = command === "serve"; | |
const isBuild = command === "build"; | |
const sourcemap = isServe || !!process.env.VSCODE_DEBUG; | |
const esmodules = (() => { | |
let packages: string[] = []; | |
const plugin = esmodule((esms) => { | |
packages = esms.filter((esm) => !(esm in pkg.devDependencies)); | |
return packages; | |
}); | |
return { plugin, packages }; | |
})(); | |
const electronBaseConfig: ElectronConfig = { | |
vite: { | |
plugins: [esmodules.plugin], | |
build: { | |
minify: isBuild, | |
rollupOptions: { | |
external: Object.keys( | |
"dependencies" in pkg ? pkg.dependencies : {} | |
).filter((pkg) => !esmodules.packages.includes(pkg)), | |
}, | |
}, | |
}, | |
}; | |
const electronConfig: { | |
mainProcess: ElectronConfig; | |
preload: ElectronConfig; | |
} = { | |
mainProcess: { | |
...electronBaseConfig, | |
entry: "electron/main/index.ts", | |
onstart(options) { | |
if (process.env.VSCODE_DEBUG) { | |
console.log( | |
/* For `.vscode/.debug.script.mjs` */ "[startup] Electron App" | |
); | |
} else { | |
options.startup(); | |
} | |
}, | |
vite: { | |
...electronBaseConfig.vite, | |
build: { | |
...electronBaseConfig.vite.build, | |
sourcemap, | |
outDir: "dist-electron/main", | |
}, | |
}, | |
}, | |
preload: { | |
...electronBaseConfig, | |
entry: "electron/preload/index.ts", | |
onstart(options) { | |
options.reload(); | |
}, | |
vite: { | |
...electronBaseConfig.vite, | |
build: { | |
...electronBaseConfig.vite.build, | |
sourcemap: sourcemap ? "inline" : undefined, // #332 | |
outDir: "dist-electron/preload", | |
}, | |
}, | |
}, | |
}; | |
return { | |
plugins: [vue(), electron(Object.values(electronConfig)), renderer()], | |
server: | |
process.env.VSCODE_DEBUG && | |
(() => { | |
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL); | |
return { | |
host: url.hostname, | |
port: +url.port, | |
}; | |
})(), | |
clearScreen: false, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment