-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathelectron-builder.js
169 lines (139 loc) · 5.12 KB
/
electron-builder.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { join } from 'node:path'
import fse from 'fs-extra'
import { merge } from 'webpack-merge'
import { log, warn, progress } from '../../utils/logger.js'
import { AppBuilder } from '../../app-builder.js'
import { quasarElectronConfig } from './electron-config.js'
import { getPackageJson } from '../../utils/get-package-json.js'
import { getFixedDeps } from '../../utils/get-fixed-deps.js'
export class QuasarModeBuilder extends AppBuilder {
async build () {
await this.#buildFiles()
await this.#writePackageJson()
this.#copyElectronFiles()
this.printSummary(join(this.quasarConf.build.distDir, 'UnPackaged'))
if (this.argv[ 'skip-pkg' ] !== true) {
await this.#packageFiles()
}
}
async #buildFiles () {
const viteConfig = await quasarElectronConfig.vite(this.quasarConf)
await this.buildWithVite('Electron UI', viteConfig)
const mainConfig = await quasarElectronConfig.main(this.quasarConf)
await this.buildWithEsbuild('Electron Main', mainConfig)
const preloadList = await quasarElectronConfig.preloadScriptList(this.quasarConf)
for (const preloadScript of preloadList) {
await this.buildWithEsbuild(`Electron Preload (${ preloadScript.scriptName })`, preloadScript.esbuildConfig)
}
}
async #writePackageJson () {
const { appPkg } = this.ctx.pkg
const pkg = merge({}, appPkg)
if (pkg.dependencies) {
pkg.dependencies = getFixedDeps(pkg.dependencies, this.ctx.appPaths.appDir)
delete pkg.dependencies[ '@quasar/extras' ]
delete pkg.dependencies[ 'register-service-worker' ]
}
// we don't need this (also, faster install time & smaller bundles)
delete pkg.devDependencies
delete pkg.browserslist
delete pkg.scripts
pkg.main = `./electron-main.${ this.quasarConf.metaConf.packageTypeBasedExtension }`
if (typeof this.quasarConf.electron.extendPackageJson === 'function') {
this.quasarConf.electron.extendPackageJson(pkg)
}
this.writeFile(
'UnPackaged/package.json',
this.quasarConf.metaConf.debugging === true
? JSON.stringify(pkg, null, 2)
: JSON.stringify(pkg)
)
}
#copyElectronFiles () {
const patterns = [
'.yarnrc',
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml'
// bun.lockb should be ignored since it errors out with devDeps in package.json
// (error: lockfile has changes, but lockfile is frozen)
].map(filename => ({
from: filename,
to: './UnPackaged'
}))
patterns.push({
from: this.ctx.appPaths.resolve.electron('icons'),
to: './UnPackaged/icons'
})
this.copyFiles(patterns)
// handle .npmrc separately
const npmrc = this.ctx.appPaths.resolve.app('.npmrc')
if (fse.existsSync(npmrc)) {
let content = this.readFile(npmrc)
if (content.indexOf('shamefully-hoist') === -1) {
content += '\n# needed by pnpm\nshamefully-hoist=true'
}
// very important, otherwise PNPM creates symlinks which is NOT
// what we want for an Electron app that should run cross-platform
if (content.indexOf('node-linker') === -1) {
content += '\n# pnpm needs this otherwise it creates symlinks\nnode-linker=hoisted'
}
this.writeFile(
join(this.quasarConf.build.distDir, 'UnPackaged/.npmrc'),
content
)
}
}
async #packageFiles () {
const { appPaths, cacheProxy } = this.ctx
const nodePackager = await cacheProxy.getModule('nodePackager')
nodePackager.install({
cwd: join(this.quasarConf.build.distDir, 'UnPackaged'),
params: this.quasarConf.electron.unPackagedInstallParams,
displayName: 'UnPackaged folder production',
env: 'production'
})
if (typeof this.quasarConf.electron.beforePackaging === 'function') {
log('Running beforePackaging()')
log()
const result = this.quasarConf.electron.beforePackaging({
appPaths,
unpackagedDir: join(this.quasarConf.build.distDir, 'UnPackaged')
})
if (result && result.then) {
await result
}
log()
log('[SUCCESS] Done running beforePackaging()')
}
const bundlerName = this.quasarConf.electron.bundler
const bundlerConfig = this.quasarConf.electron[ bundlerName ]
const { getBundler } = await cacheProxy.getModule('electron')
const bundlerResult = await getBundler(bundlerName)
const bundler = bundlerResult.default || bundlerResult
const pkgBanner = `electron/${ bundlerName }`
return new Promise((resolve, reject) => {
const done = progress('Bundling app with ___...', pkgBanner)
const bundlePromise = bundlerName === 'packager'
? bundler({
...bundlerConfig,
electronVersion: getPackageJson('electron', appPaths.appDir).version
})
: bundler.build(bundlerConfig)
bundlePromise
.then(() => {
log()
done(`${ pkgBanner } built the app`)
log()
resolve()
})
.catch(err => {
log()
warn(`${ pkgBanner } could not build`, 'FAIL')
log()
console.error(err + '\n')
reject()
})
})
}
}