-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
electron-builder.mjs
109 lines (101 loc) · 3.25 KB
/
electron-builder.mjs
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
import pkg from './package.json' with {type: 'json'};
import mapWorkspaces from '@npmcli/map-workspaces';
import {join} from 'node:path';
import {pathToFileURL} from 'node:url';
export default /** @type import('electron-builder').Configuration */
({
directories: {
output: 'dist',
buildResources: 'buildResources',
},
generateUpdatesFilesForAllChannels: true,
linux: {
target: ['deb'],
},
/**
* It is recommended to avoid using non-standard characters such as spaces in artifact names,
* as they can unpredictably change during deployment, making them impossible to locate and download for update.
*/
artifactName: '${productName}-${version}-${os}-${arch}.${ext}',
files: [
'packages/entry-point.js',
'!node_modules/@vite-electron-builder/**',
...await getListOfFilesFromEachWorkspace(),
],
});
/**
* By default, electron-builder copies each package into the output compilation entirety,
* including the source code, tests, configuration, assets, and any other files.
*
* So you may get compiled app structure like this:
* ```
* app/
* ├── node_modules/
* │ └── workspace-packages/
* │ ├── package-a/
* │ │ ├── src/ # Garbage. May be safely removed
* │ │ ├── dist/
* │ │ │ └── index.js # Runtime code
* │ │ ├── vite.config.js # Garbage
* │ │ ├── .env # some sensitive config
* │ │ └── package.json
* │ ├── package-b/
* │ ├── package-c/
* │ └── package-d/
* ├── packages/
* │ └── entry-point.js
* └── package.json
* ```
*
* To prevent this, we read the “files”
* property from each package's package.json
* and add all files that do not match the patterns to the exclusion list.
*
* This way,
* each package independently determines which files will be included in the final compilation and which will not.
*
* So if `package-a` in its `package.json` describes
* ```json
* {
* "name": "package-a",
* "files": [
* "dist/**\/"
* ]
* }
* ```
*
* Then in the compilation only those files and `package.json` will be included:
* ```
* app/
* ├── node_modules/
* │ └── workspace-packages/
* │ ├── package-a/
* │ │ ├── dist/
* │ │ │ └── index.js # Runtime code
* │ │ └── package.json
* │ ├── package-b/
* │ ├── package-c/
* │ └── package-d/
* ├── packages/
* │ └── entry-point.js
* └── package.json
* ```
*/
async function getListOfFilesFromEachWorkspace() {
/**
* @type {Map<string, string>}
*/
const workspaces = await mapWorkspaces({
cwd: process.cwd(),
pkg,
});
const allFilesToExclude = [];
for (const [name, path] of workspaces) {
const pkgPath = join(path, 'package.json');
const {default: workspacePkg} = await import(pathToFileURL(pkgPath), {with: {type: 'json'}});
let patterns = workspacePkg.files || ['dist/**', 'package.json'];
patterns = patterns.map(p => join('node_modules', name, p));
allFilesToExclude.push(...patterns);
}
return allFilesToExclude;
}