-
Notifications
You must be signed in to change notification settings - Fork 294
/
main.js
159 lines (143 loc) · 4.39 KB
/
main.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
#!/usr/bin/env node
const yargs = require("yargs");
const Electron = require("./utils/Electron");
const processReleaseNotes = require("./utils/processReleaseNotes");
const {
processNativeModules,
copyFolderRecursivelySync,
esBuildExternalsPlugin,
} = require("native-modules-tools");
const path = require("path");
const { esbuild, NodeExternalsPlugin } = require("esbuild-utils");
const { createServer } = require("vite");
const { buildMainEnv, buildRendererEnv, buildViteConfig, lldRoot } = require("./utils");
const startDev = async argv => {
const electron = new Electron("./.webpack/main.bundle.js");
const OnRebuildPlugin = {
name: "onRebuild",
setup(build) {
build.onEnd(result => {
if (result.errors.length > 0) {
console.log(`Build ended with ${result.errors.length} errors.`);
console.log(result.errors.map(error => error.message).join("\n"));
} else {
electron.reload();
}
});
},
};
const mainConfig = {
...require("./config/main.esbuild"),
define: buildMainEnv("development", argv),
plugins: [
...(require("./config/main.esbuild").plugins || []),
NodeExternalsPlugin,
OnRebuildPlugin,
],
minify: false,
};
const preloaderConfig = {
...require("./config/preloader.esbuild"),
define: buildMainEnv("development", argv),
plugins: [...(require("./config/preloader.esbuild").plugins || []), OnRebuildPlugin],
minify: false,
};
const webviewPreloaderConfig = {
...require("./config/webviewPreloader.esbuild"),
define: buildMainEnv("development", argv),
plugins: [...(require("./config/webviewPreloader.esbuild").plugins || []), OnRebuildPlugin],
minify: false,
};
const swapConnectWebviewPreloaderConfig = {
...require("./config/swapConnectWebviewPreloader.esbuild"),
define: buildMainEnv("development", argv),
plugins: [
...(require("./config/swapConnectWebviewPreloader.esbuild").plugins || []),
OnRebuildPlugin,
],
minify: false,
};
try {
await processReleaseNotes();
} catch (error) {
console.log(error);
}
const rendererServer = await createServer(buildViteConfig(argv));
const contexts = await Promise.all([
esbuild.context(mainConfig),
esbuild.context(preloaderConfig),
esbuild.context(webviewPreloaderConfig),
esbuild.context(swapConnectWebviewPreloaderConfig),
]);
await rendererServer.listen();
await Promise.all(contexts.map(context => context.watch()));
rendererServer.printUrls();
electron.start();
};
const build = async argv => {
let mappedNativeModules;
if (!process.env.TESTING) {
// Find native modules and copy them to ./dist/node_modules with their dependencies.
mappedNativeModules = processNativeModules({ root: lldRoot, destination: "dist" });
// Also copy to ./node_modules to be able to run the production build with playwright.
copyFolderRecursivelySync(
path.join(lldRoot, "dist", "node_modules"),
path.join(lldRoot, "node_modules"),
);
}
try {
await processReleaseNotes();
} catch (error) {
console.log(error);
}
const mainConfig = require("./config/main.esbuild");
await Promise.all([
esbuild.build({
...mainConfig,
define: buildMainEnv("production", argv),
plugins: [
...(mainConfig.plugins || []),
...(!mappedNativeModules
? [NodeExternalsPlugin]
: [esBuildExternalsPlugin(mappedNativeModules)]),
],
}),
esbuild.build({
...require("./config/preloader.esbuild"),
define: buildMainEnv("production", argv),
}),
esbuild.build({
...require("./config/webviewPreloader.esbuild"),
define: buildMainEnv("production", argv),
}),
esbuild.build({
...require("./config/swapConnectWebviewPreloader.esbuild"),
define: buildMainEnv("production", argv),
}),
esbuild.build({
...require("./config/renderer.esbuild"),
define: buildRendererEnv("production"),
}),
]);
};
yargs
.usage("Usage: $0 <command> [options]")
.command({
command: ["dev", "$0"],
desc: "start the development workflow",
builder: yargs =>
yargs.option("port", {
alias: "p",
type: "number",
default: 8080,
}),
handler: startDev,
})
.command({
command: "build",
desc: "build the app for production",
handler: build,
})
.help("h")
.alias("h", "help")
.parse();