Compare commits

..

No commits in common. "master" and "v2.0.0" have entirely different histories.

20 changed files with 1246 additions and 1271 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

39
.eslintrc.cjs Normal file
View File

@ -0,0 +1,39 @@
module.exports = {
root: true,
env: {
commonjs: true,
es6: true,
node: true
},
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:prettier/recommended'
],
rules: {
'prettier/prettier': 'warn',
'no-empty': ['warn', { allowEmptyCatch: true }],
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off'
},
overrides: [
{
files: ['*.js'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off'
}
}
]
}

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
node_modules node_modules
dist dist
.DS_Store .DS_Store
.eslintcache
*.log* *.log*

View File

@ -1,63 +1,4 @@
### v3.1.0 (_2025-03-25_) ### v2.0.0 (_2024-01-09_)
- fix(bytecodePlugin): optimize 'use strict' directive replacement ([#681](https://github.com/alex8088/electron-vite/issues/681))
- perf: build compatilibity target for Electron 35 ([#729](https://github.com/alex8088/electron-vite/pull/729))
- chore(deps): update all non-major dependencies
- chore(deps): update globals to v16
- chore(deps): update esbuild to v0.25
### v3.1.0-beta.0 (_2025-03-12_)
See [v3.1.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v3.1.0-beta.0/CHANGELOG.md)
### v3.0.0 (_2025-02-16_)
- feat: resolve conditions for preload
- perf: build compatilibity target for Electron 32
- perf: build compatilibity target for Electron 33 ([#651](https://github.com/alex8088/electron-vite/pull/651))
- perf: build compatilibity target for Electron 34
- chore: move to eslint flat config
- chore(deps): update all non-major dependencies
- chore(deps): update @rollup/plugin-node-resolve to v16
- chore(deps): update @rollup/plugin-typescript to v12
- chore(deps): update esbuild to v0.24
- chore(deps): update vite to v6
- chore(deps): update @type/node to v22
### v3.0.0-beta.0 (_2025-01-22_)
See [v3.0.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v3.0.0-beta.0/CHANGELOG.md)
### v2.3.0 (_2024-06-23_)
- feat: resolve import.meta.\[dirname|filename\] to support CommonJS format
- fix: don't handle module ID that begin with \0 ([#530](https://github.com/alex8088/electron-vite/pull/530))
- fix: not using the mode from the config file ([#539](https://github.com/alex8088/electron-vite/pull/539))
- fix: default mode should not overrite user config mode
- perf: build compatilibity target for Electron 31
- perf: improve cjs shim
- chore(deps): update all non-major dependencies
- chore(deps): update @typescript-eslint/\* to v7
- chore(deps): update esbuild to v0.21
### v2.2.0 (_2024-04-21_)
- feat: export mergeConfig from vite ([#471](https://github.com/alex8088/electron-vite/issues/471))
- fix(types): narrow down the return type of defineConfig
- perf: build compatilibity target for Electron 30
- refactor(config): defineConfig types
- chore: fix camelcase typo
- chore: use rollup-plugin-rm to clean dist
### v2.1.0 (_2024-03-03_)
- feat: easy way to fork processes and use workers
- fix: config via build.lib fails when default entry point not found ([#393](https://github.com/alex8088/electron-vite/issues/393))
- perf: build compatilibity target for Electron 29
- perf: allow integrating more complex render solutions ([#412](https://github.com/alex8088/electron-vite/pull/412))
- perf(bytecodePlugin): warn that strings cannot be protected when minification is enabled ([#417](https://github.com/alex8088/electron-vite/issues/417))
### v2.0.0 (_2024-01-09_)
- feat: bump minimum node version to 18 - feat: bump minimum node version to 18
- feat: migrate to ESM - feat: migrate to ESM

View File

@ -1,42 +0,0 @@
// ts-check
import eslint from '@eslint/js'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
import globals from 'globals'
import tseslint from 'typescript-eslint'
export default tseslint.config(
{ ignores: ['**/node_modules', '**/dist', '**/bin'] },
eslint.configs.recommended,
tseslint.configs.recommended,
eslintPluginPrettierRecommended,
{
languageOptions: {
parser: tseslint.parser,
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022
},
globals: {
...globals.es2021,
...globals.node
}
},
rules: {
'prettier/prettier': 'warn',
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off'
}
},
{
files: ['*.js', '*.mjs'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off'
}
}
)

6
node.d.ts vendored
View File

@ -4,12 +4,6 @@ declare module '*?nodeWorker' {
export default function (options: WorkerOptions): Worker export default function (options: WorkerOptions): Worker
} }
// module path
declare module '*?modulePath' {
const src: string
export default src
}
// node asset // node asset
declare module '*?asset' { declare module '*?asset' {
const src: string const src: string

View File

@ -1,6 +1,6 @@
{ {
"name": "electron-vite", "name": "electron-vite",
"version": "3.1.0", "version": "2.0.0",
"description": "Electron build tooling based on Vite", "description": "Electron build tooling based on Vite",
"type": "module", "type": "module",
"main": "dist/index.cjs", "main": "dist/index.cjs",
@ -47,7 +47,7 @@
], ],
"scripts": { "scripts": {
"format": "prettier --write .", "format": "prettier --write .",
"lint": "eslint --cache .", "lint": "eslint --ext .ts src/**",
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"build": "pnpm run lint && rollup -c rollup.config.ts --configPlugin typescript" "build": "pnpm run lint && rollup -c rollup.config.ts --configPlugin typescript"
}, },
@ -66,7 +66,7 @@
}, },
"peerDependencies": { "peerDependencies": {
"@swc/core": "^1.0.0", "@swc/core": "^1.0.0",
"vite": "^4.0.0 || ^5.0.0 || ^6.0.0" "vite": "^4.0.0 || ^5.0.0"
}, },
"peerDependenciesMeta": { "peerDependenciesMeta": {
"@swc/core": { "@swc/core": {
@ -74,33 +74,31 @@
} }
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.22.0", "@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-node-resolve": "^16.0.1", "@rollup/plugin-typescript": "^11.1.5",
"@rollup/plugin-typescript": "^12.1.2", "@swc/core": "^1.3.100",
"@swc/core": "^1.11.9", "@types/node": "^18.19.3",
"@types/node": "^22.13.10", "@typescript-eslint/eslint-plugin": "^6.13.2",
"eslint": "^9.22.0", "@typescript-eslint/parser": "^6.13.2",
"eslint-config-prettier": "^10.1.1", "eslint": "^8.55.0",
"eslint-plugin-prettier": "^5.2.3", "eslint-config-prettier": "^9.1.0",
"globals": "^16.0.0", "eslint-plugin-prettier": "^5.0.1",
"lint-staged": "^15.4.3", "lint-staged": "^15.2.0",
"prettier": "^3.5.3", "prettier": "^3.1.0",
"rollup": "^4.35.0", "rollup": "^4.6.1",
"rollup-plugin-dts": "^6.1.1", "rollup-plugin-dts": "^6.1.0",
"rollup-plugin-rm": "^1.0.2", "simple-git-hooks": "^2.9.0",
"simple-git-hooks": "^2.11.1", "tslib": "^2.6.2",
"tslib": "^2.8.1", "typescript": "^5.3.3",
"typescript": "^5.7.3", "vite": "^5.0.6"
"typescript-eslint": "^8.26.1",
"vite": "^6.2.1"
}, },
"dependencies": { "dependencies": {
"@babel/core": "^7.26.10", "@babel/core": "^7.23.5",
"@babel/plugin-transform-arrow-functions": "^7.25.9", "@babel/plugin-transform-arrow-functions": "^7.23.3",
"cac": "^6.7.14", "cac": "^6.7.14",
"esbuild": "^0.25.1", "esbuild": "^0.19.8",
"magic-string": "^0.30.17", "magic-string": "^0.30.5",
"picocolors": "^1.1.1" "picocolors": "^1.0.0"
} }
} }

2073
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,33 @@
import { createRequire } from 'node:module' import { createRequire } from 'node:module'
import { defineConfig } from 'rollup' import fs from 'node:fs/promises'
import { type Plugin, defineConfig } from 'rollup'
import ts from '@rollup/plugin-typescript' import ts from '@rollup/plugin-typescript'
import resolve from '@rollup/plugin-node-resolve' import resolve from '@rollup/plugin-node-resolve'
import json from '@rollup/plugin-json' import json from '@rollup/plugin-json'
import dts from 'rollup-plugin-dts' import dts from 'rollup-plugin-dts'
import rm from 'rollup-plugin-rm'
const require = createRequire(import.meta.url) const require = createRequire(import.meta.url)
const pkg = require('./package.json') const pkg = require('./package.json')
const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})] const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
function clean(when: 'buildStart' | 'buildEnd', target: string): Plugin {
const _clean = async (target): Promise<void> => {
await fs.rm(target, { recursive: true, force: true }).catch(() => {})
}
return {
name: 'clean',
buildStart: async (): Promise<void> => {
if (when !== 'buildStart') return
await _clean(target)
},
buildEnd: async (): Promise<void> => {
if (when !== 'buildEnd') return
await _clean(target)
}
}
}
export default defineConfig([ export default defineConfig([
{ {
input: ['src/index.ts', 'src/cli.ts'], input: ['src/index.ts', 'src/cli.ts'],
@ -30,7 +47,7 @@ export default defineConfig([
], ],
external, external,
plugins: [ plugins: [
rm('dist', 'buildStart'), clean('buildStart', 'dist'),
json(), json(),
ts({ compilerOptions: { rootDir: 'src', declaration: true, declarationDir: 'dist/types' } }), ts({ compilerOptions: { rootDir: 'src', declaration: true, declarationDir: 'dist/types' } }),
resolve() resolve()
@ -42,6 +59,6 @@ export default defineConfig([
{ {
input: 'dist/types/index.d.ts', input: 'dist/types/index.d.ts',
output: [{ file: pkg.types, format: 'es' }], output: [{ file: pkg.types, format: 'es' }],
plugins: [dts(), rm('dist/types', 'buildEnd')] plugins: [dts(), clean('buildEnd', 'dist/types')]
} }
]) ])

View File

@ -5,7 +5,7 @@ import { createRequire } from 'node:module'
import colors from 'picocolors' import colors from 'picocolors'
import { import {
type UserConfig as ViteConfig, type UserConfig as ViteConfig,
type UserConfigExport as ViteConfigExport, type UserConfigExport as UserViteConfigExport,
type ConfigEnv, type ConfigEnv,
type Plugin, type Plugin,
type LogLevel, type LogLevel,
@ -18,9 +18,8 @@ import { build } from 'esbuild'
import { electronMainVitePlugin, electronPreloadVitePlugin, electronRendererVitePlugin } from './plugins/electron' import { electronMainVitePlugin, electronPreloadVitePlugin, electronRendererVitePlugin } from './plugins/electron'
import assetPlugin from './plugins/asset' import assetPlugin from './plugins/asset'
import workerPlugin from './plugins/worker' import workerPlugin from './plugins/worker'
import importMetaPlugin from './plugins/importMeta' import importMetaUrlPlugin from './plugins/importMetaUrl'
import esmShimPlugin from './plugins/esm' import esmShimPlugin from './plugins/esm'
import modulePathPlugin from './plugins/modulePath'
import { isObject, isFilePathESM } from './utils' import { isObject, isFilePathESM } from './utils'
export { defineConfig as defineViteConfig } from 'vite' export { defineConfig as defineViteConfig } from 'vite'
@ -46,25 +45,25 @@ export interface UserConfig {
preload?: ViteConfig & { configFile?: string | false } preload?: ViteConfig & { configFile?: string | false }
} }
export interface ElectronViteConfig { export interface UserConfigSchema {
/** /**
* Vite config options for electron main process * Vite config options for electron main process
* *
* https://vitejs.dev/config/ * https://vitejs.dev/config/
*/ */
main?: ViteConfigExport main?: UserViteConfigExport
/** /**
* Vite config options for electron renderer process * Vite config options for electron renderer process
* *
* https://vitejs.dev/config/ * https://vitejs.dev/config/
*/ */
renderer?: ViteConfigExport renderer?: UserViteConfigExport
/** /**
* Vite config options for electron preload files * Vite config options for electron preload files
* *
* https://vitejs.dev/config/ * https://vitejs.dev/config/
*/ */
preload?: ViteConfigExport preload?: UserViteConfigExport
} }
export type InlineConfig = Omit<ViteConfig, 'base'> & { export type InlineConfig = Omit<ViteConfig, 'base'> & {
@ -73,28 +72,16 @@ export type InlineConfig = Omit<ViteConfig, 'base'> & {
ignoreConfigWarning?: boolean ignoreConfigWarning?: boolean
} }
export type ElectronViteConfigFnObject = (env: ConfigEnv) => ElectronViteConfig export type UserConfigFn = (env: ConfigEnv) => UserConfigSchema | Promise<UserConfigSchema>
export type ElectronViteConfigFnPromise = (env: ConfigEnv) => Promise<ElectronViteConfig> export type UserConfigExport = UserConfigSchema | Promise<UserConfigSchema> | UserConfigFn
export type ElectronViteConfigFn = (env: ConfigEnv) => ElectronViteConfig | Promise<ElectronViteConfig>
export type ElectronViteConfigExport =
| ElectronViteConfig
| Promise<ElectronViteConfig>
| ElectronViteConfigFnObject
| ElectronViteConfigFnPromise
| ElectronViteConfigFn
/** /**
* Type helper to make it easier to use `electron.vite.config.*` * Type helper to make it easier to use `electron.vite.config.*`
* accepts a direct {@link ElectronViteConfig} object, or a function that returns it. * accepts a direct {@link UserConfig} object, or a function that returns it.
* The function receives a object that exposes two properties: * The function receives a object that exposes two properties:
* `command` (either `'build'` or `'serve'`), and `mode`. * `command` (either `'build'` or `'serve'`), and `mode`.
*/ */
export function defineConfig(config: ElectronViteConfig): ElectronViteConfig export function defineConfig(config: UserConfigExport): UserConfigExport {
export function defineConfig(config: Promise<ElectronViteConfig>): Promise<ElectronViteConfig>
export function defineConfig(config: ElectronViteConfigFnObject): ElectronViteConfigFnObject
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport {
return config return config
} }
@ -112,6 +99,8 @@ export async function resolveConfig(
const config = inlineConfig const config = inlineConfig
const mode = inlineConfig.mode || defaultMode const mode = inlineConfig.mode || defaultMode
config.mode = mode
process.env.NODE_ENV = defaultMode process.env.NODE_ENV = defaultMode
let userConfig: UserConfig | undefined let userConfig: UserConfig | undefined
@ -140,8 +129,6 @@ export async function resolveConfig(
if (loadResult.config.main) { if (loadResult.config.main) {
const mainViteConfig: ViteConfig = mergeConfig(loadResult.config.main, deepClone(config)) const mainViteConfig: ViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(mainViteConfig, outDir, 'main') resetOutDir(mainViteConfig, outDir, 'main')
} }
@ -150,8 +137,7 @@ export async function resolveConfig(
...electronMainVitePlugin({ root }), ...electronMainVitePlugin({ root }),
assetPlugin(), assetPlugin(),
workerPlugin(), workerPlugin(),
modulePathPlugin(), importMetaUrlPlugin(),
importMetaPlugin(),
esmShimPlugin() esmShimPlugin()
]) ])
@ -162,15 +148,13 @@ export async function resolveConfig(
if (loadResult.config.preload) { if (loadResult.config.preload) {
const preloadViteConfig: ViteConfig = mergeConfig(loadResult.config.preload, deepClone(config)) const preloadViteConfig: ViteConfig = mergeConfig(loadResult.config.preload, deepClone(config))
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(preloadViteConfig, outDir, 'preload') resetOutDir(preloadViteConfig, outDir, 'preload')
} }
mergePlugins(preloadViteConfig, [ mergePlugins(preloadViteConfig, [
...electronPreloadVitePlugin({ root }), ...electronPreloadVitePlugin({ root }),
assetPlugin(), assetPlugin(),
importMetaPlugin(), importMetaUrlPlugin(),
esmShimPlugin() esmShimPlugin()
]) ])
@ -181,8 +165,6 @@ export async function resolveConfig(
if (loadResult.config.renderer) { if (loadResult.config.renderer) {
const rendererViteConfig: ViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config)) const rendererViteConfig: ViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config))
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(rendererViteConfig, outDir, 'renderer') resetOutDir(rendererViteConfig, outDir, 'renderer')
} }
@ -406,7 +388,7 @@ async function loadConfigFormBundledFile(
configFile: string, configFile: string,
bundledCode: string, bundledCode: string,
isESM: boolean isESM: boolean
): Promise<ElectronViteConfigExport> { ): Promise<UserConfigExport> {
if (isESM) { if (isESM) {
const fileNameTmp = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${Date.now()}.mjs`) const fileNameTmp = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${Date.now()}.mjs`)
fs.writeFileSync(fileNameTmp, bundledCode) fs.writeFileSync(fileNameTmp, bundledCode)
@ -417,7 +399,6 @@ async function loadConfigFormBundledFile(
} finally { } finally {
try { try {
fs.unlinkSync(fileNameTmp) fs.unlinkSync(fileNameTmp)
// eslint-disable-next-line no-empty
} catch {} } catch {}
} }
} else { } else {

View File

@ -41,11 +41,6 @@ export function supportESM(): boolean {
return parseInt(majorVer) >= 28 return parseInt(majorVer) >= 28
} }
export function getElectronMajorVersion(): number {
const majorVer = getElectronMajorVer()
return parseInt(majorVer)
}
export function getElectronPath(): string { export function getElectronPath(): string {
let electronExecPath = process.env.ELECTRON_EXEC_PATH || '' let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
if (!electronExecPath) { if (!electronExecPath) {
@ -69,13 +64,6 @@ export function getElectronNodeTarget(): string {
const electronVer = getElectronMajorVer() const electronVer = getElectronMajorVer()
const nodeVer = { const nodeVer = {
'35': '22.14',
'34': '20.18',
'33': '20.18',
'32': '20.16',
'31': '20.14',
'30': '20.11',
'29': '20.9',
'28': '18.18', '28': '18.18',
'27': '18.17', '27': '18.17',
'26': '18.16', '26': '18.16',
@ -89,7 +77,9 @@ export function getElectronNodeTarget(): string {
'18': '16.13', '18': '16.13',
'17': '16.13', '17': '16.13',
'16': '16.9', '16': '16.9',
'15': '16.5' '15': '16.5',
'14': '14.17',
'13': '14.17'
} }
if (electronVer && parseInt(electronVer) > 10) { if (electronVer && parseInt(electronVer) > 10) {
let target = nodeVer[electronVer] let target = nodeVer[electronVer]
@ -103,13 +93,6 @@ export function getElectronChromeTarget(): string {
const electronVer = getElectronMajorVer() const electronVer = getElectronMajorVer()
const chromeVer = { const chromeVer = {
'35': '134',
'34': '132',
'33': '130',
'32': '128',
'31': '126',
'30': '124',
'29': '122',
'28': '120', '28': '120',
'27': '118', '27': '118',
'26': '116', '26': '116',
@ -123,7 +106,9 @@ export function getElectronChromeTarget(): string {
'18': '100', '18': '100',
'17': '98', '17': '98',
'16': '96', '16': '96',
'15': '94' '15': '94',
'14': '93',
'13': '91'
} }
if (electronVer && parseInt(electronVer) > 10) { if (electronVer && parseInt(electronVer) > 10) {
let target = chromeVer[electronVer] let target = chromeVer[electronVer]

View File

@ -1,4 +1,4 @@
export { type LogLevel, createLogger, mergeConfig, splitVendorChunkPlugin, splitVendorChunk } from 'vite' export { type LogLevel, createLogger, splitVendorChunkPlugin, splitVendorChunk } from 'vite'
export * from './config' export * from './config'
export { createServer } from './server' export { createServer } from './server'
export { build } from './build' export { build } from './build'

View File

@ -87,12 +87,6 @@ export default function assetPlugin(): Plugin {
return wasmHelperCode return wasmHelperCode
} }
if (id.startsWith('\0')) {
// Rollup convention, this id should be handled by the
// plugin that marked it with \0
return
}
const assetResolved = resolveAsset(id) const assetResolved = resolveAsset(id)
if (!assetResolved) { if (!assetResolved) {
return return
@ -115,7 +109,7 @@ export default function assetPlugin(): Plugin {
const hash = this.emitFile({ const hash = this.emitFile({
type: 'asset', type: 'asset',
name: path.basename(file), name: path.basename(file),
source: source as unknown as Uint8Array source
}) })
referenceId = `__VITE_NODE_ASSET__${hash}__` referenceId = `__VITE_NODE_ASSET__${hash}__`
assetCache.set(file, referenceId) assetCache.set(file, referenceId)

View File

@ -198,12 +198,9 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
if (useInRenderer) { if (useInRenderer) {
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.')) config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
} }
if (resolvedConfig.build.minify && protectedStrings.length > 0) {
config.logger.warn(colors.yellow('Strings cannot be protected when minification is enabled.'))
}
}, },
transform(code, id): void | { code: string; map: SourceMapInput } { transform(code, id): void | { code: string; map: SourceMapInput } {
if (config.build.minify || protectedStrings.length === 0 || !filter(id)) return if (protectedStrings.length === 0 || !filter(id)) return
let match: RegExpExecArray | null let match: RegExpExecArray | null
let s: MagicString | undefined let s: MagicString | undefined
@ -310,7 +307,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const chunkFileName = path.resolve(outDir, name) const chunkFileName = path.resolve(outDir, name)
if (bytecodeChunks.includes(name)) { if (bytecodeChunks.includes(name)) {
const bytecodeBuffer = await compileToBytecode(_code) const bytecodeBuffer = await compileToBytecode(_code)
fs.writeFileSync(path.resolve(outDir, name + 'c'), bytecodeBuffer as unknown as Uint8Array) fs.writeFileSync(path.resolve(outDir, name + 'c'), bytecodeBuffer)
if (chunk.isEntry) { if (chunk.isEntry) {
if (!removeBundleJS) { if (!removeBundleJS) {
keepBundle(chunkFileName) keepBundle(chunkFileName)
@ -344,9 +341,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
} }
const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName) const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
_code = hasBytecodeMoudle _code = hasBytecodeMoudle ? _code.replace(useStrict, `${useStrict}\n${bytecodeLoaderBlock}`) : _code
? _code.replace(/("use strict";)|('use strict';)/, `${useStrict}\n${bytecodeLoaderBlock}`)
: _code
} }
fs.writeFileSync(chunkFileName, _code) fs.writeFileSync(chunkFileName, _code)
} }

View File

@ -11,7 +11,7 @@ export interface ElectronPluginOptions {
root?: string root?: string
} }
function findLibEntry(root: string, scope: string): string | undefined { function findLibEntry(root: string, scope: string): string {
for (const name of ['index', scope]) { for (const name of ['index', scope]) {
for (const ext of ['js', 'ts', 'mjs', 'cjs']) { for (const ext of ['js', 'ts', 'mjs', 'cjs']) {
const entryFile = path.resolve(root, 'src', scope, `${name}.${ext}`) const entryFile = path.resolve(root, 'src', scope, `${name}.${ext}`)
@ -20,7 +20,7 @@ function findLibEntry(root: string, scope: string): string | undefined {
} }
} }
} }
return undefined return ''
} }
function findInput(root: string, scope = 'renderer'): string { function findInput(root: string, scope = 'renderer'): string {
@ -198,12 +198,6 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs' const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
const defaultConfig = { const defaultConfig = {
ssr: {
resolve: {
conditions: ['module', 'browser', 'development|production'],
mainFields: ['browser', 'module', 'jsnext:main', 'jsnext']
}
},
build: { build: {
outDir: path.resolve(root, 'out', 'preload'), outDir: path.resolve(root, 'out', 'preload'),
target: nodeTarget, target: nodeTarget,
@ -278,8 +272,7 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
// enable ssr build // enable ssr build
config.build.ssr = true config.build.ssr = true
config.build.ssrEmitAssets = true config.build.ssrEmitAssets = true
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {}) config.ssr = { ...config.ssr, ...{ noExternal: true } }
config.ssr.noExternal = true
} }
}, },
{ {
@ -400,9 +393,7 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
} else { } else {
const targets = Array.isArray(build.target) ? build.target : [build.target] const targets = Array.isArray(build.target) ? build.target : [build.target]
if (targets.some(t => !t.startsWith('chrome') && !/^es((202\d{1})|next)$/.test(t))) { if (targets.some(t => !t.startsWith('chrome') && !/^es((202\d{1})|next)$/.test(t))) {
config.logger.warn( throw new Error('The electron vite renderer config build.target must be "chrome?" or "es?".')
'The electron vite renderer config build.target is not "chrome?" or "es?". This could be a mistake.'
)
} }
} }

View File

@ -8,11 +8,9 @@ import MagicString from 'magic-string'
import type { SourceMapInput } from 'rollup' import type { SourceMapInput } from 'rollup'
import type { Plugin } from 'vite' import type { Plugin } from 'vite'
import { getElectronMajorVersion } from '../electron'
const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/ const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/
const CJSShim_normal = ` const CJSShim = `
// -- CommonJS Shims -- // -- CommonJS Shims --
import __cjs_url__ from 'node:url'; import __cjs_url__ from 'node:url';
import __cjs_path__ from 'node:path'; import __cjs_path__ from 'node:path';
@ -22,14 +20,6 @@ const __dirname = __cjs_path__.dirname(__filename);
const require = __cjs_mod__.createRequire(import.meta.url); const require = __cjs_mod__.createRequire(import.meta.url);
` `
const CJSShim_node_20_11 = `
// -- CommonJS Shims --
import __cjs_mod__ from 'node:module';
const __filename = import.meta.filename;
const __dirname = import.meta.dirname;
const require = __cjs_mod__.createRequire(import.meta.url);
`
const ESMStaticImportRe = 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 /(?<=\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
@ -47,9 +37,6 @@ function findStaticImports(code: string): StaticImport[] {
export default function esmShimPlugin(): Plugin { export default function esmShimPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false let sourcemap: boolean | 'inline' | 'hidden' = false
const CJSShim = getElectronMajorVersion() >= 30 ? CJSShim_node_20_11 : CJSShim_normal
return { return {
name: 'vite:esm-shim', name: 'vite:esm-shim',
apply: 'build', apply: 'build',

View File

@ -1,20 +1,14 @@
import type { Plugin } from 'vite' import type { Plugin } from 'vite'
export default function importMetaPlugin(): Plugin { export default function importMetaUrlPlugin(): Plugin {
return { return {
name: 'vite:import-meta', name: 'vite:import-meta-url',
apply: 'build', apply: 'build',
enforce: 'pre', enforce: 'pre',
resolveImportMeta(property, { format }): string | null { resolveImportMeta(property, { format }): string | null {
if (property === 'url' && format === 'cjs') { if (property === 'url' && format === 'cjs') {
return `require("url").pathToFileURL(__filename).href` return `require("url").pathToFileURL(__filename).href`
} }
if (property === 'filename' && format === 'cjs') {
return `__filename`
}
if (property === 'dirname' && format === 'cjs') {
return `__dirname`
}
return null return null
} }
} }

View File

@ -1,65 +0,0 @@
import type { Plugin } from 'vite'
import type { SourceMapInput } from 'rollup'
import MagicString from 'magic-string'
import { cleanUrl, parseRequest, toRelativePath } from '../utils'
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
/**
* Resolve `?modulePath` import and return the module bundle path.
*/
export default function modulePathPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
return {
name: 'vite:module-path',
apply: 'build',
enforce: 'pre',
configResolved(config): void {
sourcemap = config.build.sourcemap
},
resolveId(id, importer): string | void {
const query = parseRequest(id)
if (query && typeof query.modulePath === 'string') {
return id + `&importer=${importer}`
}
},
load(id): string | void {
const query = parseRequest(id)
if (query && typeof query.modulePath === 'string' && typeof query.importer === 'string') {
const cleanPath = cleanUrl(id)
const hash = this.emitFile({
type: 'chunk',
id: cleanPath,
importer: query.importer
})
const refId = `__VITE_MODULE_PATH__${hash}__`
return `
import { join } from 'path'
export default join(__dirname, ${refId})`
}
},
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null {
if (code.match(modulePathRE)) {
let match: RegExpExecArray | null
const s = new MagicString(code)
while ((match = modulePathRE.exec(code))) {
const [full, hash] = match
const filename = this.getFileName(hash)
const outputFilepath = toRelativePath(filename, chunk.fileName)
const replacement = JSON.stringify(outputFilepath)
s.overwrite(match.index, match.index + full.length, replacement, {
contentOnly: true
})
}
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
}
}
return null
}
}
}

View File

@ -2,7 +2,7 @@ import type { ChildProcess } from 'node:child_process'
import { import {
type UserConfig as ViteConfig, type UserConfig as ViteConfig,
type ViteDevServer, type ViteDevServer,
createServer as viteCreateServer, createServer as ViteCreateServer,
build as viteBuild, build as viteBuild,
createLogger, createLogger,
mergeConfig mergeConfig
@ -80,7 +80,7 @@ export async function createServer(
if (rendererViteConfig) { if (rendererViteConfig) {
logger.info(colors.gray(`\n-----\n`)) logger.info(colors.gray(`\n-----\n`))
server = await viteCreateServer(rendererViteConfig) server = await ViteCreateServer(rendererViteConfig)
if (!server.httpServer) { if (!server.httpServer) {
throw new Error('HTTP server not available') throw new Error('HTTP server not available')

View File

@ -29,10 +29,7 @@ export function parseRequest(id: string): Record<string, string> | null {
} }
export function getHash(text: Buffer | string): string { export function getHash(text: Buffer | string): string {
return createHash('sha256') return createHash('sha256').update(text).digest('hex').substring(0, 8)
.update(text as unknown as Uint8Array)
.digest('hex')
.substring(0, 8)
} }
export function toRelativePath(filename: string, importer: string): string { export function toRelativePath(filename: string, importer: string): string {