mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-11-02 14:51:51 +08:00
Compare commits
No commits in common. "master" and "v5.0.0-beta.3" have entirely different histories.
master
...
v5.0.0-bet
137
CHANGELOG.md
137
CHANGELOG.md
@ -1,140 +1,49 @@
|
||||
### v6.0.0 (_2026-10-15_)
|
||||
|
||||
- feat(bytecode): support RegExp for chunkAlias option ([#885](https://github.com/alex8088/electron-vite/issues/885))
|
||||
- refactor: share and normalize build output resolution
|
||||
- refactor: inline bytecode build plugin name
|
||||
- refactor!: invoke the bytecode compiler only once to compile all code
|
||||
- refactor: use pipe stdio for spawn Electron bytecode compliler and append stderr error
|
||||
- refactor: use rolldown instead of esbuild to bundle config file
|
||||
- refactor: compile bytecode using the Electron main process or renderer process rather then via the `ELECTRON_RUN_AS_NODE`
|
||||
- refactor!: simplify resolve config and isolate user config for sub-builds
|
||||
- refactor: `?modulePath` sub-build output strategy
|
||||
- perf(swcPlugin): skip redundant minification when using Oxc ([#922](https://github.com/alex8088/electron-vite/issues/922))
|
||||
- perf: skip Babel loading in development and builds without bytecode
|
||||
- perf: short-circuit bytecode detection in builds
|
||||
- perf: setup rollupOption compatibility via config factory
|
||||
- perf: build compatibility target for Electron 42, 43, 44
|
||||
- fix(asset): use consistent file paths for asset cache lookups
|
||||
- fix(bytecode): unable to obtain the bytecode build session in Node.js 24 ([#928](https://github.com/alex8088/electron-vite/issues/928))
|
||||
- fix(dev): exit non-zero when Electron is killed by a signal ([#918](https://github.com/alex8088/electron-vite/issues/918))
|
||||
- fix(dev): forward termination signals to Electron child ([#900](https://github.com/alex8088/electron-vite/issues/900))
|
||||
- fix(dev): improve Electron process signal handling
|
||||
- fix(bytecode): set the regular expression's `lastIndex` to 0 to perform a global match for chunkAlias option
|
||||
- fix(esmShim)!: use `this.parse` to find the last static import ([#906](https://github.com/alex8088/electron-vite/issues/906))
|
||||
- fix(deps)!: downgraded @babel/* to v7 due to Node requirements
|
||||
- fix: remove useless preset config
|
||||
- fix(swcPlugin): also disable oxc on Vite 8 ([#920](https://github.com/alex8088/electron-vite/issues/920))
|
||||
- fix: clone config without plugins [#902](https://github.com/alex8088/electron-vite/issues/902)
|
||||
- fix(deps)!: update magic-string to v1
|
||||
- fix(deps)!: update @babel/* to v8
|
||||
- fix!: compatible with rollupOptions and rolldownOptions
|
||||
- fix: chunk filename is wrong in ES mode
|
||||
- fix(asset): isolate asset caches per environment
|
||||
- fix: use the environment api in plugins
|
||||
- fix: skip TTY terminal manipulations in CI environments
|
||||
- fix(deps)!: update vite to v8 ([#894](https://github.com/alex8088/electron-vite/issues/894))
|
||||
- fix: Typescript compiler option 'outDir' must be located inside Rollup 'dir' option
|
||||
- fix(isolateEntries): handle non-TTY stdout in build reporter (#887)
|
||||
- chore: simplify bytecode compiler initialization
|
||||
- chore: improve jsdoc for bytecode options ([#898](https://github.com/alex8088/electron-vite/issues/898))
|
||||
- chore(deps): update all non-major dependencies
|
||||
- chore(deps): update lint-staged to v17
|
||||
- chore(deps): update cac to v7
|
||||
- chore(deps): update globals to v17
|
||||
- chore(deps): update eslint and @eslint/js to v10
|
||||
- chore(deps): update all non-major dependencies
|
||||
|
||||
### v6.0.0-beta.8 (_2026-10-11_)
|
||||
|
||||
See [v6.0.0-beta.8 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.8/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.7 (_2026-10-08_)
|
||||
|
||||
See [v6.0.0-beta.7 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.7/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.6 (_2026-10-07_)
|
||||
|
||||
See [v6.0.0-beta.6 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.6/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.5 (_2026-09-29_)
|
||||
|
||||
See [v6.0.0-beta.5 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.5/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.4 (_2026-09-28_)
|
||||
|
||||
See [v6.0.0-beta.4 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.4/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.3 (_2026-09-27_)
|
||||
|
||||
See [v6.0.0-beta.3 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.3/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.2 (_2026-09-27_)
|
||||
|
||||
See [v6.0.0-beta.2 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.2/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.1 (_2026-04-12_)
|
||||
|
||||
See [v6.0.0-beta.1 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.1/CHANGELOG.md)
|
||||
|
||||
### v6.0.0-beta.0 (_2026-04-09_)
|
||||
|
||||
See [v6.0.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v6.0.0-beta.0/CHANGELOG.md)
|
||||
|
||||
### v5.0.0 (_2025-12-07_)
|
||||
### v5.0.0-beta.3 (_2025-11-01_)
|
||||
|
||||
- feat(config): add `build.externalizeDeps` and `build.bytecode` config options to replace `externalizeDepsPlugin` and `bytecodePlugin`
|
||||
- feat: reporter plugin for isolated builds
|
||||
- feat: enhanced string protection
|
||||
- feat: add `isolatedEntries` option for `preload` and `renderer` to build entries as standalone bundles [#154](https://github.com/alex8088/electron-vite/issues/154)
|
||||
- refactor(config): move the `isolateEntries` options to the `build` option
|
||||
- refactor: deprecated `externalizeDepsPlugin` and `bytecodePlugin`
|
||||
- refactor(config)!: remove function resolution for nested config fields
|
||||
- perf(buildReport): exclude node_modules from watch list
|
||||
- fix(modulePath): rewrite the build input instead of merging
|
||||
|
||||
### v5.0.0-beta.2 (_2025-10-30_)
|
||||
|
||||
- feat: reporter plugin for isolated builds
|
||||
- perf(isolateEntries): transform log
|
||||
- perf(isolateEntries): optimize entries transformation
|
||||
- fix(asset): normalize imported public asset chunk path
|
||||
- fix: avoid duplicate chunk emission
|
||||
|
||||
### v5.0.0-beta.1 (_2025-10-29_)
|
||||
|
||||
- feat: enhanced string protection
|
||||
- feat: add `isolatedEntries` option for `preload` and `renderer` to build entries as standalone bundles [#154](https://github.com/alex8088/electron-vite/issues/154)
|
||||
- refactor(asset): remove redundant path normalization
|
||||
- refactor: split electron plugin into preset and validator plugins
|
||||
- refactor(config)!: restructure Electron Vite config interfaces
|
||||
- refactor(build): simplify build logic
|
||||
- refactor: replace `JSON.parse/stringify` with manual deep clone
|
||||
- perf: build compatibility target for Electron 39
|
||||
- fix(modulePath): prevent duplicate asset emission
|
||||
- fix(modulePath): support watch mode
|
||||
- chore: improve logging message clarity and consistency
|
||||
|
||||
### v5.0.0-beta.0 (_2025-10-19_)
|
||||
|
||||
- refactor(bytecodePlugin): improved bytecode bundle generation and made a new string protection plugin
|
||||
- refactor(modulePath): better support for tree-shaking and code-splitting
|
||||
- refactor: remove Electron 18, 19, 20, 21 build compatilibity target
|
||||
- perf(buildReport): exclude node_modules from watch list
|
||||
- perf(isolateEntries): transform log
|
||||
- perf(isolateEntries): optimize entries transformation
|
||||
- perf: build compatibility target for Electron 39
|
||||
- perf(plugin): more efficient module filtering via regular expressions
|
||||
- perf(plugin): no need to cache `sourcemap` option
|
||||
- perf(plugin): lazily initialize `MagicString` and remove the redundant pre-check
|
||||
- perf(bytecodePlugin): better way to count bytecode chunks
|
||||
- perf(plugin): enhance path resolution using `import.meta.dirname` for ES modules
|
||||
- fix(modulePath): rewrite the build input instead of merging
|
||||
- fix(asset): normalize imported public asset chunk path
|
||||
- fix: avoid duplicate chunk emission
|
||||
- fix(modulePath): prevent duplicate asset emission
|
||||
- fix(modulePath): support watch mode
|
||||
- chore: fix jsdoc
|
||||
- chore: improve logging message clarity and consistency
|
||||
- chore(deps): update all non-major dependencies
|
||||
- chore: update eslint config
|
||||
- chore: replace `tseslint.config` with `defineConfig`
|
||||
- chore: remove redundant external id
|
||||
- chore: rename the file `esm` to `esmShim`
|
||||
- docs: update
|
||||
|
||||
### v5.0.0-beta.3 (_2025-11-01_)
|
||||
|
||||
See [v5.0.0-beta.3 changelog](https://github.com/alex8088/electron-vite/blob/v5.0.0-beta.3/CHANGELOG.md)
|
||||
|
||||
### v5.0.0-beta.2 (_2025-10-30_)
|
||||
|
||||
See [v5.0.0-beta.2 changelog](https://github.com/alex8088/electron-vite/blob/v5.0.0-beta.2/CHANGELOG.md)
|
||||
|
||||
### v5.0.0-beta.1 (_2025-10-29_)
|
||||
|
||||
See [v5.0.0-beta.1 changelog](https://github.com/alex8088/electron-vite/blob/v5.0.0-beta.1/CHANGELOG.md)
|
||||
|
||||
### v5.0.0-beta.0 (_2025-10-19_)
|
||||
|
||||
See [v5.0.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v5.0.0-beta.0/CHANGELOG.md)
|
||||
|
||||
### v4.0.1 (_2025-09-21_)
|
||||
|
||||
|
||||
16
README.md
16
README.md
@ -28,13 +28,13 @@
|
||||
## Features
|
||||
|
||||
- ⚡️ [Vite](https://vitejs.dev) powered and use the same way.
|
||||
- 🛠 Pre-configure with sensible defaults optimized for Electron.
|
||||
- 💡 Optimize asset handling for Electron main process.
|
||||
- 🚀 Fast HMR & hot reloading.
|
||||
- 🔥 Isolated build for multi-entry application development.
|
||||
- ✨ Simplify multi-threading development.
|
||||
- 🔒 Compile code to v8 bytecode to protect source code.
|
||||
- 🔌 Easy to debug in IDEs such as VSCode or WebStorm.
|
||||
- 🛠 Pre-configured for Electron, don't worry about configuration.
|
||||
- 💡 Optimize asset handling (Node.js addons, WebAssembly, Worker Thread, etc).
|
||||
- 🚀 Fast HMR for renderer processes.
|
||||
- 🔥 Hot reloading for main process and preload scripts.
|
||||
- 🔌 Easy to debug in IDEs like VSCode or WebStorm.
|
||||
- 🔒 Compile to v8 bytecode to protect source code.
|
||||
- 🏷️ Support for TypeScript decorators.
|
||||
- 📦 Out-of-the-box support for TypeScript, Vue, React, Svelte, SolidJS and more.
|
||||
|
||||
## Usage
|
||||
@ -83,7 +83,7 @@ export default {
|
||||
Clone the [electron-vite-boilerplate](https://github.com/alex8088/electron-vite-boilerplate) or use the [create-electron](https://github.com/alex8088/quick-start/tree/master/packages/create-electron) tool to scaffold your project.
|
||||
|
||||
```bash
|
||||
npm create @quick-start/electron@latest
|
||||
npm create @quick-start/electron
|
||||
```
|
||||
|
||||
Currently supported template presets include:
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
const vm = require('node:vm')
|
||||
const v8 = require('node:v8')
|
||||
|
||||
v8.setFlagsFromString('--no-lazy')
|
||||
v8.setFlagsFromString('--no-flush-bytecode')
|
||||
|
||||
const params = ['exports', 'require', 'module', '__filename', '__dirname']
|
||||
|
||||
module.exports = function compile(code) {
|
||||
if (typeof code !== 'string') throw new TypeError('Compilation source must be a string')
|
||||
const { cachedData } = vm.compileFunction(code, params, { produceCachedData: true })
|
||||
if (!Buffer.isBuffer(cachedData)) throw new Error('V8 did not produce cached data')
|
||||
// Explicit wire format works across both Node IPC and Electron structured clone.
|
||||
return { sourceLength: code.length, cachedData: cachedData.toString('base64') }
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
const { app, BrowserWindow, ipcMain } = require('electron')
|
||||
const path = require('node:path')
|
||||
const RpcPeer = require('./rpc-peer.cjs')
|
||||
const compile = require('./bytecode-compiler.cjs')
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
app.dock.hide()
|
||||
}
|
||||
|
||||
const channel = 'electron-vite:bytecode:rpc'
|
||||
const readyChannel = 'electron-vite:bytecode:ready'
|
||||
let win
|
||||
let rendererRPC
|
||||
let rendererStarting
|
||||
let stopping = false
|
||||
|
||||
function fail(error) {
|
||||
console.error(error)
|
||||
parentRPC.close(error)
|
||||
rendererRPC?.close(error)
|
||||
app.exit(1)
|
||||
}
|
||||
|
||||
const parentRPC = new RpcPeer(
|
||||
message =>
|
||||
new Promise((resolve, reject) => {
|
||||
if (!process.connected || !process.send) return reject(new Error('Parent IPC is disconnected'))
|
||||
process.send(message, error => (error ? reject(error) : resolve()))
|
||||
})
|
||||
)
|
||||
|
||||
function createRenderer() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let ready = false
|
||||
const timer = setTimeout(() => finish(new Error('Compiler preload readiness timed out')), 30000)
|
||||
function finish(error) {
|
||||
clearTimeout(timer)
|
||||
ipcMain.removeListener(readyChannel, onReady)
|
||||
if (error) reject(error)
|
||||
else {
|
||||
ready = true
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
function onReady(event) {
|
||||
if (event.sender === win.webContents) finish()
|
||||
}
|
||||
function onFailure(error) {
|
||||
if (stopping) return
|
||||
if (ready) fail(error)
|
||||
else finish(error)
|
||||
}
|
||||
ipcMain.on(readyChannel, onReady)
|
||||
win = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'electron-bytecode-preload.cjs'),
|
||||
sandbox: false,
|
||||
contextIsolation: true
|
||||
}
|
||||
})
|
||||
rendererRPC = new RpcPeer(message => {
|
||||
if (win.isDestroyed()) throw new Error('Compiler window is closed')
|
||||
win.webContents.send(channel, message)
|
||||
})
|
||||
ipcMain.on(channel, (event, message) => {
|
||||
if (!win.isDestroyed() && event.sender === win.webContents) void rendererRPC.receive(message)
|
||||
})
|
||||
win.webContents.on('preload-error', (_event, _path, error) => onFailure(error))
|
||||
win.webContents.on('render-process-gone', (_event, details) => {
|
||||
onFailure(new Error(`Compiler renderer exited: ${details.reason}`))
|
||||
})
|
||||
win.on('closed', () => onFailure(new Error('Compiler window closed unexpectedly')))
|
||||
win.loadURL('data:text/html,<!doctype html><html></html>').catch(onFailure)
|
||||
})
|
||||
}
|
||||
|
||||
const initialized = app.whenReady().then(() => true)
|
||||
parentRPC.register('ready', () => initialized)
|
||||
parentRPC.register('compile', async (target, code) => {
|
||||
await initialized
|
||||
if (target === 'main') return compile(code)
|
||||
if (target === 'preload') {
|
||||
rendererStarting ||= createRenderer()
|
||||
await rendererStarting
|
||||
return rendererRPC.request('compile', code)
|
||||
}
|
||||
throw new Error(`Invalid bytecode target: ${target}`)
|
||||
})
|
||||
initialized.catch(fail)
|
||||
|
||||
process.on('message', message => {
|
||||
if (message?.type === 'shutdown') {
|
||||
stopping = true
|
||||
app.quit()
|
||||
} else {
|
||||
void parentRPC.receive(message)
|
||||
}
|
||||
})
|
||||
process.on('disconnect', () => {
|
||||
stopping = true
|
||||
app.quit()
|
||||
})
|
||||
app.on('before-quit', () => {
|
||||
stopping = true
|
||||
parentRPC.close()
|
||||
rendererRPC?.close()
|
||||
})
|
||||
if (!process.send) fail(new Error('Bytecode compiler must be launched with an IPC channel'))
|
||||
@ -1,10 +0,0 @@
|
||||
const { ipcRenderer } = require('electron')
|
||||
const RpcPeer = require('./rpc-peer.cjs')
|
||||
const compile = require('./bytecode-compiler.cjs')
|
||||
|
||||
const channel = 'electron-vite:bytecode:rpc'
|
||||
const rpc = new RpcPeer(message => ipcRenderer.send(channel, message))
|
||||
rpc.register('ready', () => true)
|
||||
rpc.register('compile', compile)
|
||||
ipcRenderer.on(channel, (_event, message) => void rpc.receive(message))
|
||||
ipcRenderer.send('electron-vite:bytecode:ready')
|
||||
@ -1,89 +0,0 @@
|
||||
// Transport-independent peer. Messages must be JSON serializable.
|
||||
class RpcPeer {
|
||||
constructor(send, timeout = 30000) {
|
||||
this.send = send
|
||||
this.timeout = timeout
|
||||
this.methods = new Map()
|
||||
this.pending = new Map()
|
||||
this.nextId = 0
|
||||
this.closed = false
|
||||
}
|
||||
|
||||
register(cmd, method) {
|
||||
this.methods.set(cmd, method)
|
||||
return this
|
||||
}
|
||||
|
||||
request(cmd, ...args) {
|
||||
if (this.closed) return Promise.reject(new Error('RPC connection is closed'))
|
||||
const invocationId = this.nextId++
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(invocationId)
|
||||
reject(new Error(`RPC request timed out: ${cmd}`))
|
||||
}, this.timeout)
|
||||
this.pending.set(invocationId, { resolve, reject, timer })
|
||||
Promise.resolve()
|
||||
.then(() => this.send({ type: 'request', invocationId, cmd, args }))
|
||||
.catch(error => {
|
||||
const pending = this.pending.get(invocationId)
|
||||
if (!pending) return
|
||||
clearTimeout(timer)
|
||||
this.pending.delete(invocationId)
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async receive(message) {
|
||||
if (this.closed || !message || !Number.isSafeInteger(message.invocationId)) return
|
||||
const { type, invocationId } = message
|
||||
if (type === 'response') {
|
||||
const pending = this.pending.get(invocationId)
|
||||
if (!pending) return
|
||||
this.pending.delete(invocationId)
|
||||
clearTimeout(pending.timer)
|
||||
if (message.error) {
|
||||
const error = new Error(message.error.message)
|
||||
error.name = message.error.name || 'Error'
|
||||
if (message.error.stack) error.stack = message.error.stack
|
||||
pending.reject(error)
|
||||
} else {
|
||||
pending.resolve(message.result)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (type !== 'request' || typeof message.cmd !== 'string' || !Array.isArray(message.args)) return
|
||||
const response = { type: 'response', invocationId }
|
||||
try {
|
||||
const method = this.methods.get(message.cmd)
|
||||
if (!method) throw new Error(`Invalid method: ${message.cmd}`)
|
||||
response.result = await method(...message.args)
|
||||
} catch (error) {
|
||||
response.error = {
|
||||
name: error instanceof Error ? error.name : 'Error',
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined
|
||||
}
|
||||
}
|
||||
if (!this.closed) {
|
||||
try {
|
||||
await this.send(response)
|
||||
} catch (error) {
|
||||
this.close(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
close(error = new Error('RPC connection is closed')) {
|
||||
this.closed = true
|
||||
for (const pending of this.pending.values()) {
|
||||
clearTimeout(pending.timer)
|
||||
pending.reject(error)
|
||||
}
|
||||
this.pending.clear()
|
||||
this.methods.clear()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RpcPeer
|
||||
45
package.json
45
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "electron-vite",
|
||||
"version": "6.0.0",
|
||||
"version": "5.0.0-beta.3",
|
||||
"description": "Electron build tooling based on Vite",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
@ -61,7 +61,7 @@
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@swc/core": "^1.0.0",
|
||||
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
||||
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@swc/core": {
|
||||
@ -69,39 +69,40 @@
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^10.0.1",
|
||||
"@eslint/js": "^9.37.0",
|
||||
"@rollup/plugin-json": "^6.1.0",
|
||||
"@rollup/plugin-node-resolve": "^16.0.3",
|
||||
"@rollup/plugin-typescript": "^12.3.0",
|
||||
"@swc/core": "^1.16.2",
|
||||
"@rollup/plugin-typescript": "^12.1.4",
|
||||
"@swc/core": "^1.13.5",
|
||||
"@types/babel__core": "^7.20.5",
|
||||
"@types/node": "^22.20.4",
|
||||
"eslint": "^10.11.0",
|
||||
"@types/node": "^22.18.11",
|
||||
"eslint": "^9.37.0",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"eslint-plugin-prettier": "^5.5.6",
|
||||
"globals": "^17.12.0",
|
||||
"lint-staged": "^17.5.1",
|
||||
"prettier": "^3.9.9",
|
||||
"rollup": "^4.63.5",
|
||||
"rollup-plugin-dts": "^6.5.1",
|
||||
"eslint-plugin-prettier": "^5.5.4",
|
||||
"globals": "^16.4.0",
|
||||
"lint-staged": "^16.2.4",
|
||||
"prettier": "^3.6.2",
|
||||
"rollup": "^4.52.4",
|
||||
"rollup-plugin-dts": "^6.2.3",
|
||||
"rollup-plugin-rm": "^1.0.2",
|
||||
"simple-git-hooks": "^2.14.0",
|
||||
"simple-git-hooks": "^2.13.1",
|
||||
"tslib": "^2.8.1",
|
||||
"typescript": "^5.9.3",
|
||||
"typescript-eslint": "^8.70.1",
|
||||
"vite": "^8.3.1"
|
||||
"typescript-eslint": "^8.46.1",
|
||||
"vite": "^7.1.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.29.7",
|
||||
"@babel/plugin-transform-arrow-functions": "^7.29.7",
|
||||
"cac": "^7.0.0",
|
||||
"magic-string": "^1.4.2",
|
||||
"picocolors": "^1.1.1",
|
||||
"rolldown": "~1.2.10"
|
||||
"@babel/core": "^7.28.4",
|
||||
"@babel/plugin-transform-arrow-functions": "^7.27.1",
|
||||
"cac": "^6.7.14",
|
||||
"esbuild": "^0.25.11",
|
||||
"magic-string": "^0.30.19",
|
||||
"picocolors": "^1.1.1"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"@swc/core",
|
||||
"esbuild",
|
||||
"simple-git-hooks"
|
||||
]
|
||||
}
|
||||
|
||||
2429
pnpm-lock.yaml
generated
2429
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -26,7 +26,7 @@ export default defineConfig([
|
||||
plugins: [
|
||||
rm('dist', 'buildStart'),
|
||||
json(),
|
||||
ts({ compilerOptions: { rootDir: 'src', outDir: 'dist', declaration: true, declarationDir: 'dist/types' } }),
|
||||
ts({ compilerOptions: { rootDir: 'src', declaration: true, declarationDir: 'dist/types' } }),
|
||||
resolve()
|
||||
],
|
||||
treeshake: {
|
||||
@ -35,7 +35,6 @@ export default defineConfig([
|
||||
},
|
||||
{
|
||||
input: 'dist/types/index.d.ts',
|
||||
external: ['vite'],
|
||||
output: [{ file: pkg.types, format: 'es' }],
|
||||
plugins: [dts(), rm('dist/types', 'buildEnd')]
|
||||
}
|
||||
|
||||
52
src/build.ts
52
src/build.ts
@ -1,56 +1,28 @@
|
||||
import { build as viteBuild, mergeConfig } from 'vite'
|
||||
import { type InlineConfig, type MainViteConfig, type PreloadViteConfig, resolveConfig } from './config'
|
||||
import { BytecodeCompiler } from './bytecodeCompiler'
|
||||
import { asyncFlatten } from './utils'
|
||||
|
||||
async function usesBytecode(config?: MainViteConfig | PreloadViteConfig): Promise<boolean> {
|
||||
if (!config) return false
|
||||
if (config.build?.bytecode) return true
|
||||
const plugins = await asyncFlatten(config.plugins || [])
|
||||
return plugins.some(plugin => plugin && typeof plugin === 'object' && plugin.name === 'vite:bytecode')
|
||||
}
|
||||
import { build as viteBuild } from 'vite'
|
||||
import { type InlineConfig, resolveConfig } from './config'
|
||||
|
||||
/**
|
||||
* Bundles the electron app for production.
|
||||
*/
|
||||
export async function build(inlineConfig: InlineConfig = {}): Promise<void> {
|
||||
process.env.NODE_ENV_ELECTRON_VITE = 'production'
|
||||
|
||||
const bytecodeCompiler = new BytecodeCompiler()
|
||||
const config = await resolveConfig(
|
||||
mergeConfig(inlineConfig, {
|
||||
plugins: [{ name: 'vite:electron-bytecode-build', api: { compiler: bytecodeCompiler } }]
|
||||
}),
|
||||
'build',
|
||||
'production'
|
||||
)
|
||||
const config = await resolveConfig(inlineConfig, 'build', 'production')
|
||||
|
||||
if (!config.config) {
|
||||
return
|
||||
}
|
||||
|
||||
const hasBytecode = (await usesBytecode(config.config.main)) || (await usesBytecode(config.config.preload))
|
||||
// Build targets in order: main -> preload -> renderer
|
||||
const buildTargets = ['main', 'preload', 'renderer'] as const
|
||||
|
||||
const buildTargets = async (): Promise<void> => {
|
||||
// Build targets in order: main -> preload -> renderer
|
||||
for (const target of ['main', 'preload', 'renderer'] as const) {
|
||||
const viteConfig = config.config?.[target]
|
||||
if (viteConfig) {
|
||||
// Disable watch mode in production builds
|
||||
if (viteConfig.build?.watch) {
|
||||
viteConfig.build.watch = null
|
||||
}
|
||||
await viteBuild(viteConfig)
|
||||
for (const target of buildTargets) {
|
||||
const viteConfig = config.config[target]
|
||||
if (viteConfig) {
|
||||
// Disable watch mode in production builds
|
||||
if (viteConfig.build?.watch) {
|
||||
viteConfig.build.watch = null
|
||||
}
|
||||
await viteBuild(viteConfig)
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasBytecode) return buildTargets()
|
||||
|
||||
try {
|
||||
await bytecodeCompiler.start()
|
||||
await buildTargets()
|
||||
} finally {
|
||||
await bytecodeCompiler.stop()
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,188 +0,0 @@
|
||||
import { type ChildProcess, spawn } from 'node:child_process'
|
||||
import path from 'node:path'
|
||||
import { createRequire } from 'node:module'
|
||||
import { getElectronPath } from './electron'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const RPC_TIMEOUT = 30_000
|
||||
const SHUTDOWN_TIMEOUT = 3_000
|
||||
|
||||
export type BytecodeCompileResult = {
|
||||
sourceLength: number
|
||||
/** Base64-encoded V8 cachedData; decode with Buffer.from(cachedData, 'base64'). */
|
||||
cachedData: string
|
||||
}
|
||||
|
||||
export type BytecodeTarget = 'main' | 'preload'
|
||||
|
||||
type CompilerResponse = {
|
||||
type: 'response'
|
||||
invocationId: number
|
||||
result?: unknown
|
||||
error?: { name: string; message: string; stack?: string }
|
||||
}
|
||||
|
||||
type PendingRequest = {
|
||||
resolve: (value: unknown) => void
|
||||
reject: (reason: Error) => void
|
||||
timer: ReturnType<typeof setTimeout>
|
||||
}
|
||||
|
||||
export class BytecodeCompiler {
|
||||
private ps?: ChildProcess
|
||||
private rpcMap = new Map<number, PendingRequest>()
|
||||
private invocationId = 0
|
||||
private starting?: Promise<void>
|
||||
private stopping?: Promise<void>
|
||||
private ready = false
|
||||
private stderr = ''
|
||||
|
||||
start(): Promise<void> {
|
||||
if (this.stopping) return Promise.reject(new Error('BytecodeCompiler is stopping'))
|
||||
if (this.starting) return this.starting
|
||||
if (this.ready) return Promise.resolve()
|
||||
if (this.ps) return Promise.reject(new Error('Previous Electron process has not closed; await stop() first'))
|
||||
this.starting = this.initialize().finally(() => {
|
||||
this.starting = undefined
|
||||
})
|
||||
return this.starting
|
||||
}
|
||||
|
||||
private async initialize(): Promise<void> {
|
||||
const env: NodeJS.ProcessEnv = { ...process.env, ELECTRON_VITE_RPC: 'true' }
|
||||
delete env.ELECTRON_RUN_AS_NODE
|
||||
this.stderr = ''
|
||||
const compilerPath = path.join(
|
||||
path.dirname(require.resolve('electron-vite/package.json')),
|
||||
'bin',
|
||||
'electron-bytecode-main.cjs'
|
||||
)
|
||||
const ps = spawn(getElectronPath(), [compilerPath], {
|
||||
stdio: ['ignore', 'ignore', 'pipe', 'ipc'],
|
||||
windowsHide: true,
|
||||
env
|
||||
})
|
||||
this.ps = ps
|
||||
if (ps.stderr) {
|
||||
ps.stderr.setEncoding('utf8')
|
||||
ps.stderr.on('data', (chunk: string) => {
|
||||
if (this.ps === ps) this.stderr = (this.stderr + chunk).slice(-64 * 1024)
|
||||
})
|
||||
}
|
||||
ps.on('message', (message: CompilerResponse) => {
|
||||
if (this.ps !== ps || !message || message.type !== 'response') return
|
||||
const pending = this.rpcMap.get(message.invocationId)
|
||||
if (!pending) return
|
||||
clearTimeout(pending.timer)
|
||||
this.rpcMap.delete(message.invocationId)
|
||||
if (message.error) {
|
||||
const error = new Error(message.error.message)
|
||||
error.name = message.error.name
|
||||
if (message.error.stack) error.stack = message.error.stack
|
||||
pending.reject(error)
|
||||
} else pending.resolve(message.result)
|
||||
})
|
||||
ps.on('error', error => {
|
||||
if (this.ps === ps) this.rejectPending(error)
|
||||
})
|
||||
ps.on('disconnect', () => {
|
||||
if (this.ps === ps) this.rejectPending(new Error('Electron IPC disconnected'))
|
||||
})
|
||||
ps.on('exit', (code, signal) => {
|
||||
if (this.ps === ps) this.rejectPending(new Error('Electron exited (code ' + code + ', signal ' + signal + ')'))
|
||||
})
|
||||
ps.on('close', () => {
|
||||
if (this.ps === ps) {
|
||||
this.rejectPending(new Error('Electron process closed'))
|
||||
this.ps = undefined
|
||||
}
|
||||
})
|
||||
try {
|
||||
await this.sendRequest('ready', [])
|
||||
if (!ps.connected || this.stopping) throw new Error('Electron stopped during initialization')
|
||||
this.ready = true
|
||||
} catch (error) {
|
||||
await this.stop()
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
compile(target: BytecodeTarget, code: string): Promise<BytecodeCompileResult> {
|
||||
return this.request<BytecodeCompileResult>('compile', target, code)
|
||||
}
|
||||
|
||||
async request<T = unknown>(cmd: string, ...args: unknown[]): Promise<T> {
|
||||
if (!this.ready || this.stopping) throw new Error('BytecodeCompiler is not ready; await start() first')
|
||||
return this.sendRequest(cmd, args) as Promise<T>
|
||||
}
|
||||
|
||||
private sendRequest(cmd: string, args: unknown[]): Promise<unknown> {
|
||||
const ps = this.ps
|
||||
if (!ps?.connected) return Promise.reject(new Error('Electron IPC is not connected'))
|
||||
const invocationId = this.invocationId++
|
||||
return new Promise((resolve, reject) => {
|
||||
const fail = (error: Error): void => {
|
||||
const pending = this.rpcMap.get(invocationId)
|
||||
if (!pending) return
|
||||
clearTimeout(pending.timer)
|
||||
this.rpcMap.delete(invocationId)
|
||||
reject(this.withStderr(error))
|
||||
}
|
||||
const timer = setTimeout(() => fail(new Error('Electron RPC timed out: ' + cmd)), RPC_TIMEOUT)
|
||||
this.rpcMap.set(invocationId, { resolve, reject, timer })
|
||||
try {
|
||||
ps.send({ type: 'request', invocationId, cmd, args }, error => {
|
||||
if (error) fail(error)
|
||||
})
|
||||
} catch (error) {
|
||||
fail(error instanceof Error ? error : new Error(String(error)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private rejectPending(error: Error): void {
|
||||
this.ready = false
|
||||
if (this.rpcMap.size) this.withStderr(error)
|
||||
for (const pending of this.rpcMap.values()) {
|
||||
clearTimeout(pending.timer)
|
||||
pending.reject(error)
|
||||
}
|
||||
this.rpcMap.clear()
|
||||
}
|
||||
|
||||
private withStderr(error: Error): Error {
|
||||
const stderr = this.stderr.trim()
|
||||
if (stderr) {
|
||||
const stack = error.stack
|
||||
error.message += '\n' + stderr
|
||||
if (stack) error.stack = stack + '\n' + stderr
|
||||
}
|
||||
return error
|
||||
}
|
||||
|
||||
stop(): Promise<void> {
|
||||
if (this.stopping) return this.stopping
|
||||
this.rejectPending(new Error('BytecodeCompiler stopped'))
|
||||
const ps = this.ps
|
||||
if (!ps) return Promise.resolve()
|
||||
this.stopping = new Promise<void>(resolve => {
|
||||
const timer = setTimeout(() => ps.kill('SIGKILL'), SHUTDOWN_TIMEOUT)
|
||||
ps.once('close', () => {
|
||||
clearTimeout(timer)
|
||||
resolve()
|
||||
})
|
||||
if (ps.connected) {
|
||||
try {
|
||||
ps.send({ type: 'shutdown' }, error => {
|
||||
if (error) ps.kill()
|
||||
})
|
||||
} catch {
|
||||
ps.kill()
|
||||
}
|
||||
} else ps.kill()
|
||||
}).finally(() => {
|
||||
this.stopping = undefined
|
||||
})
|
||||
return this.stopping
|
||||
}
|
||||
}
|
||||
363
src/config.ts
363
src/config.ts
@ -1,4 +1,7 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import { createRequire } from 'node:module'
|
||||
import colors from 'picocolors'
|
||||
import {
|
||||
type UserConfig as ViteConfig,
|
||||
@ -11,6 +14,7 @@ import {
|
||||
mergeConfig,
|
||||
normalizePath
|
||||
} from 'vite'
|
||||
import { build } from 'esbuild'
|
||||
|
||||
import {
|
||||
electronMainConfigPresetPlugin,
|
||||
@ -28,8 +32,7 @@ import modulePathPlugin from './plugins/modulePath'
|
||||
import isolateEntriesPlugin from './plugins/isolateEntries'
|
||||
import { type ExternalOptions, externalizeDepsPlugin } from './plugins/externalizeDeps'
|
||||
import { type BytecodeOptions, bytecodePlugin } from './plugins/bytecode'
|
||||
import { isObject, isFilePathESM, cloneConfig, asyncFlatten } from './utils'
|
||||
import { findConfigFile, bundleConfigFile, loadConfigFormBundledFile } from './load'
|
||||
import { isObject, isFilePathESM, deepClone, asyncFlatten } from './utils'
|
||||
|
||||
export { defineConfig as defineViteConfig } from 'vite'
|
||||
|
||||
@ -66,9 +69,9 @@ interface ExternalizeDepsMixin {
|
||||
interface BytecodeMixin {
|
||||
/**
|
||||
* Options pass on to `bytecode` plugin in electron-vite.
|
||||
* https://electron-vite.org/guide/source-code-protection#options
|
||||
* https://electron-vite.org/guide/source-code-protection#bytecodeplugin-options
|
||||
*
|
||||
* Compiles source code to v8 bytecode and works only in the `build` command.
|
||||
* Compile source code to v8 bytecode.
|
||||
*/
|
||||
bytecode?: boolean | BytecodeOptions
|
||||
}
|
||||
@ -118,7 +121,11 @@ export type ElectronViteConfigFnPromise = (env: ConfigEnv) => Promise<UserConfig
|
||||
export type ElectronViteConfigFn = (env: ConfigEnv) => UserConfig | Promise<UserConfig>
|
||||
|
||||
export type ElectronViteConfigExport =
|
||||
UserConfig | Promise<UserConfig> | ElectronViteConfigFnObject | ElectronViteConfigFnPromise | ElectronViteConfigFn
|
||||
| UserConfig
|
||||
| Promise<UserConfig>
|
||||
| ElectronViteConfigFnObject
|
||||
| ElectronViteConfigFnPromise
|
||||
| ElectronViteConfigFn
|
||||
|
||||
/**
|
||||
* Type helper to make it easier to use `electron.vite.config.*`
|
||||
@ -157,8 +164,7 @@ export async function resolveConfig(
|
||||
|
||||
process.env.NODE_ENV = defaultMode
|
||||
|
||||
const userConfig: UserConfig | undefined = {}
|
||||
|
||||
let userConfig: UserConfig | undefined
|
||||
let configFileDependencies: string[] = []
|
||||
|
||||
let { configFile } = config
|
||||
@ -185,20 +191,123 @@ export async function resolveConfig(
|
||||
|
||||
const outDir = config.build?.outDir
|
||||
|
||||
const { main, preload, renderer } = loadResult.config
|
||||
if (loadResult.config.main) {
|
||||
const mainViteConfig: MainViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
|
||||
|
||||
if (main) {
|
||||
userConfig.main = await new MainConfigFactory(main, config, { outDir, root }).build()
|
||||
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
|
||||
|
||||
if (outDir) {
|
||||
resetOutDir(mainViteConfig, outDir, 'main')
|
||||
}
|
||||
|
||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(mainViteConfig)
|
||||
|
||||
const builtInMainPlugins: PluginOption[] = [
|
||||
electronMainConfigPresetPlugin({ root }),
|
||||
electronMainConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
workerPlugin(),
|
||||
modulePathPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [
|
||||
electronMainConfigPresetPlugin({ root }),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
},
|
||||
mainViteConfig
|
||||
)
|
||||
),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
|
||||
mainViteConfig.plugins = builtInMainPlugins.concat(mainViteConfig.plugins || [])
|
||||
|
||||
loadResult.config.main = mainViteConfig
|
||||
}
|
||||
|
||||
if (preload) {
|
||||
userConfig.preload = await new PreloadConfigFactory(preload, config, { outDir, root }).build()
|
||||
if (loadResult.config.preload) {
|
||||
const preloadViteConfig: PreloadViteConfig = mergeConfig(loadResult.config.preload, deepClone(config))
|
||||
|
||||
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
|
||||
|
||||
if (outDir) {
|
||||
resetOutDir(preloadViteConfig, outDir, 'preload')
|
||||
}
|
||||
|
||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(preloadViteConfig)
|
||||
|
||||
const builtInPreloadPlugins: PluginOption[] = [
|
||||
electronPreloadConfigPresetPlugin({ root }),
|
||||
electronPreloadConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
|
||||
if (preloadViteConfig.build?.isolatedEntries) {
|
||||
builtInPreloadPlugins.push(
|
||||
isolateEntriesPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [
|
||||
electronPreloadConfigPresetPlugin({ root }),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
},
|
||||
preloadViteConfig
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
preloadViteConfig.plugins = builtInPreloadPlugins.concat(preloadViteConfig.plugins)
|
||||
|
||||
loadResult.config.preload = preloadViteConfig
|
||||
}
|
||||
|
||||
if (renderer) {
|
||||
userConfig.renderer = await new RendererConfigFactory(renderer, config, { outDir, root }).build()
|
||||
if (loadResult.config.renderer) {
|
||||
const rendererViteConfig: RendererViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config))
|
||||
|
||||
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
|
||||
|
||||
if (outDir) {
|
||||
resetOutDir(rendererViteConfig, outDir, 'renderer')
|
||||
}
|
||||
|
||||
const builtInRendererPlugins: PluginOption[] = [
|
||||
electronRendererConfigPresetPlugin({ root }),
|
||||
electronRendererConfigValidatorPlugin()
|
||||
]
|
||||
|
||||
if (rendererViteConfig.build?.isolatedEntries) {
|
||||
builtInRendererPlugins.push(
|
||||
isolateEntriesPlugin(
|
||||
mergeConfig(
|
||||
{
|
||||
plugins: [electronRendererConfigPresetPlugin({ root })]
|
||||
},
|
||||
rendererViteConfig
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
rendererViteConfig.plugins = builtInRendererPlugins.concat(rendererViteConfig.plugins || [])
|
||||
|
||||
loadResult.config.renderer = rendererViteConfig
|
||||
}
|
||||
|
||||
userConfig = loadResult.config
|
||||
configFile = loadResult.path
|
||||
configFileDependencies = loadResult.dependencies
|
||||
}
|
||||
@ -213,111 +322,6 @@ export async function resolveConfig(
|
||||
return resolved
|
||||
}
|
||||
|
||||
export abstract class ConfigFactory<T extends MainViteConfig | PreloadViteConfig | RendererViteConfig> {
|
||||
constructor(
|
||||
protected readonly baseConfig: T,
|
||||
protected readonly inlineConfig: InlineConfig,
|
||||
protected readonly options: { outDir?: string; root?: string }
|
||||
) {
|
||||
baseConfig.build ??= {}
|
||||
baseConfig.build.rolldownOptions ??= baseConfig.build.rollupOptions
|
||||
}
|
||||
|
||||
async build(cleanMode?: boolean): Promise<T> {
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
const config = mergeConfig(cloneConfig(this.baseConfig) as any, cloneConfig(this.inlineConfig)) as T
|
||||
|
||||
config.mode = this.inlineConfig.mode || config.mode || process.env.NODE_ENV
|
||||
|
||||
if (this.options.outDir) {
|
||||
resetOutDir(config, this.options.outDir, this.processType())
|
||||
}
|
||||
|
||||
const builtinPlugins = await this.resolveBuiltinPlugins(config, cleanMode)
|
||||
|
||||
config.plugins = builtinPlugins.concat(config.plugins || [])
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
protected abstract processType(): 'main' | 'preload' | 'renderer'
|
||||
|
||||
protected abstract resolveBuiltinPlugins(config: T, cleanMode?: boolean): Promise<PluginOption[]>
|
||||
}
|
||||
|
||||
export class MainConfigFactory extends ConfigFactory<MainViteConfig> {
|
||||
protected processType(): 'main' {
|
||||
return 'main'
|
||||
}
|
||||
|
||||
protected async resolveBuiltinPlugins(config: MainViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(config)
|
||||
|
||||
return cleanMode
|
||||
? [
|
||||
electronMainConfigPresetPlugin({ root: this.options.root }),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
: [
|
||||
electronMainConfigPresetPlugin({ root: this.options.root }),
|
||||
electronMainConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
workerPlugin(),
|
||||
modulePathPlugin(this),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export class PreloadConfigFactory extends ConfigFactory<PreloadViteConfig> {
|
||||
protected processType(): 'preload' {
|
||||
return 'preload'
|
||||
}
|
||||
|
||||
protected async resolveBuiltinPlugins(config: PreloadViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||
const configDrivenPlugins: PluginOption[] = await resolveConfigDrivenPlugins(config)
|
||||
|
||||
return cleanMode
|
||||
? [
|
||||
electronPreloadConfigPresetPlugin({ root: this.options.root }),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins
|
||||
]
|
||||
: [
|
||||
electronPreloadConfigPresetPlugin({ root: this.options.root }),
|
||||
electronPreloadConfigValidatorPlugin(),
|
||||
assetPlugin(),
|
||||
importMetaPlugin(),
|
||||
esmShimPlugin(),
|
||||
...configDrivenPlugins,
|
||||
...(config.build?.isolatedEntries ? [isolateEntriesPlugin(this)] : [])
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export class RendererConfigFactory extends ConfigFactory<RendererViteConfig> {
|
||||
protected processType(): 'renderer' {
|
||||
return 'renderer'
|
||||
}
|
||||
|
||||
protected async resolveBuiltinPlugins(config: RendererViteConfig, cleanMode?: boolean): Promise<PluginOption[]> {
|
||||
return cleanMode
|
||||
? [electronRendererConfigPresetPlugin({ root: this.options.root })]
|
||||
: [
|
||||
electronRendererConfigPresetPlugin({ root: this.options.root }),
|
||||
electronRendererConfigValidatorPlugin(),
|
||||
...(config.build?.isolatedEntries ? [isolateEntriesPlugin(this)] : [])
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
function resetOutDir(config: ViteConfig, outDir: string, subOutDir: string): void {
|
||||
let userOutDir = config.build?.outDir
|
||||
if (outDir === userOutDir) {
|
||||
@ -362,6 +366,8 @@ function isOptions<T extends object>(value: boolean | T): value is T {
|
||||
return typeof value === 'object' && value !== null
|
||||
}
|
||||
|
||||
const CONFIG_FILE_NAME = 'electron.vite.config'
|
||||
|
||||
export async function loadConfigFromFile(
|
||||
configEnv: ConfigEnv,
|
||||
configFile?: string,
|
||||
@ -377,7 +383,9 @@ export async function loadConfigFromFile(
|
||||
throw new Error(`config file cannot be named ${configFile}.`)
|
||||
}
|
||||
|
||||
const resolvedPath = configFile ? path.resolve(configFile) : findConfigFile(configRoot)
|
||||
const resolvedPath = configFile
|
||||
? path.resolve(configFile)
|
||||
: findConfigFile(configRoot, ['js', 'ts', 'mjs', 'cjs', 'mts', 'cts'])
|
||||
|
||||
if (!resolvedPath) {
|
||||
return {
|
||||
@ -391,7 +399,7 @@ export async function loadConfigFromFile(
|
||||
|
||||
try {
|
||||
const { code, dependencies } = await bundleConfigFile(resolvedPath, isESM)
|
||||
const configExport = await loadConfigFormBundledFile<ElectronViteConfigExport>(resolvedPath, code, isESM)
|
||||
const configExport = await loadConfigFormBundledFile(configRoot, resolvedPath, code, isESM)
|
||||
|
||||
const config = await (typeof configExport === 'function' ? configExport(configEnv) : configExport)
|
||||
if (!isObject(config)) {
|
||||
@ -417,3 +425,116 @@ export async function loadConfigFromFile(
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
function findConfigFile(configRoot: string, extensions: string[]): string {
|
||||
for (const ext of extensions) {
|
||||
const configFile = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${ext}`)
|
||||
if (fs.existsSync(configFile)) {
|
||||
return configFile
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
async function bundleConfigFile(fileName: string, isESM: boolean): Promise<{ code: string; dependencies: string[] }> {
|
||||
const dirnameVarName = '__electron_vite_injected_dirname'
|
||||
const filenameVarName = '__electron_vite_injected_filename'
|
||||
const importMetaUrlVarName = '__electron_vite_injected_import_meta_url'
|
||||
const result = await build({
|
||||
absWorkingDir: process.cwd(),
|
||||
entryPoints: [fileName],
|
||||
write: false,
|
||||
target: ['node20'],
|
||||
platform: 'node',
|
||||
bundle: true,
|
||||
format: isESM ? 'esm' : 'cjs',
|
||||
sourcemap: false,
|
||||
metafile: true,
|
||||
define: {
|
||||
__dirname: dirnameVarName,
|
||||
__filename: filenameVarName,
|
||||
'import.meta.url': importMetaUrlVarName
|
||||
},
|
||||
plugins: [
|
||||
{
|
||||
name: 'externalize-deps',
|
||||
setup(build): void {
|
||||
build.onResolve({ filter: /.*/ }, args => {
|
||||
const id = args.path
|
||||
if (id[0] !== '.' && !path.isAbsolute(id)) {
|
||||
return {
|
||||
external: true
|
||||
}
|
||||
}
|
||||
return null
|
||||
})
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'replace-import-meta',
|
||||
setup(build): void {
|
||||
build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async args => {
|
||||
const contents = await fs.promises.readFile(args.path, 'utf8')
|
||||
const injectValues =
|
||||
`const ${dirnameVarName} = ${JSON.stringify(path.dirname(args.path))};` +
|
||||
`const ${filenameVarName} = ${JSON.stringify(args.path)};` +
|
||||
`const ${importMetaUrlVarName} = ${JSON.stringify(pathToFileURL(args.path).href)};`
|
||||
|
||||
return {
|
||||
loader: args.path.endsWith('ts') ? 'ts' : 'js',
|
||||
contents: injectValues + contents
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
const { text } = result.outputFiles[0]
|
||||
return {
|
||||
code: text,
|
||||
dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
|
||||
}
|
||||
}
|
||||
|
||||
interface NodeModuleWithCompile extends NodeModule {
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
_compile(code: string, filename: string): any
|
||||
}
|
||||
|
||||
const _require = createRequire(import.meta.url)
|
||||
async function loadConfigFormBundledFile(
|
||||
configRoot: string,
|
||||
configFile: string,
|
||||
bundledCode: string,
|
||||
isESM: boolean
|
||||
): Promise<ElectronViteConfigExport> {
|
||||
if (isESM) {
|
||||
const fileNameTmp = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${Date.now()}.mjs`)
|
||||
fs.writeFileSync(fileNameTmp, bundledCode)
|
||||
|
||||
const fileUrl = pathToFileURL(fileNameTmp)
|
||||
try {
|
||||
return (await import(fileUrl.href)).default
|
||||
} finally {
|
||||
try {
|
||||
fs.unlinkSync(fileNameTmp)
|
||||
} catch {}
|
||||
}
|
||||
} else {
|
||||
const extension = path.extname(configFile)
|
||||
const realFileName = fs.realpathSync(configFile)
|
||||
const loaderExt = extension in _require.extensions ? extension : '.js'
|
||||
const defaultLoader = _require.extensions[loaderExt]!
|
||||
_require.extensions[loaderExt] = (module: NodeModule, filename: string): void => {
|
||||
if (filename === realFileName) {
|
||||
;(module as NodeModuleWithCompile)._compile(bundledCode, filename)
|
||||
} else {
|
||||
defaultLoader(module, filename)
|
||||
}
|
||||
}
|
||||
delete _require.cache[_require.resolve(configFile)]
|
||||
const raw = _require(configFile)
|
||||
_require.extensions[loaderExt] = defaultLoader
|
||||
return raw.__esModule ? raw.default : raw
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,11 +69,6 @@ export function getElectronNodeTarget(): string {
|
||||
const electronVer = getElectronMajorVer()
|
||||
|
||||
const nodeVer = {
|
||||
'44': '24.18',
|
||||
'43': '24.17',
|
||||
'42': '24.15',
|
||||
'41': '24.14',
|
||||
'40': '24.14',
|
||||
'39': '22.20',
|
||||
'38': '22.19',
|
||||
'37': '22.16',
|
||||
@ -105,11 +100,6 @@ export function getElectronChromeTarget(): string {
|
||||
const electronVer = getElectronMajorVer()
|
||||
|
||||
const chromeVer = {
|
||||
'44': '152',
|
||||
'43': '150',
|
||||
'42': '148',
|
||||
'41': '146',
|
||||
'40': '144',
|
||||
'39': '142',
|
||||
'38': '140',
|
||||
'37': '138',
|
||||
@ -165,36 +155,7 @@ export function startElectron(root: string | undefined): ChildProcess {
|
||||
const entry = process.env.ELECTRON_ENTRY || '.'
|
||||
|
||||
const ps = spawn(electronPath, [entry].concat(args), { stdio: 'inherit' })
|
||||
// `code` is null when the child was terminated by a signal, and `process.exit` ignores
|
||||
// its second argument, so passing the listener directly would report every crash as a
|
||||
// successful exit. Map a signal death to a non-zero status instead.
|
||||
ps.on('close', (code, signal) => process.exit(code ?? (signal ? 1 : 0)))
|
||||
|
||||
setupElectronSignalHandlers(ps)
|
||||
ps.on('close', process.exit)
|
||||
|
||||
return ps
|
||||
}
|
||||
|
||||
let electronPs: ChildProcess | undefined
|
||||
let signalHandlersRegistered = false
|
||||
|
||||
function setupElectronSignalHandlers(child: ChildProcess): void {
|
||||
electronPs = child
|
||||
|
||||
if (signalHandlersRegistered) return
|
||||
|
||||
const signals: NodeJS.Signals[] =
|
||||
process.platform === 'win32' ? ['SIGINT', 'SIGTERM'] : ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGUSR2']
|
||||
|
||||
for (const signal of signals) {
|
||||
process.on(signal, () => {
|
||||
const child = electronPs
|
||||
if (!child?.pid || child.exitCode !== null || child.signalCode !== null) return
|
||||
|
||||
// A previously sent signal does not mean the child has exited.
|
||||
child.kill(signal)
|
||||
})
|
||||
}
|
||||
|
||||
signalHandlersRegistered = true
|
||||
}
|
||||
|
||||
130
src/load.ts
130
src/load.ts
@ -1,130 +0,0 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import { createRequire } from 'node:module'
|
||||
import { rolldown } from 'rolldown'
|
||||
import type { RolldownOutput, OutputChunk } from 'rolldown'
|
||||
|
||||
const CONFIG_FILE_NAME = 'electron.vite.config'
|
||||
|
||||
export function findConfigFile(
|
||||
configRoot: string,
|
||||
extensions: string[] = ['js', 'ts', 'mjs', 'cjs', 'mts', 'cts']
|
||||
): string {
|
||||
for (const ext of extensions) {
|
||||
const configFile = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${ext}`)
|
||||
if (fs.existsSync(configFile)) {
|
||||
return configFile
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export async function bundleConfigFile(
|
||||
fileName: string,
|
||||
isESM: boolean
|
||||
): Promise<{ code: string; dependencies: string[] }> {
|
||||
const dirnameVarName = '__electron_vite_injected_dirname'
|
||||
const filenameVarName = '__electron_vite_injected_filename'
|
||||
const importMetaUrlVarName = '__electron_vite_injected_import_meta_url'
|
||||
|
||||
const bundle = await rolldown({
|
||||
input: path.resolve(fileName),
|
||||
platform: 'node',
|
||||
transform: {
|
||||
target: 'node20',
|
||||
define: {
|
||||
__dirname: dirnameVarName,
|
||||
__filename: filenameVarName,
|
||||
'import.meta.url': importMetaUrlVarName
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
{
|
||||
name: 'externalize-deps',
|
||||
resolveId(id) {
|
||||
if (id[0] !== '.' && !path.isAbsolute(id)) {
|
||||
return { id, external: true }
|
||||
}
|
||||
return null
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'inject-file-scope-variables',
|
||||
transform: {
|
||||
filter: { id: /\.[cm]?[jt]s$/ },
|
||||
handler(code, id) {
|
||||
const injectValues =
|
||||
`const ${dirnameVarName} = ${JSON.stringify(path.dirname(id))};` +
|
||||
`const ${filenameVarName} = ${JSON.stringify(id)};` +
|
||||
`const ${importMetaUrlVarName} = ${JSON.stringify(pathToFileURL(id).href)};`
|
||||
return { code: injectValues + code, map: null }
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
let result: RolldownOutput
|
||||
try {
|
||||
result = await bundle.generate({
|
||||
format: isESM ? 'esm' : 'cjs',
|
||||
sourcemap: 'inline',
|
||||
sourcemapPathTransform(relative, sourcemapPath) {
|
||||
return path.resolve(path.dirname(sourcemapPath), relative)
|
||||
},
|
||||
codeSplitting: false
|
||||
})
|
||||
} finally {
|
||||
await bundle.close()
|
||||
}
|
||||
|
||||
const entryChunk = result.output.find((chunk): chunk is OutputChunk => chunk.type === 'chunk' && chunk.isEntry)
|
||||
if (!entryChunk) {
|
||||
throw new Error(`Failed to bundle config file: ${fileName}`)
|
||||
}
|
||||
|
||||
return {
|
||||
code: entryChunk.code,
|
||||
dependencies: entryChunk.moduleIds.filter(id => !id.startsWith('\0'))
|
||||
}
|
||||
}
|
||||
|
||||
interface NodeModuleWithCompile extends NodeModule {
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
_compile(code: string, filename: string): any
|
||||
}
|
||||
|
||||
const _require = createRequire(import.meta.url)
|
||||
|
||||
export async function loadConfigFormBundledFile<T>(fileName: string, bundledCode: string, isESM: boolean): Promise<T> {
|
||||
if (isESM) {
|
||||
const fileNameTmp = `${fileName}.${Date.now()}-${Math.random().toString(16).slice(2)}.mjs`
|
||||
fs.writeFileSync(fileNameTmp, bundledCode)
|
||||
|
||||
const fileUrl = pathToFileURL(fileNameTmp)
|
||||
try {
|
||||
return (await import(fileUrl.href)).default
|
||||
} finally {
|
||||
try {
|
||||
fs.unlinkSync(fileNameTmp)
|
||||
} catch {}
|
||||
}
|
||||
} else {
|
||||
const extension = path.extname(fileName)
|
||||
const realFileName = fs.realpathSync(fileName)
|
||||
const loaderExt = extension in _require.extensions ? extension : '.js'
|
||||
const defaultLoader = _require.extensions[loaderExt]!
|
||||
_require.extensions[loaderExt] = (module: NodeModule, filename: string): void => {
|
||||
if (filename === realFileName) {
|
||||
;(module as NodeModuleWithCompile)._compile(bundledCode, filename)
|
||||
} else {
|
||||
defaultLoader(module, filename)
|
||||
}
|
||||
}
|
||||
delete _require.cache[_require.resolve(fileName)]
|
||||
const raw = _require(fileName)
|
||||
_require.extensions[loaderExt] = defaultLoader
|
||||
return raw.__esModule ? raw.default : raw
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs/promises'
|
||||
import { type Plugin, type Environment, normalizePath } from 'vite'
|
||||
import type { SourceMapInput } from 'rollup'
|
||||
import { type Plugin, normalizePath } from 'vite'
|
||||
import MagicString from 'magic-string'
|
||||
import { cleanUrl, getHash, toRelativePath } from '../utils'
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
@ -25,18 +26,21 @@ export default async function loadWasm(file, importObject = {}) {
|
||||
}
|
||||
`
|
||||
|
||||
const assetCache = new WeakMap<Environment, Map<string, string>>()
|
||||
const publicAssetPathCache = new WeakMap<Environment, Map<string, string>>()
|
||||
|
||||
export default function assetPlugin(): Plugin {
|
||||
let publicDir = ''
|
||||
const publicAssetPathCache = new Map<string, string>()
|
||||
const assetCache = new Map<string, string>()
|
||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||
return {
|
||||
name: 'vite:node-asset',
|
||||
apply: 'build',
|
||||
enforce: 'pre',
|
||||
buildStart(): void {
|
||||
publicAssetPathCache.set(this.environment, new Map())
|
||||
assetCache.set(this.environment, new Map())
|
||||
publicAssetPathCache.clear()
|
||||
assetCache.clear()
|
||||
},
|
||||
configResolved(config): void {
|
||||
publicDir = config.publicDir
|
||||
},
|
||||
resolveId(id): string | void {
|
||||
if (id === wasmHelperId) {
|
||||
@ -53,21 +57,15 @@ export default function assetPlugin(): Plugin {
|
||||
}
|
||||
|
||||
let referenceId: string
|
||||
|
||||
const file = cleanUrl(id)
|
||||
|
||||
const { publicDir } = this.environment.config
|
||||
|
||||
if (publicDir && file.startsWith(publicDir)) {
|
||||
const hash = getHash(file)
|
||||
const publicAssetPathMap = publicAssetPathCache.get(this.environment)!
|
||||
if (!publicAssetPathMap.get(hash)) {
|
||||
publicAssetPathMap.set(hash, file)
|
||||
if (!publicAssetPathCache.get(hash)) {
|
||||
publicAssetPathCache.set(hash, file)
|
||||
}
|
||||
referenceId = `__VITE_NODE_PUBLIC_ASSET__${hash}__`
|
||||
} else {
|
||||
const cache = assetCache.get(this.environment)!
|
||||
const cached = cache.get(file)
|
||||
const cached = assetCache.get(file)
|
||||
if (cached) {
|
||||
referenceId = cached
|
||||
} else {
|
||||
@ -78,7 +76,7 @@ export default function assetPlugin(): Plugin {
|
||||
source: source as unknown as Uint8Array
|
||||
})
|
||||
referenceId = `__VITE_NODE_ASSET__${hash}__`
|
||||
cache.set(file, referenceId)
|
||||
assetCache.set(file, referenceId)
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +103,7 @@ export default function assetPlugin(): Plugin {
|
||||
export default importObject => loadWasm(${referenceId}, importObject)`
|
||||
}
|
||||
},
|
||||
renderChunk(code, chunk, { sourcemap, dir }) {
|
||||
renderChunk(code, chunk, { sourcemap, dir }): { code: string; map: SourceMapInput } | null {
|
||||
let match: RegExpExecArray | null
|
||||
let s: MagicString | undefined
|
||||
|
||||
@ -121,13 +119,11 @@ export default function assetPlugin(): Plugin {
|
||||
})
|
||||
}
|
||||
|
||||
const publicAssetPathMap = publicAssetPathCache.get(this.environment)!
|
||||
|
||||
nodePublicAssetRE.lastIndex = 0
|
||||
while ((match = nodePublicAssetRE.exec(code))) {
|
||||
s ||= new MagicString(code)
|
||||
const [full, hash] = match
|
||||
const filename = publicAssetPathMap.get(hash)!
|
||||
const filename = publicAssetPathCache.get(hash)!
|
||||
const outputFilepath = toRelativePath(filename, normalizePath(path.join(dir!, chunk.fileName)))
|
||||
const replacement = JSON.stringify(outputFilepath)
|
||||
s.overwrite(match.index, match.index + full.length, replacement, {
|
||||
|
||||
30
src/plugins/buildReporter.ts
Normal file
30
src/plugins/buildReporter.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { type Plugin } from 'vite'
|
||||
|
||||
type BuildReporterApi = {
|
||||
getWatchFiles: () => string[]
|
||||
}
|
||||
|
||||
export default function buildReporterPlugin(): Plugin<BuildReporterApi> {
|
||||
const moduleIds: string[] = []
|
||||
return {
|
||||
name: 'vite:build-reporter',
|
||||
|
||||
buildEnd() {
|
||||
const allModuleIds = Array.from(this.getModuleIds())
|
||||
const sourceFiles = allModuleIds.filter(id => {
|
||||
if (id.includes('node_modules')) {
|
||||
return false
|
||||
}
|
||||
const info = this.getModuleInfo(id)
|
||||
return info && !info.isExternal
|
||||
})
|
||||
moduleIds.push(...sourceFiles)
|
||||
},
|
||||
|
||||
api: {
|
||||
getWatchFiles() {
|
||||
return moduleIds
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,80 +1,149 @@
|
||||
import path from 'node:path'
|
||||
import { randomBytes } from 'node:crypto'
|
||||
import { spawn } from 'node:child_process'
|
||||
import { createRequire } from 'node:module'
|
||||
import colors from 'picocolors'
|
||||
import { type Plugin, type Rolldown, normalizePath } from 'vite'
|
||||
import type * as Babel from '@babel/core'
|
||||
import { type Plugin, type Logger, type LibraryOptions, normalizePath } from 'vite'
|
||||
import * as babel from '@babel/core'
|
||||
import MagicString from 'magic-string'
|
||||
import { toRelativePath, resolveBuildOutputs } from '../utils'
|
||||
import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler'
|
||||
import type { SourceMapInput, OutputChunk, OutputOptions } from 'rollup'
|
||||
import { getElectronPath } from '../electron'
|
||||
import { toRelativePath } from '../utils'
|
||||
|
||||
// Inspired by https://github.com/bytenode/bytenode
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
let babel: typeof Babel | undefined
|
||||
const _require = createRequire(import.meta.url)
|
||||
|
||||
function getBytecodeCompiler(plugins: readonly Plugin[]): BytecodeCompiler | undefined {
|
||||
const plugin = plugins.find(plugin => plugin.name === 'vite:electron-bytecode-build')
|
||||
return (plugin?.api as { compiler?: BytecodeCompiler } | undefined)?.compiler
|
||||
function getBytecodeCompilerPath(): string {
|
||||
return path.join(path.dirname(_require.resolve('electron-vite/package.json')), 'bin', 'electron-bytecode.cjs')
|
||||
}
|
||||
|
||||
// Versioned envelope: magic/version (8), UTF-16 source length (4), module ID (16), V8 cache.
|
||||
// Keep our metadata separate from the private V8 cache header.
|
||||
async function compileToBytecode(compiler: BytecodeCompiler, target: BytecodeTarget, code: string): Promise<Buffer> {
|
||||
const id = randomBytes(16)
|
||||
// Reserve enough source space for an untruncated, per-artifact placeholder identity.
|
||||
const source = code + '\n/*' + id.toString('hex') + '*/'
|
||||
const result = await compiler.compile(target, source)
|
||||
const header = Buffer.alloc(28)
|
||||
header.write('EVBC0001', 0, 'ascii')
|
||||
header.writeUInt32LE(result.sourceLength, 8)
|
||||
id.copy(header, 12)
|
||||
return Buffer.concat([header, Buffer.from(result.cachedData, 'base64')])
|
||||
function compileToBytecode(code: string): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let data = Buffer.from([])
|
||||
|
||||
const electronPath = getElectronPath()
|
||||
const bytecodePath = getBytecodeCompilerPath()
|
||||
|
||||
const proc = spawn(electronPath, [bytecodePath], {
|
||||
env: { ELECTRON_RUN_AS_NODE: '1' },
|
||||
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
|
||||
})
|
||||
|
||||
if (proc.stdin) {
|
||||
proc.stdin.write(code)
|
||||
proc.stdin.end()
|
||||
}
|
||||
|
||||
if (proc.stdout) {
|
||||
proc.stdout.on('data', chunk => {
|
||||
data = Buffer.concat([data, chunk])
|
||||
})
|
||||
proc.stdout.on('error', err => {
|
||||
console.error(err)
|
||||
})
|
||||
proc.stdout.on('end', () => {
|
||||
resolve(data)
|
||||
})
|
||||
}
|
||||
|
||||
if (proc.stderr) {
|
||||
proc.stderr.on('data', chunk => {
|
||||
console.error('Error: ', chunk.toString())
|
||||
})
|
||||
proc.stderr.on('error', err => {
|
||||
console.error('Error: ', err)
|
||||
})
|
||||
}
|
||||
|
||||
proc.addListener('message', message => console.log(message))
|
||||
proc.addListener('error', err => console.error(err))
|
||||
|
||||
proc.on('error', err => reject(err))
|
||||
proc.on('exit', () => {
|
||||
resolve(data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const bytecodeModuleLoaderCode = [
|
||||
'"use strict";',
|
||||
'const fs = require("node:fs");',
|
||||
'const path = require("node:path");',
|
||||
'const vm = require("node:vm");',
|
||||
'const v8 = require("node:v8");',
|
||||
'const Module = require("node:module");',
|
||||
'v8.setFlagsFromString("--no-lazy");',
|
||||
'v8.setFlagsFromString("--no-flush-bytecode");',
|
||||
'const params = ["exports", "require", "module", "__filename", "__dirname"];',
|
||||
'Module._extensions[".jsc"] = Module._extensions[".cjsc"] = function (module, filename) {',
|
||||
' const data = fs.readFileSync(filename);',
|
||||
' if (data.length <= 28 || data.toString("ascii", 0, 8) !== "EVBC0001") {',
|
||||
' throw new Error("Invalid electron-vite bytecode format; rebuild the application: " + filename);',
|
||||
' }',
|
||||
' const length = data.readUInt32LE(8);',
|
||||
' const tag = "/*" + data.subarray(12, 28).toString("hex") + "*/";',
|
||||
' if (length < tag.length || length > 0x1fffffff) {',
|
||||
' throw new Error("Invalid bytecode source length: " + filename);',
|
||||
' }',
|
||||
' const placeholder = tag + " ".repeat(length - tag.length);',
|
||||
' const compiledWrapper = vm.compileFunction(placeholder, params, {',
|
||||
' filename,',
|
||||
' cachedData: data.subarray(28)',
|
||||
' });',
|
||||
' if (compiledWrapper.cachedDataRejected) {',
|
||||
' throw new Error("Invalid or incompatible cached data (cachedDataRejected): " + filename);',
|
||||
' }',
|
||||
' const require = function (id) { return module.require(id); };',
|
||||
' require.resolve = function (request, options) {',
|
||||
' return Module._resolveFilename(request, module, false, options);',
|
||||
' };',
|
||||
' if (process.mainModule) require.main = process.mainModule;',
|
||||
' require.extensions = Module._extensions;',
|
||||
' require.cache = Module._cache;',
|
||||
' return compiledWrapper.call(module.exports, module.exports, require, module, filename, path.dirname(filename));',
|
||||
'};'
|
||||
`"use strict";`,
|
||||
`const fs = require("fs");`,
|
||||
`const path = require("path");`,
|
||||
`const vm = require("vm");`,
|
||||
`const v8 = require("v8");`,
|
||||
`const Module = require("module");`,
|
||||
`v8.setFlagsFromString("--no-lazy");`,
|
||||
`v8.setFlagsFromString("--no-flush-bytecode");`,
|
||||
`const FLAG_HASH_OFFSET = 12;`,
|
||||
`const SOURCE_HASH_OFFSET = 8;`,
|
||||
`let dummyBytecode;`,
|
||||
`function setFlagHashHeader(bytecodeBuffer) {`,
|
||||
` if (!dummyBytecode) {`,
|
||||
` const script = new vm.Script("", {`,
|
||||
` produceCachedData: true`,
|
||||
` });`,
|
||||
` dummyBytecode = script.createCachedData();`,
|
||||
` }`,
|
||||
` dummyBytecode.slice(FLAG_HASH_OFFSET, FLAG_HASH_OFFSET + 4).copy(bytecodeBuffer, FLAG_HASH_OFFSET);`,
|
||||
`};`,
|
||||
`function getSourceHashHeader(bytecodeBuffer) {`,
|
||||
` return bytecodeBuffer.slice(SOURCE_HASH_OFFSET, SOURCE_HASH_OFFSET + 4);`,
|
||||
`};`,
|
||||
`function buffer2Number(buffer) {`,
|
||||
` let ret = 0;`,
|
||||
` ret |= buffer[3] << 24;`,
|
||||
` ret |= buffer[2] << 16;`,
|
||||
` ret |= buffer[1] << 8;`,
|
||||
` ret |= buffer[0];`,
|
||||
` return ret;`,
|
||||
`};`,
|
||||
`Module._extensions[".jsc"] = Module._extensions[".cjsc"] = function (module, filename) {`,
|
||||
` const bytecodeBuffer = fs.readFileSync(filename);`,
|
||||
` if (!Buffer.isBuffer(bytecodeBuffer)) {`,
|
||||
` throw new Error("BytecodeBuffer must be a buffer object.");`,
|
||||
` }`,
|
||||
` setFlagHashHeader(bytecodeBuffer);`,
|
||||
` const length = buffer2Number(getSourceHashHeader(bytecodeBuffer));`,
|
||||
` let dummyCode = "";`,
|
||||
` if (length > 1) {`,
|
||||
` dummyCode = "\\"" + "\\u200b".repeat(length - 2) + "\\"";`,
|
||||
` }`,
|
||||
` const script = new vm.Script(dummyCode, {`,
|
||||
` filename: filename,`,
|
||||
` lineOffset: 0,`,
|
||||
` displayErrors: true,`,
|
||||
` cachedData: bytecodeBuffer`,
|
||||
` });`,
|
||||
` if (script.cachedDataRejected) {`,
|
||||
` throw new Error("Invalid or incompatible cached data (cachedDataRejected)");`,
|
||||
` }`,
|
||||
` const require = function (id) {`,
|
||||
` return module.require(id);`,
|
||||
` };`,
|
||||
` require.resolve = function (request, options) {`,
|
||||
` return Module._resolveFilename(request, module, false, options);`,
|
||||
` };`,
|
||||
` if (process.mainModule) {`,
|
||||
` require.main = process.mainModule;`,
|
||||
` }`,
|
||||
` require.extensions = Module._extensions;`,
|
||||
` require.cache = Module._cache;`,
|
||||
` const compiledWrapper = script.runInThisContext({`,
|
||||
` filename: filename,`,
|
||||
` lineOffset: 0,`,
|
||||
` columnOffset: 0,`,
|
||||
` displayErrors: true`,
|
||||
` });`,
|
||||
` const dirname = path.dirname(filename);`,
|
||||
` const args = [module.exports, require, module, filename, dirname, process, global];`,
|
||||
` return compiledWrapper.apply(module.exports, args);`,
|
||||
`};`
|
||||
]
|
||||
|
||||
const bytecodeChunkExtensionRE = /.(jsc|cjsc)$/
|
||||
|
||||
export interface BytecodeOptions {
|
||||
chunkAlias?: string | string[] | RegExp
|
||||
chunkAlias?: string | string[]
|
||||
transformArrowFunctions?: boolean
|
||||
removeBundleJS?: boolean
|
||||
protectedStrings?: string[]
|
||||
@ -91,75 +160,67 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
||||
}
|
||||
|
||||
const { chunkAlias = [], transformArrowFunctions = true, removeBundleJS = true, protectedStrings = [] } = options
|
||||
const _chunkAlias = chunkAlias instanceof RegExp ? chunkAlias : Array.isArray(chunkAlias) ? chunkAlias : [chunkAlias]
|
||||
const _chunkAlias = Array.isArray(chunkAlias) ? chunkAlias : [chunkAlias]
|
||||
|
||||
const transformAllChunks = _chunkAlias.length === 0
|
||||
const isBytecodeChunk = (chunkName: string): boolean => {
|
||||
if (_chunkAlias instanceof RegExp) {
|
||||
_chunkAlias.lastIndex = 0
|
||||
return _chunkAlias.test(chunkName)
|
||||
}
|
||||
return _chunkAlias.length === 0 || _chunkAlias.some(alias => alias === chunkName)
|
||||
return transformAllChunks || _chunkAlias.some(alias => alias === chunkName)
|
||||
}
|
||||
|
||||
const plugins: Babel.PluginItem[] = []
|
||||
const plugins: babel.PluginItem[] = []
|
||||
|
||||
if (transformArrowFunctions) {
|
||||
plugins.push('@babel/plugin-transform-arrow-functions')
|
||||
}
|
||||
|
||||
if (protectedStrings.length > 0) {
|
||||
plugins.push([protectStringsPlugin as Babel.PluginTarget, { protectedStrings: new Set(protectedStrings) }])
|
||||
plugins.push([protectStringsPlugin, { protectedStrings: new Set(protectedStrings) }])
|
||||
}
|
||||
|
||||
const shouldTransformBytecodeChunk = plugins.length !== 0
|
||||
|
||||
const _transform = (
|
||||
code: string,
|
||||
sourceMaps: boolean = false
|
||||
): { code: string; map?: Rolldown.SourceMapInput } | null => {
|
||||
babel ??= require('@babel/core') as typeof Babel
|
||||
const re = babel.transformSync(code, { plugins, sourceMaps })
|
||||
return re ? { code: re.code || '', map: re.map as Rolldown.SourceMapInput } : null
|
||||
const _transform = (code: string, sourceMaps: boolean = false): { code: string; map?: SourceMapInput } | null => {
|
||||
const re = babel.transform(code, { plugins, sourceMaps })
|
||||
return re ? { code: re.code || '', map: re.map } : null
|
||||
}
|
||||
|
||||
const useStrict = '"use strict";'
|
||||
const bytecodeModuleLoader = 'bytecode-loader.cjs'
|
||||
|
||||
let logger: Logger
|
||||
let supported = false
|
||||
let isPreload = false
|
||||
let bytecodeCompiler: BytecodeCompiler | undefined
|
||||
|
||||
return {
|
||||
name: 'vite:bytecode',
|
||||
apply: 'build',
|
||||
enforce: 'post',
|
||||
configResolved(config): void {
|
||||
bytecodeCompiler = getBytecodeCompiler(config.plugins)
|
||||
if (supported) {
|
||||
return
|
||||
}
|
||||
isPreload = config.plugins.some(p => p.name === 'vite:electron-preload-config-preset')
|
||||
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-config-preset')
|
||||
logger = config.logger
|
||||
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
|
||||
if (useInRenderer) {
|
||||
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
|
||||
return
|
||||
}
|
||||
const build = config.build
|
||||
const outputs = resolveBuildOutputs(build.rolldownOptions.output, build.lib)
|
||||
const output = outputs[0]
|
||||
if (output) {
|
||||
const resolvedOutputs = resolveBuildOutputs(build.rollupOptions.output, build.lib)
|
||||
if (resolvedOutputs) {
|
||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||
const output = outputs[0]
|
||||
if (output.format === 'es') {
|
||||
config.logger.warn(
|
||||
colors.yellow(
|
||||
'bytecodePlugin does not support ES module, please remove "type": "module" ' +
|
||||
'in package.json or set build.rollupOptions.output.format (or build.rolldownOptions.output.format) to "cjs".'
|
||||
'in package.json or set the "build.rollupOptions.output.format" option to "cjs".'
|
||||
)
|
||||
)
|
||||
}
|
||||
supported = output.format === 'cjs' && !useInRenderer
|
||||
}
|
||||
},
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map?: Rolldown.SourceMapInput } | null {
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map?: SourceMapInput } | null {
|
||||
if (supported && isBytecodeChunk(chunk.name) && shouldTransformBytecodeChunk) {
|
||||
return _transform(code, !!sourcemap)
|
||||
}
|
||||
@ -170,9 +231,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
||||
return
|
||||
}
|
||||
const _chunks = Object.values(output)
|
||||
const chunks = _chunks.filter(
|
||||
chunk => chunk.type === 'chunk' && isBytecodeChunk(chunk.name)
|
||||
) as Rolldown.OutputChunk[]
|
||||
const chunks = _chunks.filter(chunk => chunk.type === 'chunk' && isBytecodeChunk(chunk.name)) as OutputChunk[]
|
||||
|
||||
if (chunks.length === 0) {
|
||||
return
|
||||
@ -192,79 +251,77 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
||||
|
||||
const bundles = Object.keys(output)
|
||||
|
||||
if (!bytecodeCompiler) {
|
||||
throw new Error('Bytecode compilation requires an electron-vite build')
|
||||
}
|
||||
for (const name of bundles) {
|
||||
const chunk = output[name]
|
||||
if (chunk.type === 'chunk') {
|
||||
let _code = chunk.code
|
||||
if (bytecodeRE) {
|
||||
let match: RegExpExecArray | null
|
||||
let s: MagicString | undefined
|
||||
while ((match = bytecodeRE.exec(_code))) {
|
||||
s ||= new MagicString(_code)
|
||||
const [prefix, chunkName] = match
|
||||
const len = prefix.length + chunkName.length
|
||||
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
|
||||
contentOnly: true
|
||||
})
|
||||
await Promise.all(
|
||||
bundles.map(async name => {
|
||||
const chunk = output[name]
|
||||
if (chunk.type === 'chunk') {
|
||||
let _code = chunk.code
|
||||
if (bytecodeRE) {
|
||||
let match: RegExpExecArray | null
|
||||
let s: MagicString | undefined
|
||||
while ((match = bytecodeRE.exec(_code))) {
|
||||
s ||= new MagicString(_code)
|
||||
const [prefix, chunkName] = match
|
||||
const len = prefix.length + chunkName.length
|
||||
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
|
||||
contentOnly: true
|
||||
})
|
||||
}
|
||||
if (s) {
|
||||
_code = s.toString()
|
||||
}
|
||||
}
|
||||
if (s) {
|
||||
_code = s.toString()
|
||||
}
|
||||
}
|
||||
if (bytecodeChunks.includes(name)) {
|
||||
const bytecodeBuffer = await compileToBytecode(bytecodeCompiler, isPreload ? 'preload' : 'main', _code)
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: name + 'c',
|
||||
source: bytecodeBuffer
|
||||
})
|
||||
if (!removeBundleJS) {
|
||||
if (bytecodeChunks.includes(name)) {
|
||||
const bytecodeBuffer = await compileToBytecode(_code)
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: '_' + chunk.fileName,
|
||||
source: chunk.code
|
||||
fileName: name + 'c',
|
||||
source: bytecodeBuffer
|
||||
})
|
||||
}
|
||||
if (chunk.isEntry) {
|
||||
const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
|
||||
const bytecodeModuleBlock = `require("./${path.basename(name) + 'c'}");`
|
||||
const code = `${useStrict}\n${bytecodeLoaderBlock}\n${bytecodeModuleBlock}\n`
|
||||
chunk.code = code
|
||||
if (!removeBundleJS) {
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: '_' + chunk.fileName,
|
||||
source: chunk.code
|
||||
})
|
||||
}
|
||||
if (chunk.isEntry) {
|
||||
const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
|
||||
const bytecodeModuleBlock = `require("./${path.basename(name) + 'c'}");`
|
||||
const code = `${useStrict}\n${bytecodeLoaderBlock}\n${bytecodeModuleBlock}\n`
|
||||
chunk.code = code
|
||||
} else {
|
||||
delete output[chunk.fileName]
|
||||
}
|
||||
bytecodeChunkCount += 1
|
||||
} else {
|
||||
delete output[chunk.fileName]
|
||||
}
|
||||
bytecodeChunkCount += 1
|
||||
} else {
|
||||
if (chunk.isEntry) {
|
||||
let hasBytecodeMoudle = false
|
||||
const idsToHandle = new Set([...chunk.imports, ...chunk.dynamicImports])
|
||||
for (const moduleId of idsToHandle) {
|
||||
if (bytecodeChunks.includes(moduleId)) {
|
||||
hasBytecodeMoudle = true
|
||||
break
|
||||
}
|
||||
const moduleInfo = this.getModuleInfo(moduleId)
|
||||
if (moduleInfo) {
|
||||
const { importers, dynamicImporters } = moduleInfo
|
||||
for (const importerId of importers) idsToHandle.add(importerId)
|
||||
for (const importerId of dynamicImporters) idsToHandle.add(importerId)
|
||||
if (chunk.isEntry) {
|
||||
let hasBytecodeMoudle = false
|
||||
const idsToHandle = new Set([...chunk.imports, ...chunk.dynamicImports])
|
||||
for (const moduleId of idsToHandle) {
|
||||
if (bytecodeChunks.includes(moduleId)) {
|
||||
hasBytecodeMoudle = true
|
||||
break
|
||||
}
|
||||
const moduleInfo = this.getModuleInfo(moduleId)
|
||||
if (moduleInfo && !moduleInfo.isExternal) {
|
||||
const { importers, dynamicImporters } = moduleInfo
|
||||
for (const importerId of importers) idsToHandle.add(importerId)
|
||||
for (const importerId of dynamicImporters) idsToHandle.add(importerId)
|
||||
}
|
||||
}
|
||||
_code = hasBytecodeMoudle
|
||||
? _code.replace(
|
||||
/("use strict";)|('use strict';)/,
|
||||
`${useStrict}\n${getBytecodeLoaderBlock(chunk.fileName)}`
|
||||
)
|
||||
: _code
|
||||
}
|
||||
if (hasBytecodeMoudle) {
|
||||
const loader = getBytecodeLoaderBlock(chunk.fileName)
|
||||
const strictRE = /^(#![^\n]*\n)?(["'])use strict\2;/
|
||||
_code = strictRE.test(_code)
|
||||
? _code.replace(strictRE, `$&\n${loader}`)
|
||||
: _code.replace(/^(#![^\n]*\n)?/, `$&${loader}\n`)
|
||||
}
|
||||
chunk.code = _code
|
||||
}
|
||||
chunk.code = _code
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
if (bytecodeChunkCount && !_chunks.some(ass => ass.type === 'asset' && ass.fileName === bytecodeModuleLoader)) {
|
||||
this.emitFile({
|
||||
@ -278,20 +335,31 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
||||
writeBundle(_, output): void {
|
||||
if (supported) {
|
||||
const bytecodeChunkCount = Object.keys(output).filter(chunk => bytecodeChunkExtensionRE.test(chunk)).length
|
||||
this.environment.logger.info(`${colors.green(`✓`)} ${bytecodeChunkCount} chunks compiled into bytecode.`)
|
||||
logger.info(`${colors.green(`✓`)} ${bytecodeChunkCount} chunks compiled into bytecode.`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ProtectStringsPluginState extends Babel.PluginPass {
|
||||
function resolveBuildOutputs(
|
||||
outputs: OutputOptions | OutputOptions[] | undefined,
|
||||
libOptions: LibraryOptions | false
|
||||
): OutputOptions | OutputOptions[] | undefined {
|
||||
if (libOptions && !Array.isArray(outputs)) {
|
||||
const libFormats = libOptions.formats || []
|
||||
return libFormats.map(format => ({ ...outputs, format }))
|
||||
}
|
||||
return outputs
|
||||
}
|
||||
|
||||
interface ProtectStringsPluginState extends babel.PluginPass {
|
||||
opts: { protectedStrings: Set<string> }
|
||||
}
|
||||
|
||||
function protectStringsPlugin(api: typeof Babel & Babel.ConfigAPI): Babel.PluginObj<ProtectStringsPluginState> {
|
||||
function protectStringsPlugin(api: typeof babel & babel.ConfigAPI): babel.PluginObj<ProtectStringsPluginState> {
|
||||
const { types: t } = api
|
||||
|
||||
function createFromCharCodeFunction(value: string): Babel.types.CallExpression {
|
||||
function createFromCharCodeFunction(value: string): babel.types.CallExpression {
|
||||
const charCodes = Array.from(value).map(s => s.charCodeAt(0))
|
||||
const charCodeLiterals = charCodes.map(code => t.numericLiteral(code))
|
||||
|
||||
|
||||
@ -2,9 +2,10 @@ import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { builtinModules } from 'node:module'
|
||||
import colors from 'picocolors'
|
||||
import { type Plugin, type UserConfig, mergeConfig, normalizePath } from 'vite'
|
||||
import { type Plugin, type LibraryOptions, mergeConfig, normalizePath } from 'vite'
|
||||
import type { OutputOptions } from 'rollup'
|
||||
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
||||
import { loadPackageData, resolveBuildOutputs } from '../utils'
|
||||
import { loadPackageData } from '../utils'
|
||||
|
||||
export interface ElectronPluginOptions {
|
||||
root?: string
|
||||
@ -38,6 +39,17 @@ function processEnvDefine(): Record<string, string> {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveBuildOutputs(
|
||||
outputs: OutputOptions | OutputOptions[] | undefined,
|
||||
libOptions: LibraryOptions | false
|
||||
): OutputOptions | OutputOptions[] | undefined {
|
||||
if (libOptions && !Array.isArray(outputs)) {
|
||||
const libFormats = libOptions.formats || []
|
||||
return libFormats.map(format => ({ ...outputs, format }))
|
||||
}
|
||||
return outputs
|
||||
}
|
||||
|
||||
export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions): Plugin {
|
||||
return {
|
||||
name: 'vite:electron-main-config-preset',
|
||||
@ -53,24 +65,29 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
||||
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
|
||||
|
||||
const defaultConfig = {
|
||||
resolve: {
|
||||
browserField: false,
|
||||
mainFields: ['module', 'jsnext:main', 'jsnext'],
|
||||
conditions: ['node']
|
||||
},
|
||||
build: {
|
||||
outDir: path.resolve(root, 'out', 'main'),
|
||||
target: nodeTarget,
|
||||
assetsDir: 'chunks',
|
||||
rolldownOptions: {
|
||||
rollupOptions: {
|
||||
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
||||
output: {}
|
||||
},
|
||||
reportCompressedSize: false,
|
||||
minify: false
|
||||
}
|
||||
} satisfies UserConfig
|
||||
}
|
||||
|
||||
const build = config.build || {}
|
||||
const rolldownOptions = build.rolldownOptions || {}
|
||||
if (!rolldownOptions.input) {
|
||||
const rollupOptions = build.rollupOptions || {}
|
||||
if (!rollupOptions.input) {
|
||||
const libOptions = build.lib
|
||||
const outputOptions = rolldownOptions.output
|
||||
const outputOptions = rollupOptions.output
|
||||
defaultConfig.build['lib'] = {
|
||||
entry: findLibEntry(root, 'main'),
|
||||
formats:
|
||||
@ -79,10 +96,10 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
||||
: [outputOptions && !Array.isArray(outputOptions) && outputOptions.format ? outputOptions.format : format]
|
||||
}
|
||||
} else {
|
||||
defaultConfig.build.rolldownOptions.output['format'] = format
|
||||
defaultConfig.build.rollupOptions.output['format'] = format
|
||||
}
|
||||
|
||||
defaultConfig.build.rolldownOptions.output['assetFileNames'] = path.posix.join(
|
||||
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
|
||||
build.assetsDir || defaultConfig.build.assetsDir,
|
||||
'[name]-[hash].[ext]'
|
||||
)
|
||||
@ -90,6 +107,8 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||
config.build = buildConfig
|
||||
|
||||
config.resolve = mergeConfig(defaultConfig.resolve, config.resolve || {})
|
||||
|
||||
config.define = config.define || {}
|
||||
config.define = { ...processEnvDefine(), ...config.define }
|
||||
|
||||
@ -104,8 +123,6 @@ export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions):
|
||||
config.build.ssr = true
|
||||
config.build.ssrEmitAssets = true
|
||||
config.ssr = { ...config.ssr, ...{ noExternal: true } }
|
||||
|
||||
config.build.rollupOptions = config.build.rolldownOptions
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -127,25 +144,25 @@ export function electronMainConfigValidatorPlugin(): Plugin {
|
||||
}
|
||||
|
||||
const libOptions = build.lib
|
||||
const rolldownOptions = build.rolldownOptions
|
||||
const rollupOptions = build.rollupOptions
|
||||
|
||||
if (!(libOptions && libOptions.entry) && !rolldownOptions?.input) {
|
||||
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) {
|
||||
throw new Error(
|
||||
'An entry point is required in the electron vite main config, ' +
|
||||
'which can be specified using build.lib.entry, build.rollupOptions.input, ' +
|
||||
'or build.rolldownOptions.input.'
|
||||
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
|
||||
)
|
||||
}
|
||||
|
||||
const outputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
|
||||
|
||||
if (outputs.length > 0) {
|
||||
if (resolvedOutputs) {
|
||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||
if (outputs.length > 1) {
|
||||
throw new Error('The electron vite main config does not support multiple outputs.')
|
||||
} else {
|
||||
const output = outputs[0]
|
||||
if (['es', 'cjs'].includes(output.format || '')) {
|
||||
if (output.format === 'es' && !supportESM()) {
|
||||
const outpout = outputs[0]
|
||||
if (['es', 'cjs'].includes(outpout.format || '')) {
|
||||
if (outpout.format === 'es' && !supportESM()) {
|
||||
throw new Error(
|
||||
'The electron vite main config output format does not support "es", ' +
|
||||
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
||||
@ -187,20 +204,20 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
||||
outDir: path.resolve(root, 'out', 'preload'),
|
||||
target: nodeTarget,
|
||||
assetsDir: 'chunks',
|
||||
rolldownOptions: {
|
||||
rollupOptions: {
|
||||
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])],
|
||||
output: {}
|
||||
},
|
||||
reportCompressedSize: false,
|
||||
minify: false
|
||||
}
|
||||
} satisfies UserConfig
|
||||
}
|
||||
|
||||
const build = config.build || {}
|
||||
const rolldownOptions = build.rolldownOptions || {}
|
||||
if (!rolldownOptions.input) {
|
||||
const rollupOptions = build.rollupOptions || {}
|
||||
if (!rollupOptions.input) {
|
||||
const libOptions = build.lib
|
||||
const outputOptions = rolldownOptions.output
|
||||
const outputOptions = rollupOptions.output
|
||||
defaultConfig.build['lib'] = {
|
||||
entry: findLibEntry(root, 'preload'),
|
||||
formats:
|
||||
@ -209,10 +226,10 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
||||
: [outputOptions && !Array.isArray(outputOptions) && outputOptions.format ? outputOptions.format : format]
|
||||
}
|
||||
} else {
|
||||
defaultConfig.build.rolldownOptions.output['format'] = format
|
||||
defaultConfig.build.rollupOptions.output['format'] = format
|
||||
}
|
||||
|
||||
defaultConfig.build.rolldownOptions.output['assetFileNames'] = path.posix.join(
|
||||
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
|
||||
build.assetsDir || defaultConfig.build.assetsDir,
|
||||
'[name]-[hash].[ext]'
|
||||
)
|
||||
@ -220,21 +237,23 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||
config.build = buildConfig
|
||||
|
||||
const outputs = resolveBuildOutputs(config.build.rolldownOptions!.output, config.build.lib)
|
||||
const resolvedOutputs = resolveBuildOutputs(config.build.rollupOptions!.output, config.build.lib || false)
|
||||
|
||||
if (outputs.some(({ format }) => format === 'es')) {
|
||||
if (Array.isArray(config.build.rolldownOptions!.output)) {
|
||||
config.build.rolldownOptions!.output.forEach(output => {
|
||||
if (output.format === 'es') {
|
||||
output['entryFileNames'] = '[name].mjs'
|
||||
output['chunkFileNames'] = '[name]-[hash].mjs'
|
||||
}
|
||||
})
|
||||
} else {
|
||||
config.build.rolldownOptions!.output!['entryFileNames'] = '[name].mjs'
|
||||
config.build.rolldownOptions!.output!['chunkFileNames'] = config.build.lib
|
||||
? '[name]-[hash].mjs'
|
||||
: path.posix.join(build.assetsDir || defaultConfig.build.assetsDir, '[name]-[hash].mjs')
|
||||
if (resolvedOutputs) {
|
||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||
|
||||
if (outputs.find(({ format }) => format === 'es')) {
|
||||
if (Array.isArray(config.build.rollupOptions!.output)) {
|
||||
config.build.rollupOptions!.output.forEach(output => {
|
||||
if (output.format === 'es') {
|
||||
output['entryFileNames'] = '[name].mjs'
|
||||
output['chunkFileNames'] = '[name]-[hash].mjs'
|
||||
}
|
||||
})
|
||||
} else {
|
||||
config.build.rollupOptions!.output!['entryFileNames'] = '[name].mjs'
|
||||
config.build.rollupOptions!.output!['chunkFileNames'] = '[name]-[hash].mjs'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -253,8 +272,6 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
||||
config.build.ssrEmitAssets = true
|
||||
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {})
|
||||
config.ssr.noExternal = true
|
||||
|
||||
config.build.rollupOptions = config.build.rolldownOptions
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -276,25 +293,25 @@ export function electronPreloadConfigValidatorPlugin(): Plugin {
|
||||
}
|
||||
|
||||
const libOptions = build.lib
|
||||
const rolldownOptions = build.rolldownOptions
|
||||
const rollupOptions = build.rollupOptions
|
||||
|
||||
if (!(libOptions && libOptions.entry) && !rolldownOptions?.input) {
|
||||
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) {
|
||||
throw new Error(
|
||||
'An entry point is required in the electron vite preload config, ' +
|
||||
'which can be specified using build.lib.entry, build.rollupOptions.input, ' +
|
||||
' or build.rolldownOptions.input.'
|
||||
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
|
||||
)
|
||||
}
|
||||
|
||||
const outputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
|
||||
|
||||
if (outputs.length > 0) {
|
||||
if (resolvedOutputs) {
|
||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
||||
if (outputs.length > 1) {
|
||||
throw new Error('The electron vite preload config does not support multiple outputs.')
|
||||
} else {
|
||||
const output = outputs[0]
|
||||
if (['es', 'cjs'].includes(output.format || '')) {
|
||||
if (output.format === 'es' && !supportESM()) {
|
||||
const outpout = outputs[0]
|
||||
if (['es', 'cjs'].includes(outpout.format || '')) {
|
||||
if (outpout.format === 'es' && !supportESM()) {
|
||||
throw new Error(
|
||||
'The electron vite preload config output format does not support "es", ' +
|
||||
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
||||
@ -341,14 +358,14 @@ export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptio
|
||||
outDir: path.resolve(root, 'out', 'renderer'),
|
||||
target: chromeTarget,
|
||||
modulePreload: { polyfill: false },
|
||||
rolldownOptions: {
|
||||
rollupOptions: {
|
||||
input: findInput(root)
|
||||
},
|
||||
reportCompressedSize: false,
|
||||
minify: false,
|
||||
emptyOutDir: emptyOutDir()
|
||||
}
|
||||
} satisfies UserConfig
|
||||
}
|
||||
|
||||
if (config.build?.outDir) {
|
||||
config.build.outDir = path.resolve(root, config.build.outDir)
|
||||
@ -360,8 +377,6 @@ export function electronRendererConfigPresetPlugin(options?: ElectronPluginOptio
|
||||
config.envDir = config.envDir || path.resolve(root)
|
||||
|
||||
config.envPrefix = config.envPrefix || ['RENDERER_VITE_', 'VITE_']
|
||||
|
||||
config.build.rollupOptions = config.build.rolldownOptions
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -387,12 +402,10 @@ export function electronRendererConfigValidatorPlugin(): Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
const rolldownOptions = build.rolldownOptions
|
||||
if (!rolldownOptions.input) {
|
||||
const rollupOptions = build.rollupOptions
|
||||
if (!rollupOptions.input) {
|
||||
config.logger.warn(colors.yellow(`index.html file is not found in ${colors.dim('/src/renderer')} directory.`))
|
||||
throw new Error(
|
||||
'build.rollupOptions.input or build.rolldownOptions.input option is required in the electron vite renderer config.'
|
||||
)
|
||||
throw new Error('build.rollupOptions.input option is required in the electron vite renderer config.')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,11 @@
|
||||
/*
|
||||
* The core of this plugin was conceived by pi0 and is taken from the following repository:
|
||||
* https://github.com/unjs/unbuild/blob/main/src/builder/plugins/cjs.ts
|
||||
* license: https://github.com/unjs/unbuild/blob/main/LICENSE
|
||||
*/
|
||||
|
||||
import MagicString from 'magic-string'
|
||||
import type { SourceMapInput } from 'rollup'
|
||||
import type { Plugin } from 'vite'
|
||||
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
@ -7,22 +14,37 @@ const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/
|
||||
|
||||
const CJSShim_normal = `
|
||||
// -- CommonJS Shims --
|
||||
import __shims_url__ from 'node:url';
|
||||
import __shims_path__ from 'node:path';
|
||||
import __shims_module__ from 'node:module';
|
||||
const __filename = __shims_url__.fileURLToPath(import.meta.url);
|
||||
const __dirname = __shims_path__.dirname(__filename);
|
||||
const require = __shims_module__.createRequire(import.meta.url);
|
||||
import __cjs_url__ from 'node:url';
|
||||
import __cjs_path__ from 'node:path';
|
||||
import __cjs_mod__ from 'node:module';
|
||||
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
||||
const __dirname = __cjs_path__.dirname(__filename);
|
||||
const require = __cjs_mod__.createRequire(import.meta.url);
|
||||
`
|
||||
|
||||
const CJSShim_node_20_11 = `
|
||||
// -- CommonJS Shims --
|
||||
import __shims_module__ from 'node:module';
|
||||
import __cjs_mod__ from 'node:module';
|
||||
const __filename = import.meta.filename;
|
||||
const __dirname = import.meta.dirname;
|
||||
const require = __shims_module__.createRequire(import.meta.url);
|
||||
const require = __cjs_mod__.createRequire(import.meta.url);
|
||||
`
|
||||
|
||||
const ESMStaticImportRe =
|
||||
/(?<=\s|^|;)import\s*([\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu
|
||||
|
||||
interface StaticImport {
|
||||
end: number
|
||||
}
|
||||
|
||||
function findStaticImports(code: string): StaticImport[] {
|
||||
const matches: StaticImport[] = []
|
||||
for (const match of code.matchAll(ESMStaticImportRe)) {
|
||||
matches.push({ end: (match.index || 0) + match[0].length })
|
||||
}
|
||||
return matches
|
||||
}
|
||||
|
||||
export default function esmShimPlugin(): Plugin {
|
||||
const CJSShim = supportImportMetaPaths() ? CJSShim_node_20_11 : CJSShim_normal
|
||||
|
||||
@ -30,16 +52,13 @@ export default function esmShimPlugin(): Plugin {
|
||||
name: 'vite:esm-shim',
|
||||
apply: 'build',
|
||||
enforce: 'post',
|
||||
renderChunk(code, _chunk, { format, sourcemap }) {
|
||||
renderChunk(code, _chunk, { format, sourcemap }): { code: string; map?: SourceMapInput } | null {
|
||||
if (format === 'es') {
|
||||
// Rolldown may remove unused shim bindings after this hook; referenced bindings remain available.
|
||||
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const lastESMImport = this.parse(code)
|
||||
.body.filter(node => node.type === 'ImportDeclaration')
|
||||
.pop()
|
||||
const lastESMImport = findStaticImports(code).pop()
|
||||
const indexToAppend = lastESMImport ? lastESMImport.end : 0
|
||||
const s = new MagicString(code)
|
||||
s.appendRight(indexToAppend, CJSShim)
|
||||
|
||||
@ -33,15 +33,13 @@ export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | n
|
||||
config(config): void {
|
||||
const defaultConfig = {
|
||||
build: {
|
||||
rolldownOptions: {
|
||||
rollupOptions: {
|
||||
external: deps.length > 0 ? [...deps, new RegExp(`^(${deps.join('|')})/.+`)] : []
|
||||
}
|
||||
}
|
||||
}
|
||||
const buildConfig = mergeConfig(defaultConfig.build, config.build || {})
|
||||
config.build = buildConfig
|
||||
|
||||
config.build.rollupOptions = config.build.rolldownOptions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { type Plugin, version } from 'vite'
|
||||
import type { Plugin } from 'vite'
|
||||
|
||||
/**
|
||||
* @deprecated Vite 8 (Rolldown) now provides native support for these
|
||||
* `import.meta` properties. These properties are polyfilled when the output
|
||||
* format is CJS.
|
||||
*
|
||||
* @see https://rolldown.rs/in-depth/non-esm-output-formats#well-known-import-meta-properties
|
||||
*/
|
||||
export default function importMetaPlugin(): Plugin | null {
|
||||
const currentVersion = version.split('.')
|
||||
const major = parseInt(currentVersion[0], 10)
|
||||
if (major >= 8) {
|
||||
return null
|
||||
}
|
||||
export default function importMetaPlugin(): Plugin {
|
||||
return {
|
||||
name: 'vite:import-meta',
|
||||
apply: 'build',
|
||||
@ -30,5 +17,5 @@ export default function importMetaPlugin(): Plugin | null {
|
||||
}
|
||||
return null
|
||||
}
|
||||
} as any
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-function-type */
|
||||
import path from 'node:path'
|
||||
import { type InlineConfig, type Plugin, type LogLevel, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
|
||||
import { type InlineConfig, type Plugin, type Logger, type LogLevel, build as viteBuild, mergeConfig } from 'vite'
|
||||
import type { InputOptions, RollupOutput } from 'rollup'
|
||||
import colors from 'picocolors'
|
||||
import { cleanUrl } from '../utils'
|
||||
import type { ConfigFactory, PreloadViteConfig, RendererViteConfig } from '../config'
|
||||
import buildReporterPlugin from './buildReporter'
|
||||
|
||||
const VIRTUAL_ENTRY_ID = '\0virtual:isolate-entries'
|
||||
|
||||
@ -14,7 +14,9 @@ const LogLevels: Record<LogLevel, number> = {
|
||||
info: 3
|
||||
}
|
||||
|
||||
export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteConfig | RendererViteConfig>): Plugin {
|
||||
export default function isolateEntriesPlugin(userConfig: InlineConfig): Plugin {
|
||||
let logger: Logger
|
||||
|
||||
let entries: string[] | { [x: string]: string }[]
|
||||
|
||||
let transformedCount = 0
|
||||
@ -25,7 +27,11 @@ export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteC
|
||||
name: 'vite:isolate-entries',
|
||||
apply: 'build',
|
||||
|
||||
options(opts): Rolldown.InputOptions | void {
|
||||
configResolved(config): void {
|
||||
logger = config.logger
|
||||
},
|
||||
|
||||
options(opts): InputOptions | void {
|
||||
const { input } = opts
|
||||
if (input && typeof input === 'object') {
|
||||
if ((Array.isArray(input) && input.length > 0) || Object.keys(input).length > 1) {
|
||||
@ -50,13 +56,13 @@ export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteC
|
||||
|
||||
async load(id): Promise<string | void> {
|
||||
if (id === VIRTUAL_ENTRY_ID) {
|
||||
const userConfig = await factory.build(true)
|
||||
const shouldLog = LogLevels[userConfig.logLevel || 'info'] >= LogLevels.info
|
||||
const shouldWatch = this.meta.watchMode
|
||||
|
||||
const watchFiles = new Set<string>()
|
||||
|
||||
for (const entry of entries) {
|
||||
const re = await bundleEntryFile(entry, userConfig, shouldLog, transformedCount)
|
||||
const re = await bundleEntryFile(entry, userConfig, shouldWatch, shouldLog, transformedCount)
|
||||
|
||||
const outputChunks = re.bundles.output
|
||||
for (const chunk of outputChunks) {
|
||||
@ -71,20 +77,8 @@ export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteC
|
||||
assetCache.add(chunk.fileName)
|
||||
}
|
||||
|
||||
if (this.meta.watchMode) {
|
||||
const moduleIds = new Set<string>()
|
||||
for (const chunk of re.bundles.output) {
|
||||
if (chunk.type === 'chunk') {
|
||||
for (const id of chunk.moduleIds) {
|
||||
if (!id.startsWith('\0')) {
|
||||
moduleIds.add(cleanUrl(id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const id of moduleIds) {
|
||||
this.addWatchFile(id)
|
||||
}
|
||||
for (const id of re.watchFiles) {
|
||||
watchFiles.add(id)
|
||||
}
|
||||
|
||||
transformedCount += re.transformedCount
|
||||
@ -102,7 +96,7 @@ export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteC
|
||||
|
||||
renderStart(): void {
|
||||
clearLine(-1)
|
||||
this.environment.logger.info(`${colors.green(`✓`)} ${transformedCount} modules transformed.`)
|
||||
logger.info(`${colors.green(`✓`)} ${transformedCount} modules transformed.`)
|
||||
},
|
||||
|
||||
generateBundle(_, bundle): void {
|
||||
@ -118,28 +112,31 @@ export default function isolateEntriesPlugin(factory: ConfigFactory<PreloadViteC
|
||||
async function bundleEntryFile(
|
||||
input: string | Record<string, string>,
|
||||
config: InlineConfig,
|
||||
watch: boolean,
|
||||
shouldLog: boolean,
|
||||
preTransformedCount: number
|
||||
): Promise<{ bundles: Rolldown.RolldownOutput; transformedCount: number }> {
|
||||
): Promise<{ bundles: RollupOutput; watchFiles: string[]; transformedCount: number }> {
|
||||
const transformReporter = transformReporterPlugin(preTransformedCount, shouldLog)
|
||||
const buildReporter = watch ? buildReporterPlugin() : undefined
|
||||
|
||||
const viteConfig = mergeConfig(config, {
|
||||
build: {
|
||||
write: false,
|
||||
watch: false
|
||||
},
|
||||
plugins: [transformReporter],
|
||||
plugins: [transformReporter, buildReporter],
|
||||
logLevel: 'warn',
|
||||
configFile: false
|
||||
}) as InlineConfig
|
||||
|
||||
// rewrite the input instead of merging
|
||||
viteConfig.build!.rolldownOptions!.input = input
|
||||
viteConfig.build!.rollupOptions!.input = input
|
||||
|
||||
const bundles = (await viteBuild(viteConfig)) as Rolldown.RolldownOutput
|
||||
const bundles = await viteBuild(viteConfig)
|
||||
|
||||
return {
|
||||
bundles: bundles,
|
||||
bundles: bundles as RollupOutput,
|
||||
watchFiles: buildReporter?.api?.getWatchFiles() || [],
|
||||
transformedCount: transformReporter?.api?.getTransformedCount() || 0
|
||||
}
|
||||
}
|
||||
@ -149,16 +146,20 @@ function transformReporterPlugin(
|
||||
shouldLog = true
|
||||
): Plugin<{ getTransformedCount: () => number }> {
|
||||
let transformedCount = 0
|
||||
const log = throttle((id, root) => {
|
||||
let root
|
||||
const log = throttle(id => {
|
||||
writeLine(`transforming (${preTransformedCount + transformedCount}) ${colors.dim(path.relative(root, id))}`)
|
||||
})
|
||||
return {
|
||||
name: 'vite:transform-reporter',
|
||||
configResolved(config) {
|
||||
root = config.root
|
||||
},
|
||||
transform(_, id) {
|
||||
transformedCount++
|
||||
if (!shouldLog) return
|
||||
if (id.includes('?')) return
|
||||
log(id, this.environment.config.root)
|
||||
log(id)
|
||||
},
|
||||
api: {
|
||||
getTransformedCount() {
|
||||
@ -169,7 +170,6 @@ function transformReporterPlugin(
|
||||
}
|
||||
|
||||
function writeLine(output: string): void {
|
||||
if (!process.stdout.isTTY || process.env.CI) return
|
||||
clearLine()
|
||||
if (output.length < process.stdout.columns) {
|
||||
process.stdout.write(output)
|
||||
@ -179,7 +179,6 @@ function writeLine(output: string): void {
|
||||
}
|
||||
|
||||
function clearLine(move: number = 0): void {
|
||||
if (!process.stdout.isTTY || process.env.CI) return
|
||||
if (move < 0) {
|
||||
process.stdout.moveCursor(0, move)
|
||||
}
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
import { type Plugin, type InlineConfig, type Rolldown, build as viteBuild, mergeConfig } from 'vite'
|
||||
import path from 'node:path'
|
||||
import { type Plugin, type InlineConfig, build as viteBuild, mergeConfig } from 'vite'
|
||||
import type { SourceMapInput, RollupOutput, OutputOptions } from 'rollup'
|
||||
import MagicString from 'magic-string'
|
||||
import buildReporterPlugin from './buildReporter'
|
||||
import { cleanUrl, toRelativePath } from '../utils'
|
||||
import { supportImportMetaPaths } from '../electron'
|
||||
import type { ConfigFactory, MainViteConfig } from '../config'
|
||||
|
||||
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
|
||||
|
||||
/**
|
||||
* Resolve `?modulePath` import and return the module bundle path.
|
||||
*/
|
||||
export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>): Plugin {
|
||||
export default function modulePathPlugin(config: InlineConfig): Plugin {
|
||||
const isImportMetaPathSupported = supportImportMetaPaths()
|
||||
const assetCache = new Set<string>()
|
||||
return {
|
||||
@ -22,9 +24,8 @@ export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>)
|
||||
async load(id): Promise<string | void> {
|
||||
if (id.endsWith('?modulePath')) {
|
||||
// id resolved by Vite resolve plugin
|
||||
const config = await factory.build(true)
|
||||
const bundles = await bundleEntryFile(cleanUrl(id), config)
|
||||
const [outputChunk, ...outputChunks] = bundles.output
|
||||
const re = await bundleEntryFile(cleanUrl(id), config, this.meta.watchMode)
|
||||
const [outputChunk, ...outputChunks] = re.bundles.output
|
||||
const hash = this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: outputChunk.fileName,
|
||||
@ -41,20 +42,8 @@ export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>)
|
||||
})
|
||||
assetCache.add(chunk.fileName)
|
||||
}
|
||||
if (this.meta.watchMode) {
|
||||
const moduleIds = new Set<string>()
|
||||
for (const chunk of bundles.output) {
|
||||
if (chunk.type === 'chunk') {
|
||||
for (const id of chunk.moduleIds) {
|
||||
if (!id.startsWith('\0')) {
|
||||
moduleIds.add(cleanUrl(id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const id of moduleIds) {
|
||||
this.addWatchFile(id)
|
||||
}
|
||||
for (const id of re.watchFiles) {
|
||||
this.addWatchFile(id)
|
||||
}
|
||||
const refId = `__VITE_MODULE_PATH__${hash}__`
|
||||
const dirnameExpr = isImportMetaPathSupported ? 'import.meta.dirname' : '__dirname'
|
||||
@ -63,7 +52,7 @@ export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>)
|
||||
export default join(${dirnameExpr}, ${refId})`
|
||||
}
|
||||
},
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: Rolldown.SourceMapInput } | null {
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
||||
let match: RegExpExecArray | null
|
||||
let s: MagicString | undefined
|
||||
|
||||
@ -91,7 +80,12 @@ export default function modulePathPlugin(factory: ConfigFactory<MainViteConfig>)
|
||||
}
|
||||
}
|
||||
|
||||
async function bundleEntryFile(input: string, config: InlineConfig): Promise<Rolldown.RolldownOutput> {
|
||||
async function bundleEntryFile(
|
||||
input: string,
|
||||
config: InlineConfig,
|
||||
watch: boolean
|
||||
): Promise<{ bundles: RollupOutput; watchFiles: string[] }> {
|
||||
const reporter = watch ? buildReporterPlugin() : undefined
|
||||
const viteConfig = mergeConfig(config, {
|
||||
build: {
|
||||
write: false,
|
||||
@ -100,27 +94,30 @@ async function bundleEntryFile(input: string, config: InlineConfig): Promise<Rol
|
||||
plugins: [
|
||||
{
|
||||
name: 'vite:entry-file-name',
|
||||
outputOptions(output): Rolldown.OutputOptions {
|
||||
// Prevent output conflicts
|
||||
output.hashCharacters = 'hex'
|
||||
// Assign chunkFileNames to entryFileNames to treat sub-entries as internal chunks
|
||||
output.entryFileNames = output.chunkFileNames
|
||||
outputOptions(output): OutputOptions {
|
||||
if (typeof output.entryFileNames !== 'function' && output.entryFileNames) {
|
||||
output.entryFileNames = '[name]-[hash]' + path.extname(output.entryFileNames)
|
||||
}
|
||||
return output
|
||||
}
|
||||
}
|
||||
] as Plugin[],
|
||||
},
|
||||
reporter
|
||||
],
|
||||
logLevel: 'warn',
|
||||
configFile: false
|
||||
}) as InlineConfig
|
||||
|
||||
// rewrite the input instead of merging
|
||||
const buildOptions = viteConfig.build!
|
||||
buildOptions.rolldownOptions = {
|
||||
...buildOptions.rolldownOptions,
|
||||
buildOptions.rollupOptions = {
|
||||
...buildOptions.rollupOptions,
|
||||
input
|
||||
}
|
||||
|
||||
const bundles = await viteBuild(viteConfig)
|
||||
|
||||
return bundles as Rolldown.RolldownOutput
|
||||
return {
|
||||
bundles: bundles as RollupOutput,
|
||||
watchFiles: reporter?.api?.getWatchFiles() || []
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,16 +79,12 @@ export type SwcOptions = {
|
||||
export function swcPlugin(options: SwcOptions = {}): Plugin {
|
||||
const filter = createFilter(options.include || /\.(m?ts|[jt]sx)$/, options.exclude || /\.js$/)
|
||||
let sourcemap: boolean | 'inline' = false
|
||||
let minify: boolean | 'esbuild' | 'oxc' | 'terser' = false
|
||||
let minify: boolean | 'esbuild' | 'terser' = false
|
||||
return {
|
||||
name: 'vite:swc',
|
||||
config(): UserConfig {
|
||||
// Vite 8 defaults to Oxc for transform; `esbuild: false` is ignored and
|
||||
// warns. SWC replaces both paths when this plugin is active.
|
||||
// See https://github.com/alex8088/electron-vite/issues/916
|
||||
return {
|
||||
esbuild: false,
|
||||
oxc: false
|
||||
esbuild: false
|
||||
}
|
||||
},
|
||||
async configResolved(resolvedConfig): Promise<void> {
|
||||
@ -105,7 +101,7 @@ export function swcPlugin(options: SwcOptions = {}): Plugin {
|
||||
}
|
||||
},
|
||||
async renderChunk(code, chunk): Promise<null | { code: string; map: SourceMap }> {
|
||||
if (!minify || minify === 'terser' || minify === 'oxc') {
|
||||
if (!minify || minify === 'terser') {
|
||||
return null
|
||||
}
|
||||
const result = await transformWithSWC(code, chunk.fileName, {
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import type { Plugin } from 'vite'
|
||||
import type { SourceMapInput } from 'rollup'
|
||||
import MagicString from 'magic-string'
|
||||
import { cleanUrl, toRelativePath } from '../utils'
|
||||
|
||||
@ -35,7 +36,7 @@ export default function workerPlugin(): Plugin {
|
||||
}
|
||||
}
|
||||
},
|
||||
renderChunk(code, chunk, { sourcemap }) {
|
||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
||||
let match: RegExpExecArray | null
|
||||
let s: MagicString | undefined
|
||||
|
||||
|
||||
72
src/utils.ts
72
src/utils.ts
@ -3,7 +3,7 @@ import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
import { createHash } from 'node:crypto'
|
||||
import { createRequire } from 'node:module'
|
||||
import { type LibraryOptions, type Rolldown, loadEnv as viteLoadEnv } from 'vite'
|
||||
import { loadEnv as viteLoadEnv } from 'vite'
|
||||
|
||||
export function isObject(value: unknown): value is Record<string, unknown> {
|
||||
return Object.prototype.toString.call(value) === '[object Object]'
|
||||
@ -32,23 +32,6 @@ export function toRelativePath(filename: string, importer: string): string {
|
||||
return relPath.startsWith('.') ? relPath : `./${relPath}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve configured build outputs into an array.
|
||||
*/
|
||||
export function resolveBuildOutputs(
|
||||
outputs: Rolldown.OutputOptions | Rolldown.OutputOptions[] | undefined,
|
||||
libOptions: LibraryOptions | false | undefined
|
||||
): Rolldown.OutputOptions[] {
|
||||
if (Array.isArray(outputs)) {
|
||||
return outputs
|
||||
}
|
||||
if (libOptions) {
|
||||
const libFormats = libOptions.formats || []
|
||||
return libFormats.map(format => ({ ...outputs, format }))
|
||||
}
|
||||
return outputs ? [outputs] : []
|
||||
}
|
||||
|
||||
/**
|
||||
* Load `.env` files within the `envDir` (default: `process.cwd()`) .
|
||||
* By default, only env variables prefixed with `VITE_`, `MAIN_VITE_`, `PRELOAD_VITE_` and
|
||||
@ -129,59 +112,6 @@ export function deepClone<T>(value: T): DeepWritable<T> {
|
||||
return value as DeepWritable<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolate configuration data without cloning plugins or runtime instances.
|
||||
* Plugins may capture their own object in hooks, so their identity must survive.
|
||||
*/
|
||||
export function cloneConfig<T>(config: T): T {
|
||||
const seen = new WeakMap<object, unknown>()
|
||||
const pluginArrays = new WeakMap<object, unknown>()
|
||||
|
||||
function clone(value: unknown, plugins = false): unknown {
|
||||
if (value === null || typeof value !== 'object') return value
|
||||
|
||||
const cache = plugins ? pluginArrays : seen
|
||||
if (cache.has(value)) return cache.get(value)
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
const result: unknown[] = new Array(value.length)
|
||||
cache.set(value, result)
|
||||
value.forEach((item, index) => {
|
||||
result[index] = clone(item, plugins)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
// Preserve plugin objects, promises and other opaque plugin options.
|
||||
if (plugins) return value
|
||||
|
||||
if (value instanceof RegExp) {
|
||||
const result = new RegExp(value)
|
||||
result.lastIndex = value.lastIndex
|
||||
cache.set(value, result)
|
||||
return result
|
||||
}
|
||||
|
||||
const prototype = Object.getPrototypeOf(value)
|
||||
if (prototype !== Object.prototype && prototype !== null) return value
|
||||
|
||||
const result = Object.create(prototype) as Record<PropertyKey, unknown>
|
||||
cache.set(value, result)
|
||||
for (const key of Reflect.ownKeys(value)) {
|
||||
if (!Object.prototype.propertyIsEnumerable.call(value, key)) continue
|
||||
Object.defineProperty(result, key, {
|
||||
value: clone((value as Record<PropertyKey, unknown>)[key], key === 'plugins'),
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
return clone(config) as T
|
||||
}
|
||||
|
||||
type AsyncFlatten<T extends unknown[]> = T extends (infer U)[] ? Exclude<Awaited<U>, U[]>[] : never
|
||||
|
||||
export async function asyncFlatten<T extends unknown[]>(arr: T): Promise<AsyncFlatten<T>> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user