-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
83 lines (66 loc) · 2.12 KB
/
index.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
const { Walker, DepType } = require("flora-colossus");
const { dirname } = require("path");
const defaultOpts = {
externals: [],
includeDeps: true,
};
class ForgeExternalsPlugin {
__isElectronForgePlugin = true;
constructor(opts) {
const options = { ...defaultOpts, ...(opts || {}) };
this._externals = options.externals;
this._includeDeps = options.includeDeps;
}
init = (dir) => {
this._dir = dir;
};
getHook(hookName) {
switch (hookName) {
case "resolveForgeConfig":
return this.resolveForgeConfig;
}
}
getHooks() {
return {
"resolveForgeConfig": this.resolveForgeConfig
};
}
resolveForgeConfig = async (forgeConfig) => {
const foundModules = new Set(this._externals);
if (this._includeDeps) {
for (const external of this._externals) {
const moduleRoot = dirname(
require.resolve(`${external}/package.json`, { paths: [this._dir] })
);
const walker = new Walker(moduleRoot);
// These are private so it's quite nasty!
walker.modules = [];
await walker.walkDependenciesForModule(moduleRoot, DepType.PROD);
walker.modules
.filter((dep) => dep.nativeModuleType === DepType.PROD)
.map((dep) => dep.name)
.forEach((name) => foundModules.add(name));
}
}
// The webpack plugin already sets the ignore function.
const existingIgnoreFn = forgeConfig.packagerConfig.ignore;
// We override it and ensure we include external modules too
forgeConfig.packagerConfig.ignore = (file) => {
const existingResult = existingIgnoreFn(file);
if (existingResult == false) {
return false;
}
if (file === "/node_modules") {
return false;
}
for (const module of foundModules) {
if (file.startsWith(`/node_modules/${module}`) || file.startsWith(`/node_modules/${module.split('/')[0]}`)) {
return false;
}
}
return true;
};
return forgeConfig;
};
}
module.exports = ForgeExternalsPlugin;