mirror of
https://github.com/alex8088/electron-vite.git
synced 2026-11-02 14:51:51 +08:00
refactor: share and normalize build output resolution
This commit is contained in:
parent
0655933704
commit
19050aaec9
@ -1,10 +1,10 @@
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import { randomBytes } from 'node:crypto'
|
import { randomBytes } from 'node:crypto'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { type Plugin, type LibraryOptions, type Rolldown, normalizePath } from 'vite'
|
import { type Plugin, type Rolldown, normalizePath } from 'vite'
|
||||||
import * as babel from '@babel/core'
|
import * as babel from '@babel/core'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import { toRelativePath } from '../utils'
|
import { toRelativePath, resolveBuildOutputs } from '../utils'
|
||||||
import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler'
|
import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler'
|
||||||
|
|
||||||
// Inspired by https://github.com/bytenode/bytenode
|
// Inspired by https://github.com/bytenode/bytenode
|
||||||
@ -140,10 +140,9 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
const build = config.build
|
const build = config.build
|
||||||
const resolvedOutputs = resolveBuildOutputs(build.rolldownOptions.output, build.lib)
|
const outputs = resolveBuildOutputs(build.rolldownOptions.output, build.lib)
|
||||||
if (resolvedOutputs) {
|
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
|
||||||
const output = outputs[0]
|
const output = outputs[0]
|
||||||
|
if (output) {
|
||||||
if (output.format === 'es') {
|
if (output.format === 'es') {
|
||||||
config.logger.warn(
|
config.logger.warn(
|
||||||
colors.yellow(
|
colors.yellow(
|
||||||
@ -280,17 +279,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveBuildOutputs(
|
|
||||||
outputs: Rolldown.OutputOptions | Rolldown.OutputOptions[] | undefined,
|
|
||||||
libOptions: LibraryOptions | false
|
|
||||||
): Rolldown.OutputOptions | Rolldown.OutputOptions[] | undefined {
|
|
||||||
if (libOptions && !Array.isArray(outputs)) {
|
|
||||||
const libFormats = libOptions.formats || []
|
|
||||||
return libFormats.map(format => ({ ...outputs, format }))
|
|
||||||
}
|
|
||||||
return outputs
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ProtectStringsPluginState extends babel.PluginPass {
|
interface ProtectStringsPluginState extends babel.PluginPass {
|
||||||
opts: { protectedStrings: Set<string> }
|
opts: { protectedStrings: Set<string> }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,9 @@ import path from 'node:path'
|
|||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { builtinModules } from 'node:module'
|
import { builtinModules } from 'node:module'
|
||||||
import colors from 'picocolors'
|
import colors from 'picocolors'
|
||||||
import { type Plugin, type LibraryOptions, type Rolldown, type UserConfig, mergeConfig, normalizePath } from 'vite'
|
import { type Plugin, type UserConfig, mergeConfig, normalizePath } from 'vite'
|
||||||
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
|
||||||
import { loadPackageData } from '../utils'
|
import { loadPackageData, resolveBuildOutputs } from '../utils'
|
||||||
|
|
||||||
export interface ElectronPluginOptions {
|
export interface ElectronPluginOptions {
|
||||||
root?: string
|
root?: string
|
||||||
@ -38,17 +38,6 @@ function processEnvDefine(): Record<string, string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveBuildOutputs(
|
|
||||||
outputs: Rolldown.OutputOptions | Rolldown.OutputOptions[] | undefined,
|
|
||||||
libOptions: LibraryOptions | false
|
|
||||||
): Rolldown.OutputOptions | Rolldown.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 {
|
export function electronMainConfigPresetPlugin(options?: ElectronPluginOptions): Plugin {
|
||||||
return {
|
return {
|
||||||
name: 'vite:electron-main-config-preset',
|
name: 'vite:electron-main-config-preset',
|
||||||
@ -148,16 +137,15 @@ export function electronMainConfigValidatorPlugin(): Plugin {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
const outputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (outputs.length > 0) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
|
||||||
if (outputs.length > 1) {
|
if (outputs.length > 1) {
|
||||||
throw new Error('The electron vite main config does not support multiple outputs.')
|
throw new Error('The electron vite main config does not support multiple outputs.')
|
||||||
} else {
|
} else {
|
||||||
const outpout = outputs[0]
|
const output = outputs[0]
|
||||||
if (['es', 'cjs'].includes(outpout.format || '')) {
|
if (['es', 'cjs'].includes(output.format || '')) {
|
||||||
if (outpout.format === 'es' && !supportESM()) {
|
if (output.format === 'es' && !supportESM()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'The electron vite main config output format does not support "es", ' +
|
'The electron vite main config output format does not support "es", ' +
|
||||||
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
||||||
@ -232,12 +220,9 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
const buildConfig = mergeConfig(defaultConfig.build, build)
|
const buildConfig = mergeConfig(defaultConfig.build, build)
|
||||||
config.build = buildConfig
|
config.build = buildConfig
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(config.build.rolldownOptions!.output, config.build.lib || false)
|
const outputs = resolveBuildOutputs(config.build.rolldownOptions!.output, config.build.lib)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (outputs.some(({ format }) => format === 'es')) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
|
||||||
|
|
||||||
if (outputs.find(({ format }) => format === 'es')) {
|
|
||||||
if (Array.isArray(config.build.rolldownOptions!.output)) {
|
if (Array.isArray(config.build.rolldownOptions!.output)) {
|
||||||
config.build.rolldownOptions!.output.forEach(output => {
|
config.build.rolldownOptions!.output.forEach(output => {
|
||||||
if (output.format === 'es') {
|
if (output.format === 'es') {
|
||||||
@ -252,7 +237,6 @@ export function electronPreloadConfigPresetPlugin(options?: ElectronPluginOption
|
|||||||
: path.posix.join(build.assetsDir || defaultConfig.build.assetsDir, '[name]-[hash].mjs')
|
: path.posix.join(build.assetsDir || defaultConfig.build.assetsDir, '[name]-[hash].mjs')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
config.define = config.define || {}
|
config.define = config.define || {}
|
||||||
config.define = { ...processEnvDefine(), ...config.define }
|
config.define = { ...processEnvDefine(), ...config.define }
|
||||||
@ -302,16 +286,15 @@ export function electronPreloadConfigValidatorPlugin(): Plugin {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedOutputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
const outputs = resolveBuildOutputs(rolldownOptions.output, libOptions)
|
||||||
|
|
||||||
if (resolvedOutputs) {
|
if (outputs.length > 0) {
|
||||||
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
|
|
||||||
if (outputs.length > 1) {
|
if (outputs.length > 1) {
|
||||||
throw new Error('The electron vite preload config does not support multiple outputs.')
|
throw new Error('The electron vite preload config does not support multiple outputs.')
|
||||||
} else {
|
} else {
|
||||||
const outpout = outputs[0]
|
const output = outputs[0]
|
||||||
if (['es', 'cjs'].includes(outpout.format || '')) {
|
if (['es', 'cjs'].includes(output.format || '')) {
|
||||||
if (outpout.format === 'es' && !supportESM()) {
|
if (output.format === 'es' && !supportESM()) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'The electron vite preload config output format does not support "es", ' +
|
'The electron vite preload config output format does not support "es", ' +
|
||||||
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
'you can upgrade electron to the latest version or switch to "cjs" format.'
|
||||||
|
|||||||
19
src/utils.ts
19
src/utils.ts
@ -3,7 +3,7 @@ import path from 'node:path'
|
|||||||
import fs from 'node:fs'
|
import fs from 'node:fs'
|
||||||
import { createHash } from 'node:crypto'
|
import { createHash } from 'node:crypto'
|
||||||
import { createRequire } from 'node:module'
|
import { createRequire } from 'node:module'
|
||||||
import { loadEnv as viteLoadEnv } from 'vite'
|
import { type LibraryOptions, type Rolldown, loadEnv as viteLoadEnv } from 'vite'
|
||||||
|
|
||||||
export function isObject(value: unknown): value is Record<string, unknown> {
|
export function isObject(value: unknown): value is Record<string, unknown> {
|
||||||
return Object.prototype.toString.call(value) === '[object Object]'
|
return Object.prototype.toString.call(value) === '[object Object]'
|
||||||
@ -32,6 +32,23 @@ export function toRelativePath(filename: string, importer: string): string {
|
|||||||
return relPath.startsWith('.') ? relPath : `./${relPath}`
|
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()`) .
|
* Load `.env` files within the `envDir` (default: `process.cwd()`) .
|
||||||
* By default, only env variables prefixed with `VITE_`, `MAIN_VITE_`, `PRELOAD_VITE_` and
|
* By default, only env variables prefixed with `VITE_`, `MAIN_VITE_`, `PRELOAD_VITE_` and
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user