feat: monorepo support
This commit is contained in:
parent
a62b20359b
commit
97a62b75f2
|
@ -1,8 +1,11 @@
|
||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
|
import { createRequire } from 'node:module'
|
||||||
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
|
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
|
||||||
import { type Logger } from 'vite'
|
import { type Logger } from 'vite'
|
||||||
|
|
||||||
|
const _require = createRequire(import.meta.url)
|
||||||
|
|
||||||
const ensureElectronEntryFile = (root = process.cwd()): void => {
|
const ensureElectronEntryFile = (root = process.cwd()): void => {
|
||||||
const pkg = path.join(root, 'package.json')
|
const pkg = path.join(root, 'package.json')
|
||||||
if (fs.existsSync(pkg)) {
|
if (fs.existsSync(pkg)) {
|
||||||
|
@ -20,10 +23,23 @@ const ensureElectronEntryFile = (root = process.cwd()): void => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getElectronPath = (): string => {
|
const getElectronMajorVer = (): string => {
|
||||||
|
let majorVer = process.env.ELECTRON_MAJOR_VER || ''
|
||||||
|
if (!majorVer) {
|
||||||
|
const pkg = _require.resolve('electron/package.json')
|
||||||
|
if (fs.existsSync(pkg)) {
|
||||||
|
const version = _require(pkg).version
|
||||||
|
majorVer = version.split('.')[0]
|
||||||
|
process.env.ELECTRON_MAJOR_VER = majorVer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return majorVer
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getElectronPath(): string {
|
||||||
let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
|
let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
|
||||||
if (!electronExecPath) {
|
if (!electronExecPath) {
|
||||||
const electronModulePath = path.resolve(process.cwd(), 'node_modules', 'electron')
|
const electronModulePath = path.dirname(_require.resolve('electron'))
|
||||||
const pathFile = path.join(electronModulePath, 'path.txt')
|
const pathFile = path.join(electronModulePath, 'path.txt')
|
||||||
let executablePath
|
let executablePath
|
||||||
if (fs.existsSync(pathFile)) {
|
if (fs.existsSync(pathFile)) {
|
||||||
|
@ -39,6 +55,54 @@ export const getElectronPath = (): string => {
|
||||||
return electronExecPath
|
return electronExecPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getElectronNodeTarget(): string {
|
||||||
|
const electronVer = getElectronMajorVer()
|
||||||
|
|
||||||
|
const nodeVer = {
|
||||||
|
'21': '16.16',
|
||||||
|
'20': '16.15',
|
||||||
|
'19': '16.14',
|
||||||
|
'18': '16.13',
|
||||||
|
'17': '16.13',
|
||||||
|
'16': '16.9',
|
||||||
|
'15': '16.5',
|
||||||
|
'14': '14.17',
|
||||||
|
'13': '14.17',
|
||||||
|
'12': '14.16',
|
||||||
|
'11': '12.18'
|
||||||
|
}
|
||||||
|
if (electronVer && parseInt(electronVer) > 10) {
|
||||||
|
let target = nodeVer[electronVer]
|
||||||
|
if (!target) target = Object.values(nodeVer).reverse()[0]
|
||||||
|
return 'node' + target
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getElectronChromeTarget(): string {
|
||||||
|
const electronVer = getElectronMajorVer()
|
||||||
|
|
||||||
|
const chromeVer = {
|
||||||
|
'21': '106',
|
||||||
|
'20': '104',
|
||||||
|
'19': '102',
|
||||||
|
'18': '100',
|
||||||
|
'17': '98',
|
||||||
|
'16': '96',
|
||||||
|
'15': '94',
|
||||||
|
'14': '93',
|
||||||
|
'13': '91',
|
||||||
|
'12': '89',
|
||||||
|
'11': '87'
|
||||||
|
}
|
||||||
|
if (electronVer && parseInt(electronVer) > 10) {
|
||||||
|
let target = chromeVer[electronVer]
|
||||||
|
if (!target) target = Object.values(chromeVer).reverse()[0]
|
||||||
|
return 'chrome' + target
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
export function startElectron(root: string | undefined, logger: Logger): ChildProcessWithoutNullStreams {
|
export function startElectron(root: string | undefined, logger: Logger): ChildProcessWithoutNullStreams {
|
||||||
ensureElectronEntryFile(root)
|
ensureElectronEntryFile(root)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import fs from 'node:fs'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { builtinModules, createRequire } from 'node:module'
|
import { builtinModules, createRequire } from 'node:module'
|
||||||
import { type Plugin, type ResolvedConfig, mergeConfig, normalizePath } from 'vite'
|
import { type Plugin, type ResolvedConfig, mergeConfig, normalizePath } from 'vite'
|
||||||
|
import { getElectronNodeTarget, getElectronChromeTarget } from './electron'
|
||||||
import { compileToBytecode, bytecodeModuleLoaderCode } from './bytecode'
|
import { compileToBytecode, bytecodeModuleLoaderCode } from './bytecode'
|
||||||
import * as babel from '@babel/core'
|
import * as babel from '@babel/core'
|
||||||
|
|
||||||
|
@ -58,8 +59,7 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
|
||||||
config(config): void {
|
config(config): void {
|
||||||
const root = options?.root || process.cwd()
|
const root = options?.root || process.cwd()
|
||||||
|
|
||||||
const electornVer = getElectronMainVer(root)
|
const nodeTarget = getElectronNodeTarget()
|
||||||
const nodeTarget = getElectronNodeTarget(electornVer)
|
|
||||||
|
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
build: {
|
build: {
|
||||||
|
@ -131,8 +131,7 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
|
||||||
config(config): void {
|
config(config): void {
|
||||||
const root = options?.root || process.cwd()
|
const root = options?.root || process.cwd()
|
||||||
|
|
||||||
const electornVer = getElectronMainVer(root)
|
const nodeTarget = getElectronNodeTarget()
|
||||||
const nodeTarget = getElectronNodeTarget(electornVer)
|
|
||||||
|
|
||||||
const defaultConfig = {
|
const defaultConfig = {
|
||||||
build: {
|
build: {
|
||||||
|
@ -227,8 +226,7 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
|
||||||
config.mode === 'production' || process.env.NODE_ENV_ELECTRON_VITE === 'production' ? './' : config.base
|
config.mode === 'production' || process.env.NODE_ENV_ELECTRON_VITE === 'production' ? './' : config.base
|
||||||
config.root = config.root || './src/renderer'
|
config.root = config.root || './src/renderer'
|
||||||
|
|
||||||
const electornVer = getElectronMainVer(root)
|
const chromeTarget = getElectronChromeTarget()
|
||||||
const chromeTarget = getElectronChromeTarget(electornVer)
|
|
||||||
|
|
||||||
const emptyOutDir = (): boolean => {
|
const emptyOutDir = (): boolean => {
|
||||||
let outDir = config.build?.outDir
|
let outDir = config.build?.outDir
|
||||||
|
@ -507,62 +505,3 @@ export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getElectronMainVer(root: string): string {
|
|
||||||
let mainVer = process.env.ELECTRON_MAIN_VER || ''
|
|
||||||
if (!mainVer) {
|
|
||||||
const electronModulePath = require.resolve('electron')
|
|
||||||
const pkg = path.join(electronModulePath, '../package.json')
|
|
||||||
if (fs.existsSync(pkg)) {
|
|
||||||
const require = createRequire(import.meta.url)
|
|
||||||
const version = require(pkg).version
|
|
||||||
mainVer = version.split('.')[0]
|
|
||||||
process.env.ELECTRON_MAIN_VER = mainVer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mainVer
|
|
||||||
}
|
|
||||||
|
|
||||||
function getElectronNodeTarget(electronVer: string): string {
|
|
||||||
const nodeVer = {
|
|
||||||
'21': '16.16',
|
|
||||||
'20': '16.15',
|
|
||||||
'19': '16.14',
|
|
||||||
'18': '16.13',
|
|
||||||
'17': '16.13',
|
|
||||||
'16': '16.9',
|
|
||||||
'15': '16.5',
|
|
||||||
'14': '14.17',
|
|
||||||
'13': '14.17',
|
|
||||||
'12': '14.16',
|
|
||||||
'11': '12.18'
|
|
||||||
}
|
|
||||||
if (electronVer && parseInt(electronVer) > 10) {
|
|
||||||
let target = nodeVer[electronVer]
|
|
||||||
if (!target) target = Object.values(nodeVer).reverse()[0]
|
|
||||||
return 'node' + target
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
function getElectronChromeTarget(electronVer: string): string {
|
|
||||||
const chromeVer = {
|
|
||||||
'21': '106',
|
|
||||||
'20': '104',
|
|
||||||
'19': '102',
|
|
||||||
'18': '100',
|
|
||||||
'17': '98',
|
|
||||||
'16': '96',
|
|
||||||
'15': '94',
|
|
||||||
'14': '93',
|
|
||||||
'13': '91',
|
|
||||||
'12': '89',
|
|
||||||
'11': '87'
|
|
||||||
}
|
|
||||||
if (electronVer && parseInt(electronVer) > 10) {
|
|
||||||
let target = chromeVer[electronVer]
|
|
||||||
if (!target) target = Object.values(chromeVer).reverse()[0]
|
|
||||||
return 'chrome' + target
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue