-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathconfig.ts
149 lines (130 loc) · 4.25 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import fs from 'fs'
import path from 'path'
import {
type InlineConfig,
type ResolvedConfig,
type Plugin,
mergeConfig,
normalizePath,
} from 'vite'
import type { Configuration } from './types'
import { resolveModules } from 'vite-plugin-electron-renderer/plugins/use-node.js'
export interface Runtime {
proc: 'main' | 'preload'
config: Configuration
viteConfig: ResolvedConfig
}
export function resolveRuntime(
proc: 'main' | 'preload',
config: Configuration,
viteConfig: ResolvedConfig,
): Runtime {
return { proc, config, viteConfig }
}
export function resolveBuildConfig(runtime: Runtime): InlineConfig {
const { proc, config, viteConfig } = runtime
const conf: InlineConfig = {
// 🚧 Avoid recursive build caused by load config file
configFile: false,
envFile: false,
publicDir: false,
build: {
emptyOutDir: false,
minify: process.env./* from mode option */NODE_ENV === 'production',
},
}
// In practice, there may be multiple Electron-Preload, but only one Electron-Main
if (proc === 'preload') {
// Electron-Preload
conf.build.rollupOptions = {
...conf.build.rollupOptions,
input: config[proc].input,
output: {
format: 'cjs',
// Only one file will be bundled, which is consistent with the behavior of `build.lib`
manualChunks: {},
// https://github.com/vitejs/vite/blob/09c4fc01a83b84f77b7292abcfe7500f0e948db6/packages/vite/src/node/build.ts#L467
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
assetFileNames: '[name].[ext]',
},
}
} else {
// Electron-Main
// TODO: consider also support `build.rollupOptions`
conf.build.lib = {
entry: config[proc].entry,
formats: ['cjs'],
fileName: () => '[name].js',
}
}
// Assign default dir
conf.build.outDir = normalizePath(`${viteConfig.build.outDir}/electron`)
return mergeConfig(conf, config[proc]?.vite || {}) as InlineConfig
}
export function createWithExternal(runtime: Runtime) {
const { proc, config, viteConfig } = runtime
const { builtins, dependencies } = resolveModules(viteConfig.root, config[proc])
const modules = builtins.concat(dependencies)
return function withExternal(ILCG: InlineConfig) {
if (!ILCG.build) ILCG.build = {}
if (!ILCG.build.rollupOptions) ILCG.build.rollupOptions = {}
let external = ILCG.build.rollupOptions.external
if (
Array.isArray(external) ||
typeof external === 'string' ||
external instanceof RegExp
) {
external = modules.concat(external as string[])
} else if (typeof external === 'function') {
const original = external
external = function (source, importer, isResolved) {
if (modules.includes(source)) {
return true
}
return original(source, importer, isResolved)
}
} else {
external = modules
}
ILCG.build.rollupOptions.external = external
return ILCG
}
}
export function checkPkgMain(runtime: Runtime, electronMainBuildResolvedConfig: ResolvedConfig) {
const mainConfig = electronMainBuildResolvedConfig
const { config, viteConfig } = runtime
const cwd = process.cwd()
const pkgId = path.join(cwd, 'package.json')
if (!fs.existsSync(pkgId)) return
const distfile = path.resolve(
mainConfig.root,
mainConfig.build.outDir,
path.parse(config.main.entry).name,
)
// https://github.com/electron-vite/vite-plugin-electron/blob/5cd2c2ce68bb76b2a1770d50aa4164a59ab8110c/packages/electron/src/config.ts#L57
+ '.js'
let message: string
const pkg = require(pkgId)
if (!(pkg.main && distfile.endsWith(pkg.main))) {
message = `
[${new Date().toLocaleString()}]
Command: "vite ${viteConfig.command}".
The main field in package.json may be incorrect, which causes the App to fail to start.
File build path: "${distfile}".
Recommended main value: "${distfile.replace(cwd + '/', '')}".
`
}
if (message) {
fs.appendFileSync(path.join(cwd, 'vite-plugin-electron.log'), message)
return message
}
}
checkPkgMain.buildElectronMainPlugin = function buildElectronMainPlugin(runtime: Runtime): Plugin {
return {
name: 'vite-plugin-electron:check-package.json-main',
configResolved(config) {
checkPkgMain(runtime, config)
},
}
}