From 97a62b75f2b1f2ac814af66d90ae5f2d8e56165c Mon Sep 17 00:00:00 2001 From: alex8088 <244096523@qq.com> Date: Sun, 23 Oct 2022 21:23:27 +0800 Subject: [PATCH] feat: monorepo support --- src/electron.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++-- src/plugin.ts | 69 +++---------------------------------------------- 2 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/electron.ts b/src/electron.ts index 78e225d..c1d321e 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -1,8 +1,11 @@ import path from 'node:path' import fs from 'node:fs' +import { createRequire } from 'node:module' import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process' import { type Logger } from 'vite' +const _require = createRequire(import.meta.url) + const ensureElectronEntryFile = (root = process.cwd()): void => { const pkg = path.join(root, 'package.json') 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 || '' 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') let executablePath if (fs.existsSync(pathFile)) { @@ -39,6 +55,54 @@ export const getElectronPath = (): string => { 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 { ensureElectronEntryFile(root) diff --git a/src/plugin.ts b/src/plugin.ts index acb656b..d30af95 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,6 +3,7 @@ import fs from 'node:fs' import colors from 'picocolors' import { builtinModules, createRequire } from 'node:module' import { type Plugin, type ResolvedConfig, mergeConfig, normalizePath } from 'vite' +import { getElectronNodeTarget, getElectronChromeTarget } from './electron' import { compileToBytecode, bytecodeModuleLoaderCode } from './bytecode' import * as babel from '@babel/core' @@ -58,8 +59,7 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[ config(config): void { const root = options?.root || process.cwd() - const electornVer = getElectronMainVer(root) - const nodeTarget = getElectronNodeTarget(electornVer) + const nodeTarget = getElectronNodeTarget() const defaultConfig = { build: { @@ -131,8 +131,7 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug config(config): void { const root = options?.root || process.cwd() - const electornVer = getElectronMainVer(root) - const nodeTarget = getElectronNodeTarget(electornVer) + const nodeTarget = getElectronNodeTarget() const defaultConfig = { build: { @@ -227,8 +226,7 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu config.mode === 'production' || process.env.NODE_ENV_ELECTRON_VITE === 'production' ? './' : config.base config.root = config.root || './src/renderer' - const electornVer = getElectronMainVer(root) - const chromeTarget = getElectronChromeTarget(electornVer) + const chromeTarget = getElectronChromeTarget() const emptyOutDir = (): boolean => { 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 '' -}