-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
269 lines (221 loc) · 7.35 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const BluebirdPromise = require("bluebird");
const { readJson } = require("fs-extra");
const { Lazy } = require("lazy-val");
const path = require("path");
const { deepAssign } = require("read-config-file/out/deepAssign");
const merge = require("webpack-merge");
const { getElectronWebpackConfiguration, getPackageMetadata } = require("./config");
const { configureTypescript } = require("./configurators/ts");
const { getFirstExistingFile } = require("./util");
const { MainTarget } = require("./targets/MainTarget");
const { RendererTarget } = require("./targets/RendererTarget");
function getAppConfiguration() {
return BluebirdPromise.filter([configure("main"), configure("renderer")], (it) => it != null);
}
function getRendererConfiguration() {
return configure("renderer");
}
class WebpackConfigurator {
constructor(type, env, electronWebpackConfiguration, metadata) {
this.type = type;
this.env = env;
this.electronWebpackConfiguration = electronWebpackConfiguration;
this.metadata = metadata;
this.electronVersionPromise = new Lazy(() => getInstalledElectronVersion(this.projectDir));
this._configuration = null;
this.rules = [];
this.plugins = [];
this.extensions = [".js", ".json"];
this._electronVersion = null;
this.entryFiles = [];
if (metadata.dependencies == null) {
metadata.dependencies = {};
}
if (metadata.devDependencies == null) {
metadata.devDependencies = {};
}
this.isRenderer = type.startsWith("renderer");
this.isProduction = this.env.production == null ? process.env.NODE_ENV === "production" : this.env.production;
this.projectDir = electronWebpackConfiguration.projectDir;
this.sourceDir = this.getSourceDirectory(this.type);
this.commonDistDirectory = this.electronWebpackConfiguration.commonDistDirectory;
process.env.BABEL_ENV = type;
}
get config() {
return this._configuration;
}
get electronVersion() {
return this._electronVersion;
}
getSourceDirectory(type) {
return path.join(this.projectDir, "src", this.isRenderer ? "renderer" : type);
}
hasDevDependency(name) {
return name in this.metadata.devDependencies;
}
/**
* Returns the names of devDependencies that match a given string or regex.
* If no matching dependencies are found, an empty array is returned.
*
* @return list of matching dependency names, e.g. `["@babel/preset-react", "@babel/preset-stage-0"]`
*/
getMatchingDevDependencies(options) {
const includes = options.includes || [];
const excludes = new Set(options.excludes || []);
return Object.keys(this.metadata.devDependencies).filter(
(name) => !excludes.has(name) && includes.some((prefix) => name.startsWith(prefix))
);
}
async configure() {
this._configuration = {
context: this.projectDir,
devtool: false,
externals: this.computeExternals(),
node: {
__dirname: !this.isProduction,
__filename: !this.isProduction,
},
output: {
filename: "[name].js",
chunkFilename: "[name].bundle.js",
libraryTarget: "commonjs2",
path: path.join(this.commonDistDirectory, this.type),
},
target: `electron-${this.type}`,
resolve: {
alias: {
"@": this.sourceDir,
},
extensions: this.extensions,
},
module: {
rules: this.rules,
},
plugins: this.plugins,
};
this._electronVersion = await this.electronVersionPromise.value;
const target = (() => {
switch (this.type) {
case "renderer":
return new RendererTarget();
case "main":
return new MainTarget();
}
})();
target.configureRules(this);
await Promise.all([target.configurePlugins(this), configureTypescript(this)]);
if (this.config.entry == null) {
this.entryFiles.push(await computeEntryFile(this.sourceDir, this.projectDir));
this.config.entry = {
[this.type]: this.entryFiles,
};
const mainConfiguration = this.electronWebpackConfiguration.main || {};
let extraEntries = mainConfiguration.extraEntries;
if (this.type === "main" && extraEntries != null) {
if (typeof extraEntries === "string") {
extraEntries = [extraEntries];
}
if (Array.isArray(extraEntries)) {
for (const p of extraEntries) {
this.config.entry[path.basename(p, path.extname(p))] = p;
}
} else {
Object.assign(this.config.entry, extraEntries);
}
}
}
this._configuration = await this.applyCustomModifications(this.config);
return this.config;
}
applyCustomModifications(config) {
const { renderer, main } = this.electronWebpackConfiguration;
const applyCustom = (configPath) => {
const customModule = require(path.join(this.projectDir, configPath));
if (typeof customModule === "function") {
return customModule(config, this);
} else {
return merge.smart(config, customModule);
}
};
if (this.type === "renderer" && renderer && renderer.webpackConfig) {
return applyCustom(renderer.webpackConfig);
} else if (this.type === "main" && main && main.webpackConfig) {
return applyCustom(main.webpackConfig);
} else {
return config;
}
}
computeExternals() {
const whiteListedModules = new Set(this.electronWebpackConfiguration.whiteListedModules || []);
if (this.isRenderer) {
whiteListedModules.add("react");
whiteListedModules.add("react-dom");
}
const filter = (name) => !name.startsWith("@types/") && (whiteListedModules == null || !whiteListedModules.has(name));
const externals = Object.keys(this.metadata.dependencies).filter(filter);
externals.push("electron");
externals.push("webpack");
if (this.type === "main") {
externals.push("webpack/hot/log-apply-result");
externals.push("better-electron-webpack/electron-main-hmr/HmrClient");
}
if (this.electronWebpackConfiguration.externals != null) {
return externals.concat(this.electronWebpackConfiguration.externals);
}
return externals;
}
}
async function createConfigurator(type, env) {
if (env != null) {
const _env = env;
for (const name of ["minify", "production"]) {
if (_env[name] === "true") {
_env[name] = true;
} else if (_env[name] === "false") {
_env[name] = false;
}
}
}
if (env == null) env = {};
const projectDir = (env.configuration || {}).projectDir || process.cwd();
const packageMetadata = getPackageMetadata(projectDir);
const electronWebpackConfig = await getElectronWebpackConfiguration({
packageMetadata,
projectDir,
});
if (env.configuration != null) deepAssign(electronWebpackConfig, env.configuration);
return new WebpackConfigurator(type, env, electronWebpackConfig, await packageMetadata.value);
}
async function configure(type, env) {
const configurator = await createConfigurator(type, env);
return await configurator.configure();
}
module.exports = {
getAppConfiguration,
getRendererConfiguration,
WebpackConfigurator,
createConfigurator,
configure,
};
async function computeEntryFile(srcDir) {
const candidates = [];
for (const ext of ["ts", "js", "tsx", "jsx"]) {
for (const name of ["index", "main", "app"]) {
candidates.push(`${name}.${ext}`);
}
}
const file = await getFirstExistingFile(candidates, srcDir);
if (file == null) {
throw new Error(`Cannot find entry file index.ts (or main.ts, or app.ts, or index.js, or main.js, or app.js)`);
}
return file;
}
async function getInstalledElectronVersion(projectDir) {
try {
return (await readJson(path.join(projectDir, "node_modules", "electron", "package.json"))).version;
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
}