akey-umi-plugin-electron-builder 4.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 BySlin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # umi-plugin-electron-builder
2
+
3
+ 本插件提供基于umijs的electron的开发及打包,无需修改项目结构,支持混合项目结构和main+renderer项目结构,仅支持 umijs4
4
+
5
+ 混合项目结构示例 <a href="https://github.com/BySlin/umi-plugin-electron-builder/tree/main/examples/demo">点此访问</a>
6
+
7
+ main+renderer项目结构示例 <a href="https://github.com/BySlin/umi-plugin-electron-builder/tree/main/examples/main%2Brenderer">点此访问</a>
8
+
9
+ 使用混合项目结构时,插件可自动生成项目文件,使用main+renderer项目结构时,请参考示例修改目录结构
10
+
11
+ <a href="https://www.npmjs.com/package/umi-plugin-electron-builder"><img src="https://img.shields.io/npm/v/umi-plugin-electron-builder.svg?sanitize=true" alt="Version"></a>
12
+
13
+ [更新日志](https://github.com/BySlin/umi-plugin-electron-builder/blob/main/CHANGELOG.md)
14
+
15
+ [umi3请使用2.x版本插件,点此访问](https://github.com/BySlin/umi-plugin-electron-builder/tree/2.x)
16
+
17
+ ## 安装
18
+
19
+ ```shell
20
+ $ yarn add umi-plugin-electron-builder --dev
21
+ ```
22
+
23
+ umi4需要手动启用插件
24
+
25
+ ```typescript
26
+ import { defineConfig } from "umi";
27
+
28
+ export default defineConfig({
29
+ npmClient: "yarn",
30
+ plugins: ["umi-plugin-electron-builder"],
31
+ });
32
+ ```
33
+
34
+ 配置完成之后,执行
35
+
36
+ ```shell
37
+ $ yarn postinstall
38
+ ```
39
+
40
+ 执行以下指令,生成主进程文件 src/main/index.ts
41
+
42
+ ```shell
43
+ $ yarn electron:init
44
+ ```
45
+
46
+ 默认安装最新版本的 electron
47
+
48
+ 自动在 package.json 增加以下配置,使用@umijs/max时,请将以下命令中的umi修改为max
49
+
50
+ ```json5
51
+ {
52
+ scripts: {
53
+ 'rebuild-deps': 'electron-builder install-app-deps',
54
+ 'electron:init': 'umi electron init',
55
+ 'electron:dev': 'umi dev electron',
56
+ 'electron:build:win': 'umi build electron --win',
57
+ 'electron:build:mac': 'umi build electron --mac',
58
+ 'electron:build:linux': 'umi build electron --linux',
59
+ },
60
+ //这里需要修改成你自己的应用名称
61
+ name: 'electron_builder_app',
62
+ version: '0.0.1',
63
+ }
64
+ ```
65
+
66
+ ### Electron 版本降级
67
+
68
+ 你可以手动将 package.json 中的 electron 修改至低版本,插件与 electron 版本无关
69
+
70
+ ### 开发
71
+
72
+ ```
73
+ $ yarn electron:dev
74
+ ```
75
+
76
+ ### 调试主进程(VS Code)
77
+
78
+ ```json5
79
+ {
80
+ "version": "0.2.0",
81
+ "configurations": [
82
+ {
83
+ "type": "node-terminal",
84
+ "request": "launch",
85
+ "name": "debug electron:dev",
86
+ "command": "yarn electron:dev",
87
+ "skipFiles": [
88
+ "<node_internals>/**"
89
+ ],
90
+ "sourceMapPathOverrides": {
91
+ "webpack://main/./*": "${workspaceFolder}/src/main/*"
92
+ },
93
+ "resolveSourceMapLocations": [
94
+ "${workspaceFolder}/src/.umi/electron/**",
95
+ "${workspaceFolder}/src/renderer/.umi/electron/**"
96
+ ],
97
+ "autoAttachChildProcesses": true
98
+ }
99
+ ]
100
+ }
101
+ ```
102
+
103
+ ### 打包
104
+
105
+ 打包路径不能有中文,electron-builder不能跨平台打包,请在对应系统上打包
106
+
107
+ ```
108
+ //windows
109
+ $ umi build electron --win
110
+ //mac
111
+ $ umi build electron --mac
112
+ //linux
113
+ $ umi build electron --linux
114
+ //按平台打包
115
+ $ umi build electron --win --ia32 //32位
116
+ $ umi build electron --win --x64 //64位
117
+ $ umi build electron --win --armv7l //arm32位
118
+ $ umi build electron --win --arm64 //arm64位
119
+ ```
120
+
121
+ ### 使用 node 环境下运行的模块
122
+
123
+ 例:使用 serialport 插件
124
+
125
+ ```
126
+ $ yarn add serialport @types/serialport
127
+ ```
128
+
129
+ ### 配置 .umirc.ts
130
+
131
+ ```typescript
132
+ import Config from '@umijs/bundler-webpack/compiled/webpack-5-chain';
133
+ import { defineConfig } from 'umi';
134
+ import { InlineConfig } from 'vite';
135
+
136
+ export default defineConfig({
137
+ electronBuilder: {
138
+ //可选参数
139
+ buildType: 'vite', //webpack或vite,当编译出现问题,可尝试切换为webpack
140
+ //并行构建,默认关闭,如开启出现问题,请关闭此功能
141
+ parallelBuild: false, //并行构建,开启时主进程和渲染进程同时编译
142
+ mainSrc: 'src/main', //默认主进程目录
143
+ preloadSrc: 'src/preload', //默认preload目录,可选,不需要可删除
144
+ routerMode: 'hash', //路由 hash或memory,仅electron下有效,推荐使用hash
145
+ outputDir: 'dist_electron', //默认打包目录
146
+ externals: ['serialport'], //node原生模块配置,打包之后找不到包也需要配置在这里
147
+ rendererTarget: 'web', //构建目标electron-renderer或web,使用上下文隔离时,必须设置为web
148
+ debugPort: 5858, //主进程调试端口
149
+ //2.1.0新增
150
+ preloadEntry: {
151
+ //默认值 key为preload文件名 值为preload输出文件名
152
+ //输出文件名不能为main.js会和主进程文件名冲突
153
+ //文件名为preload目录下多文件名
154
+ //多级目录时key为xxxx/xxxx.ts
155
+ //使用时输出文件会和主进程在同一目录下 preload: path.join(__dirname, 'preload.js')
156
+ 'index.ts': 'preload.js',
157
+ },
158
+ viteConfig(config: InlineConfig, type: "main" | "preload") {
159
+ //主进程Vite配置
160
+ //配置参考 https://vitejs.dev/config/
161
+ //ConfigType分为main和preload可分别配置
162
+ },
163
+ //通过 webpack-chain 的 API 修改 webpack 配置。
164
+ mainWebpackChain(config: Config, type: "main" | "preload") {
165
+ //ConfigType分为main和preload可分别配置
166
+ // if (type === 'main') {}
167
+ // if (type === 'preload') {}
168
+ },
169
+ //2.1.10新增 开启自定义主进程日志时
170
+ logProcess(log: string, type: "normal" | "error") {
171
+ if (type === 'normal') {
172
+ console.log(log);
173
+ } else if (type === 'error') {
174
+ console.error(log);
175
+ }
176
+ },
177
+ builderOptions: {
178
+ //配置参考 https://www.electron.build/configuration/configuration
179
+ appId: 'com.test.test',
180
+ productName: '测试',
181
+ publish: [
182
+ {
183
+ provider: 'generic',
184
+ url: 'http://localhost/test',
185
+ },
186
+ ],
187
+ }, //electronBuilder参数
188
+ },
189
+ routes: [{ path: '/', component: '@/pages/index' }],
190
+ });
191
+ ```
192
+
193
+ 在 Electron10 以上使用[contextIsolation](https://www.electronjs.org/docs/tutorial/context-isolation)时 rendererTarget 需要设置成
194
+ web
195
+
196
+ builderOptions[参考 Electron Builder](https://www.electron.build/configuration/configuration)
197
+
198
+ ### 已知问题
199
+
200
+ esbuild 暂不支持 typescript decorator metadata
201
+
202
+ Vite 与 typeorm 冲突,typeorm 在主进程无法使用
203
+
204
+ 相关 Issue https://github.com/evanw/esbuild/issues/257
@@ -0,0 +1,168 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/compile/index.ts
30
+ var compile_exports = {};
31
+ __export(compile_exports, {
32
+ runBuild: () => runBuild,
33
+ runDev: () => runDev
34
+ });
35
+ module.exports = __toCommonJS(compile_exports);
36
+ var import_utils = require("@umijs/utils");
37
+ var import_child_process = require("child_process");
38
+ var import_path = __toESM(require("path"));
39
+ var import_vite = require("vite");
40
+ var import_utils2 = require("../utils");
41
+ var import_vite2 = require("./vite");
42
+ var import_webpack = require("./webpack");
43
+ var TIMEOUT = 500;
44
+ var buildMain = (api) => {
45
+ const { buildType } = api.config.electronBuilder;
46
+ if (buildType === "webpack") {
47
+ return (0, import_webpack.build)((0, import_webpack.getMainWebpackConfig)(api));
48
+ } else {
49
+ return (0, import_vite.build)((0, import_vite2.getMainViteConfig)(api));
50
+ }
51
+ };
52
+ var buildPreload = (api) => {
53
+ const { preloadEntry, buildType } = api.config.electronBuilder;
54
+ if (import_utils.fsExtra.pathExistsSync((0, import_utils2.getPreloadSrc)(api))) {
55
+ const tasks = [];
56
+ if (buildType === "webpack") {
57
+ for (let inputFileName in preloadEntry) {
58
+ tasks.push(
59
+ (0, import_webpack.build)(
60
+ (0, import_webpack.getPreloadWebpackConfig)(
61
+ api,
62
+ inputFileName,
63
+ preloadEntry[inputFileName]
64
+ )
65
+ )
66
+ );
67
+ }
68
+ } else {
69
+ for (let inputFileName in preloadEntry) {
70
+ tasks.push(
71
+ (0, import_vite.build)(
72
+ (0, import_vite2.getPreloadViteConfig)(
73
+ api,
74
+ inputFileName,
75
+ preloadEntry[inputFileName]
76
+ )
77
+ )
78
+ );
79
+ }
80
+ }
81
+ return Promise.all(tasks);
82
+ }
83
+ return Promise.resolve();
84
+ };
85
+ var runDev = async (api) => {
86
+ const { logProcess, debugPort, parallelBuild } = api.config.electronBuilder;
87
+ const electronPath = require(import_path.default.join((0, import_utils2.getNodeModulesPath)(), "electron"));
88
+ let spawnProcess = null;
89
+ const runMain = (0, import_utils2.debounce)(() => {
90
+ if (spawnProcess !== null) {
91
+ spawnProcess.kill("SIGKILL");
92
+ spawnProcess = null;
93
+ }
94
+ spawnProcess = (0, import_child_process.spawn)(String(electronPath), [
95
+ `--inspect=${debugPort}`,
96
+ import_path.default.join((0, import_utils2.getDevBuildDir)(api), "main.js")
97
+ ]);
98
+ spawnProcess.stdout.on("data", (data) => {
99
+ const log = (0, import_utils2.filterText)(data.toString());
100
+ if (log) {
101
+ logProcess(log, "normal");
102
+ }
103
+ });
104
+ spawnProcess.stderr.on("data", (data) => {
105
+ const log = (0, import_utils2.filterText)(data.toString());
106
+ if (log) {
107
+ logProcess(log, "error");
108
+ }
109
+ });
110
+ spawnProcess.on("close", (code, signal) => {
111
+ if (signal != "SIGKILL") {
112
+ process.exit(-1);
113
+ }
114
+ });
115
+ return spawnProcess;
116
+ }, TIMEOUT);
117
+ const buildMainDebounced = (0, import_utils2.debounce)(() => buildMain(api), TIMEOUT);
118
+ const buildPreloadDebounced = (0, import_utils2.debounce)(() => buildPreload(api), TIMEOUT);
119
+ const runPreload = (0, import_utils2.debounce)(() => {
120
+ }, TIMEOUT);
121
+ if (!parallelBuild) {
122
+ await Promise.all([buildMain(api), buildPreload(api)]);
123
+ }
124
+ const watcher = import_utils.chokidar.watch(
125
+ [
126
+ `${(0, import_utils2.getMainSrc)(api)}/**`,
127
+ `${(0, import_utils2.getPreloadSrc)(api)}/**`,
128
+ `${(0, import_utils2.getDevBuildDir)(api)}/**`
129
+ ],
130
+ { ignoreInitial: true }
131
+ );
132
+ watcher.on("unlink", (path2) => {
133
+ if (spawnProcess !== null && path2.includes((0, import_utils2.getDevBuildDir)(api))) {
134
+ spawnProcess.kill("SIGINT");
135
+ spawnProcess = null;
136
+ }
137
+ }).on("add", (path2) => {
138
+ if (path2.includes((0, import_utils2.getDevBuildDir)(api))) {
139
+ return runMain();
140
+ }
141
+ if (spawnProcess !== void 0 && path2.includes("preload.js")) {
142
+ return runPreload();
143
+ }
144
+ }).on("change", (path2) => {
145
+ if (path2.includes((0, import_utils2.getMainSrc)(api))) {
146
+ return buildMainDebounced();
147
+ }
148
+ if (path2.includes("main.js")) {
149
+ return runMain();
150
+ }
151
+ if (path2.includes((0, import_utils2.getPreloadSrc)(api))) {
152
+ return buildPreloadDebounced();
153
+ }
154
+ if (path2.includes("preload.js")) {
155
+ return runPreload();
156
+ }
157
+ });
158
+ await runMain();
159
+ };
160
+ var runBuild = async (api) => {
161
+ await buildMain(api);
162
+ await buildPreload(api);
163
+ };
164
+ // Annotate the CommonJS export names for ESM import in node:
165
+ 0 && (module.exports = {
166
+ runBuild,
167
+ runDev
168
+ });
@@ -0,0 +1,111 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/compile/vite/index.ts
30
+ var vite_exports = {};
31
+ __export(vite_exports, {
32
+ getMainViteConfig: () => getMainViteConfig,
33
+ getPreloadViteConfig: () => getPreloadViteConfig
34
+ });
35
+ module.exports = __toCommonJS(vite_exports);
36
+ var import_external_packages = __toESM(require("../../external-packages.config"));
37
+ var path = __toESM(require("path"));
38
+ var import_utils = require("../../utils");
39
+ function getMainViteConfig(api) {
40
+ const mode = api.env || "development";
41
+ const { externals, viteConfig } = api.config.electronBuilder;
42
+ const external = [...import_external_packages.default, ...externals];
43
+ const mainConfig = {
44
+ mode,
45
+ resolve: {
46
+ alias: {
47
+ "@/common": path.join(process.cwd(), "src/common"),
48
+ "@": (0, import_utils.getMainSrc)(api)
49
+ }
50
+ },
51
+ root: (0, import_utils.getMainSrc)(api),
52
+ build: {
53
+ sourcemap: mode === "development" ? "inline" : false,
54
+ outDir: mode === "development" ? (0, import_utils.getDevBuildDir)(api) : (0, import_utils.getBuildDir)(api),
55
+ assetsDir: ".",
56
+ minify: mode !== "development",
57
+ lib: {
58
+ entry: "index.ts",
59
+ formats: ["cjs"]
60
+ },
61
+ rollupOptions: {
62
+ external,
63
+ output: {
64
+ entryFileNames: "main.js"
65
+ }
66
+ },
67
+ emptyOutDir: false
68
+ }
69
+ };
70
+ viteConfig(mainConfig, "main");
71
+ return mainConfig;
72
+ }
73
+ function getPreloadViteConfig(api, inputFileName, outputFileName) {
74
+ const mode = api.env || "development";
75
+ const { externals, viteConfig } = api.config.electronBuilder;
76
+ const external = [...import_external_packages.default, ...externals];
77
+ const preloadConfig = {
78
+ mode,
79
+ resolve: {
80
+ alias: {
81
+ "@/common": path.join(process.cwd(), "src/common"),
82
+ "@": (0, import_utils.getPreloadSrc)(api)
83
+ }
84
+ },
85
+ root: (0, import_utils.getPreloadSrc)(api),
86
+ build: {
87
+ sourcemap: mode === "development" ? "inline" : false,
88
+ outDir: mode === "development" ? (0, import_utils.getDevBuildDir)(api) : (0, import_utils.getBuildDir)(api),
89
+ assetsDir: ".",
90
+ minify: mode !== "development",
91
+ lib: {
92
+ entry: inputFileName,
93
+ formats: ["cjs"]
94
+ },
95
+ rollupOptions: {
96
+ external,
97
+ output: {
98
+ entryFileNames: outputFileName
99
+ }
100
+ },
101
+ emptyOutDir: false
102
+ }
103
+ };
104
+ viteConfig(preloadConfig, "preload");
105
+ return preloadConfig;
106
+ }
107
+ // Annotate the CommonJS export names for ESM import in node:
108
+ 0 && (module.exports = {
109
+ getMainViteConfig,
110
+ getPreloadViteConfig
111
+ });
@@ -0,0 +1,101 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/compile/webpack/index.ts
30
+ var webpack_exports = {};
31
+ __export(webpack_exports, {
32
+ build: () => build,
33
+ getMainWebpackConfig: () => getMainWebpackConfig,
34
+ getPreloadWebpackConfig: () => getPreloadWebpackConfig
35
+ });
36
+ module.exports = __toCommonJS(webpack_exports);
37
+ var import_path = __toESM(require("path"));
38
+ var import_webpack = __toESM(require("@umijs/bundler-webpack/compiled/webpack"));
39
+ var import_webpack_5_chain = __toESM(require("@umijs/bundler-webpack/compiled/webpack-5-chain"));
40
+ var import_external_packages = __toESM(require("../../external-packages.config"));
41
+ var import_utils = require("../../utils");
42
+ function getBaseWebpackConfig(api) {
43
+ const mode = api.env === "development" ? "development" : "production";
44
+ const { externals } = api.config.electronBuilder;
45
+ const external = [...import_external_packages.default, ...externals];
46
+ const config = new import_webpack_5_chain.default();
47
+ config.mode(mode);
48
+ config.node.set("__filename", false).set("__dirname", false);
49
+ config.devtool(mode === "development" ? "inline-source-map" : false);
50
+ config.resolve.extensions.add(".ts").add(".js").add(".node");
51
+ config.module.rule("ts").exclude.add(/node_modules/);
52
+ config.module.rule("ts").test(/\.ts?$/).use("ts").loader("ts-loader").options({ transpileOnly: true });
53
+ config.resolve.alias.set("@/common", import_path.default.join(process.cwd(), "src/common"));
54
+ config.externals(external);
55
+ config.output.path(
56
+ mode === "development" ? (0, import_utils.getDevBuildDir)(api) : (0, import_utils.getBuildDir)(api)
57
+ );
58
+ return config;
59
+ }
60
+ function getMainWebpackConfig(api) {
61
+ const { mainWebpackChain } = api.config.electronBuilder;
62
+ const config = getBaseWebpackConfig(api);
63
+ config.resolve.alias.set("@", (0, import_utils.getMainSrc)(api));
64
+ config.context((0, import_utils.getMainSrc)(api));
65
+ config.entry("main").add("./index.ts");
66
+ config.output.filename("main.js");
67
+ config.target("electron-main");
68
+ config.output.library("main").libraryTarget("commonjs2");
69
+ mainWebpackChain(config, "main");
70
+ return config.toConfig();
71
+ }
72
+ function getPreloadWebpackConfig(api, inputFileName, outputFileName) {
73
+ const { mainWebpackChain } = api.config.electronBuilder;
74
+ const config = getBaseWebpackConfig(api);
75
+ config.resolve.alias.set("@", (0, import_utils.getPreloadSrc)(api));
76
+ config.context((0, import_utils.getPreloadSrc)(api));
77
+ config.entry("preload").add(`./${inputFileName}`);
78
+ config.output.filename(outputFileName);
79
+ config.target("electron-renderer");
80
+ config.output.library("preload").libraryTarget("commonjs2");
81
+ mainWebpackChain(config, "preload");
82
+ return config.toConfig();
83
+ }
84
+ var build = async (config) => {
85
+ return await new Promise((resolve, reject) => {
86
+ const compiler = (0, import_webpack.default)(config);
87
+ compiler.run((err) => {
88
+ if (err) {
89
+ reject(err);
90
+ } else {
91
+ resolve();
92
+ }
93
+ });
94
+ });
95
+ };
96
+ // Annotate the CommonJS export names for ESM import in node:
97
+ 0 && (module.exports = {
98
+ build,
99
+ getMainWebpackConfig,
100
+ getPreloadWebpackConfig
101
+ });
@@ -0,0 +1,76 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/external-packages.config.ts
20
+ var external_packages_config_exports = {};
21
+ __export(external_packages_config_exports, {
22
+ builtins: () => builtins,
23
+ default: () => external_packages_config_default,
24
+ external: () => external,
25
+ libExternal: () => libExternal
26
+ });
27
+ module.exports = __toCommonJS(external_packages_config_exports);
28
+ var external = ["electron", "electron-updater"];
29
+ var builtins = [
30
+ "assert",
31
+ "async_hooks",
32
+ "buffer",
33
+ "child_process",
34
+ "cluster",
35
+ "console",
36
+ "constants",
37
+ "crypto",
38
+ "dgram",
39
+ "dns",
40
+ "domain",
41
+ "events",
42
+ "fs",
43
+ "http",
44
+ "http2",
45
+ "https",
46
+ "inspector",
47
+ "module",
48
+ "net",
49
+ "os",
50
+ "path",
51
+ "perf_hooks",
52
+ "process",
53
+ "punycode",
54
+ "querystring",
55
+ "readline",
56
+ "repl",
57
+ "stream",
58
+ "string_decoder",
59
+ "timers",
60
+ "tls",
61
+ "trace_events",
62
+ "tty",
63
+ "url",
64
+ "util",
65
+ "v8",
66
+ "vm",
67
+ "zlib"
68
+ ];
69
+ var libExternal = ["webpack", "glob"];
70
+ var external_packages_config_default = [...builtins, ...external, ...libExternal];
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ builtins,
74
+ external,
75
+ libExternal
76
+ });
package/lib/index.js ADDED
@@ -0,0 +1,254 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ default: () => src_default
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+ var import_utils = require("@umijs/utils");
36
+ var path = __toESM(require("path"));
37
+ var import_yargs = __toESM(require("yargs"));
38
+ var import_compile = require("./compile");
39
+ var import_external_packages = __toESM(require("./external-packages.config"));
40
+ var import_setup = __toESM(require("./setup"));
41
+ var import_utils2 = require("./utils");
42
+ var defaultConfig = {
43
+ buildType: "vite",
44
+ parallelBuild: false,
45
+ mainSrc: "src/main",
46
+ preloadSrc: "src/preload",
47
+ builderOptions: {},
48
+ externals: [],
49
+ outputDir: "dist_electron",
50
+ routerMode: "hash",
51
+ rendererTarget: "web",
52
+ debugPort: 5858,
53
+ preloadEntry: {
54
+ "index.ts": "preload.js"
55
+ },
56
+ viteConfig: () => {
57
+ },
58
+ mainWebpackChain: () => {
59
+ },
60
+ logProcess: (log, type) => {
61
+ if (type === "normal") {
62
+ (0, import_utils2.logProcess)("Main", log, import_utils.chalk.blue);
63
+ } else if (type === "error") {
64
+ (0, import_utils2.logProcess)("Main", log, import_utils.chalk.red);
65
+ }
66
+ }
67
+ };
68
+ function src_default(api) {
69
+ var _a;
70
+ (0, import_setup.default)(api);
71
+ api.describe({
72
+ key: "electronBuilder",
73
+ config: {
74
+ default: defaultConfig,
75
+ schema({ zod }) {
76
+ return zod.object({
77
+ buildType: zod.enum(["vite", "webpack"]).optional(),
78
+ parallelBuild: zod.boolean().optional(),
79
+ mainSrc: zod.string().optional(),
80
+ preloadSrc: zod.string().optional(),
81
+ outputDir: zod.string().optional(),
82
+ externals: zod.string().array().optional(),
83
+ builderOptions: zod.record(zod.string(), zod.any()).optional(),
84
+ routerMode: zod.enum(["hash", "memory", "browser"]).optional(),
85
+ rendererTarget: zod.enum(["electron-renderer", "web"]).optional(),
86
+ debugPort: zod.number().optional(),
87
+ preloadEntry: zod.record(zod.string(), zod.string()).optional(),
88
+ viteConfig: zod.function().args(zod.any(), zod.enum(["main", "preload"])).returns(zod.void()).optional(),
89
+ mainWebpackChain: zod.function().args(zod.any(), zod.enum(["main", "preload"])).returns(zod.void()).optional(),
90
+ logProcess: zod.function().args(zod.string(), zod.enum(["normal", "error"])).returns(zod.void()).optional()
91
+ });
92
+ }
93
+ }
94
+ });
95
+ api.registerCommand({
96
+ name: "electron",
97
+ fn({ args }) {
98
+ const arg = args._[0];
99
+ if (arg === "init") {
100
+ copyMainProcess();
101
+ }
102
+ }
103
+ });
104
+ const isElectron = ((_a = api.args) == null ? void 0 : _a._[0]) === "electron";
105
+ if (!isElectron) {
106
+ return;
107
+ }
108
+ api.modifyConfig((oldConfig) => {
109
+ const config = import_utils.lodash.merge({ electronBuilder: defaultConfig }, oldConfig);
110
+ const { outputDir, externals, routerMode } = config.electronBuilder;
111
+ config.outputPath = process.env.APP_ROOT ? path.join("../..", outputDir, "bundled") : path.join(outputDir, "bundled");
112
+ config.alias = config.alias || {};
113
+ config.alias["@/common"] = path.join(process.cwd(), "src/common");
114
+ config.history = config.history || {
115
+ type: routerMode
116
+ };
117
+ config.history.type = routerMode;
118
+ const configExternals = {
119
+ electron: `require('electron')`
120
+ };
121
+ if (externals.length > 0) {
122
+ for (const moduleName of externals) {
123
+ configExternals[moduleName] = `require('${moduleName}')`;
124
+ }
125
+ }
126
+ config.externals = { ...configExternals, ...config.externals };
127
+ return config;
128
+ });
129
+ api.chainWebpack((config) => {
130
+ const { rendererTarget } = api.config.electronBuilder;
131
+ config.target(rendererTarget);
132
+ if (process.env.PROGRESS !== "none") {
133
+ }
134
+ return config;
135
+ });
136
+ api.onStart(() => {
137
+ const { parallelBuild } = api.config.electronBuilder;
138
+ if (parallelBuild) {
139
+ (0, import_compile.runBuild)(api).catch((error) => {
140
+ console.error(error);
141
+ });
142
+ }
143
+ });
144
+ api.onDevCompileDone(({ isFirstCompile }) => {
145
+ if (isFirstCompile) {
146
+ (0, import_compile.runDev)(api).catch((error) => {
147
+ console.error(error);
148
+ });
149
+ }
150
+ });
151
+ api.onBuildComplete(({ err }) => {
152
+ const { parallelBuild } = api.config.electronBuilder;
153
+ if (err == null) {
154
+ if (parallelBuild) {
155
+ buildDist();
156
+ } else {
157
+ (0, import_compile.runBuild)(api).then(buildDist).catch((error) => {
158
+ console.error(error);
159
+ });
160
+ }
161
+ }
162
+ });
163
+ function buildDist() {
164
+ var _a2;
165
+ const { builderOptions, externals } = api.config.electronBuilder;
166
+ const absOutputDir = (0, import_utils2.getAbsOutputDir)(api);
167
+ const buildPkg = (0, import_utils2.getRootPkg)();
168
+ buildPkg.main = "main.js";
169
+ delete buildPkg.scripts;
170
+ delete buildPkg.devDependencies;
171
+ Object.keys(buildPkg.dependencies).forEach((dependency) => {
172
+ if (!externals.includes(dependency) && !import_external_packages.default.includes(dependency)) {
173
+ delete buildPkg.dependencies[dependency];
174
+ }
175
+ });
176
+ externals.forEach((external) => {
177
+ var _a3;
178
+ if (!buildPkg.dependencies[external]) {
179
+ buildPkg.dependencies[external] = (_a3 = require(path.join(
180
+ process.cwd(),
181
+ "node_modules",
182
+ external,
183
+ "package.json"
184
+ ))) == null ? void 0 : _a3.version;
185
+ }
186
+ });
187
+ const buildDependencies = [];
188
+ for (const dep of buildDependencies) {
189
+ const depPackageJsonPath = path.join(
190
+ (0, import_utils2.getNodeModulesPath)(),
191
+ dep,
192
+ "package.json"
193
+ );
194
+ if (import_utils.fsExtra.existsSync(depPackageJsonPath)) {
195
+ buildPkg.dependencies[dep] = require(depPackageJsonPath).version;
196
+ } else {
197
+ buildPkg.dependencies[dep] = (_a2 = require(path.join(
198
+ process.cwd(),
199
+ "node_modules",
200
+ dep,
201
+ "package.json"
202
+ ))) == null ? void 0 : _a2.version;
203
+ }
204
+ }
205
+ const buildDir = (0, import_utils2.getBuildDir)(api);
206
+ import_utils.fsExtra.copySync(buildDir, (0, import_utils2.getBundledDir)(api), { overwrite: true });
207
+ import_utils.fsExtra.rmSync(buildDir, { recursive: true, force: true });
208
+ import_utils.fsExtra.ensureDirSync(`${absOutputDir}/bundled/node_modules`);
209
+ import_utils.fsExtra.writeFileSync(
210
+ `${absOutputDir}/bundled/package.json`,
211
+ JSON.stringify(buildPkg, null, 2)
212
+ );
213
+ const defaultBuildConfig = {
214
+ directories: {
215
+ output: absOutputDir,
216
+ app: `${absOutputDir}/bundled`
217
+ },
218
+ files: ["**"],
219
+ extends: null
220
+ };
221
+ api.logger.info("build electron");
222
+ const { configureBuildCommand } = require("electron-builder/out/builder");
223
+ const builderArgs = (0, import_yargs.default)().command(["build", "*"], "Build", configureBuildCommand).parse(process.argv);
224
+ require("electron-builder").build(
225
+ import_utils.lodash.merge({
226
+ config: import_utils.lodash.merge(defaultBuildConfig, builderOptions),
227
+ ...builderArgs
228
+ })
229
+ ).then(() => {
230
+ api.logger.info("build electron success");
231
+ process.exit();
232
+ });
233
+ }
234
+ function copyMainProcess() {
235
+ const mainSrc = (0, import_utils2.getMainSrc)(api);
236
+ if (!import_utils.fsExtra.pathExistsSync(mainSrc)) {
237
+ import_utils.fsExtra.copySync(
238
+ path.join(__dirname, "..", "template", "main"),
239
+ mainSrc,
240
+ {
241
+ overwrite: true
242
+ }
243
+ );
244
+ }
245
+ const preloadSrc = (0, import_utils2.getPreloadSrc)(api);
246
+ if (!import_utils.fsExtra.pathExistsSync(preloadSrc)) {
247
+ import_utils.fsExtra.copySync(
248
+ path.join(__dirname, "..", "template", "preload"),
249
+ preloadSrc,
250
+ { overwrite: true }
251
+ );
252
+ }
253
+ }
254
+ }
package/lib/setup.js ADDED
@@ -0,0 +1,105 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/setup.ts
30
+ var setup_exports = {};
31
+ __export(setup_exports, {
32
+ default: () => setup_default
33
+ });
34
+ module.exports = __toCommonJS(setup_exports);
35
+ var import_utils = require("@umijs/utils");
36
+ var import_path = __toESM(require("path"));
37
+ var import_utils2 = require("./utils");
38
+ var setup_default = (api) => {
39
+ (0, import_utils2.setNpmClient)(api.userConfig.npmClient);
40
+ const nodeModulesPath = (0, import_utils2.getNodeModulesPath)();
41
+ let rootPkg = (0, import_utils2.getRootPkg)();
42
+ const requiredDependencies = ["electron", "electron-builder"];
43
+ const installDependencies = [];
44
+ for (const dep of requiredDependencies) {
45
+ if (rootPkg.devDependencies[dep] == null) {
46
+ installDependencies.push(dep);
47
+ }
48
+ }
49
+ if (installDependencies.length > 0) {
50
+ (0, import_utils2.installRely)(installDependencies.join(" "));
51
+ }
52
+ rootPkg = (0, import_utils2.getRootPkg)();
53
+ const electronPackageJson = import_utils.fsExtra.readJSONSync(
54
+ import_path.default.join(nodeModulesPath, "electron", "package.json")
55
+ );
56
+ if (electronPackageJson.dependencies["@types/node"] !== rootPkg.devDependencies["@types/node"]) {
57
+ const electronTypesNodeVersion = electronPackageJson.dependencies["@types/node"];
58
+ (0, import_utils2.installRely)(`@types/node@${electronTypesNodeVersion}`);
59
+ }
60
+ rootPkg = (0, import_utils2.getRootPkg)();
61
+ let isUpdateRootPkg = false;
62
+ if (rootPkg.name == null) {
63
+ rootPkg.name = "electron_builder_app";
64
+ isUpdateRootPkg = true;
65
+ }
66
+ if (rootPkg.version == null) {
67
+ rootPkg.version = "0.0.1";
68
+ isUpdateRootPkg = true;
69
+ }
70
+ if (rootPkg.scripts["rebuild-deps"] == null) {
71
+ rootPkg.scripts["rebuild-deps"] = "electron-builder install-app-deps";
72
+ isUpdateRootPkg = true;
73
+ }
74
+ if (rootPkg.scripts["electron:init"] == null) {
75
+ rootPkg.scripts["electron:init"] = "umi electron init";
76
+ isUpdateRootPkg = true;
77
+ }
78
+ if (rootPkg.scripts["electron:dev"] == null) {
79
+ rootPkg.scripts["electron:dev"] = "umi dev electron";
80
+ isUpdateRootPkg = true;
81
+ }
82
+ if (rootPkg.scripts["electron:dir"] == null) {
83
+ rootPkg.scripts["electron:dir"] = "umi build electron --dir";
84
+ isUpdateRootPkg = true;
85
+ }
86
+ if (rootPkg.scripts["electron:build:win"] == null) {
87
+ rootPkg.scripts["electron:build:win"] = "umi build electron --win";
88
+ isUpdateRootPkg = true;
89
+ }
90
+ if (rootPkg.scripts["electron:build:mac"] == null) {
91
+ rootPkg.scripts["electron:build:mac"] = "umi build electron --mac";
92
+ isUpdateRootPkg = true;
93
+ }
94
+ if (rootPkg.scripts["electron:build:linux"] == null) {
95
+ rootPkg.scripts["electron:build:linux"] = "umi build electron --linux";
96
+ isUpdateRootPkg = true;
97
+ }
98
+ if (isUpdateRootPkg) {
99
+ api.logger.info("update package.json");
100
+ import_utils.fsExtra.writeFileSync(
101
+ import_path.default.join(process.cwd(), "package.json"),
102
+ JSON.stringify(rootPkg, null, 2)
103
+ );
104
+ }
105
+ };
@@ -0,0 +1,17 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+
15
+ // src/types/index.ts
16
+ var types_exports = {};
17
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,174 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/utils/index.ts
30
+ var utils_exports = {};
31
+ __export(utils_exports, {
32
+ debounce: () => debounce,
33
+ filterText: () => filterText,
34
+ getAbsOutputDir: () => getAbsOutputDir,
35
+ getBuildDir: () => getBuildDir,
36
+ getBundledDir: () => getBundledDir,
37
+ getDevBuildDir: () => getDevBuildDir,
38
+ getMainSrc: () => getMainSrc,
39
+ getNodeModulesPath: () => getNodeModulesPath,
40
+ getPreloadSrc: () => getPreloadSrc,
41
+ getRootPkg: () => getRootPkg,
42
+ installRely: () => installRely,
43
+ logError: () => logError,
44
+ logProcess: () => logProcess,
45
+ setNpmClient: () => setNpmClient
46
+ });
47
+ module.exports = __toCommonJS(utils_exports);
48
+ var import_utils = require("@umijs/utils");
49
+ var import_path = __toESM(require("path"));
50
+ var npmClient = "pnpm";
51
+ function setNpmClient(_npmClient) {
52
+ if (_npmClient != null && _npmClient != "") {
53
+ npmClient = _npmClient;
54
+ }
55
+ }
56
+ function debounce(f, ms) {
57
+ let isCoolDown = false;
58
+ return () => {
59
+ if (isCoolDown)
60
+ return;
61
+ f();
62
+ isCoolDown = true;
63
+ setTimeout(() => isCoolDown = false, ms);
64
+ };
65
+ }
66
+ function installRely(pkgName) {
67
+ const commandOpts = {
68
+ cwd: process.cwd(),
69
+ cleanup: true,
70
+ stdin: "inherit",
71
+ stdout: "inherit",
72
+ stderr: "inherit",
73
+ env: {
74
+ FORCE_COLOR: "true"
75
+ }
76
+ };
77
+ switch (npmClient) {
78
+ case "pnpm":
79
+ import_utils.execa.execaCommandSync(`pnpm i ${pkgName} --save-dev`, commandOpts);
80
+ break;
81
+ case "npm":
82
+ import_utils.execa.execaCommandSync(`npm i ${pkgName} --save-dev`, commandOpts);
83
+ break;
84
+ case "yarn":
85
+ import_utils.execa.execaCommandSync(`yarn add ${pkgName} --dev`, commandOpts);
86
+ break;
87
+ default:
88
+ import_utils.execa.execaCommandSync(`pnpm i ${pkgName} --save-dev`, commandOpts);
89
+ break;
90
+ }
91
+ }
92
+ function getRootPkg() {
93
+ const pkg = import_utils.fsExtra.readJSONSync(import_path.default.join(process.cwd(), "package.json"));
94
+ if (pkg.devDependencies == null) {
95
+ pkg.devDependencies = {};
96
+ }
97
+ return pkg;
98
+ }
99
+ function getNodeModulesPath() {
100
+ return import_path.default.join(process.cwd(), "node_modules");
101
+ }
102
+ function getMainSrc(api) {
103
+ const { mainSrc } = api.config.electronBuilder;
104
+ return import_path.default.join(process.cwd(), mainSrc);
105
+ }
106
+ function getPreloadSrc(api) {
107
+ const { preloadSrc } = api.config.electronBuilder;
108
+ return import_path.default.join(process.cwd(), preloadSrc);
109
+ }
110
+ function getDevBuildDir(api) {
111
+ return import_path.default.join(api.paths.absTmpPath, "electron");
112
+ }
113
+ function getBuildDir(api) {
114
+ return import_path.default.join(getAbsOutputDir(api), "electron");
115
+ }
116
+ function getBundledDir(api) {
117
+ return import_path.default.join(getAbsOutputDir(api), "bundled");
118
+ }
119
+ function getAbsOutputDir(api) {
120
+ const { outputDir } = api.config.electronBuilder;
121
+ return import_path.default.join(process.cwd(), outputDir);
122
+ }
123
+ function filterText(s) {
124
+ const lines = s.trim().split(/\r?\n/).filter((it) => {
125
+ if (it.includes("Couldn't set selectedTextBackgroundColor from default ()")) {
126
+ return false;
127
+ }
128
+ if (it.includes("Use NSWindow's -titlebarAppearsTransparent=YES instead.")) {
129
+ return false;
130
+ }
131
+ if (it.includes("Debugger listening on")) {
132
+ return false;
133
+ }
134
+ return !it.includes(
135
+ "Warning: This is an experimental feature and could change at any time."
136
+ ) && !it.includes("No type errors found") && !it.includes("webpack: Compiled successfully.");
137
+ });
138
+ if (lines.length === 0) {
139
+ return null;
140
+ }
141
+ return " " + lines.join(`
142
+ `) + "\n";
143
+ }
144
+ function logError(label, error) {
145
+ logProcess(label, error.stack || error.toString(), import_utils.chalk.red);
146
+ }
147
+ function logProcess(label, log, labelColor) {
148
+ const LABEL_LENGTH = 28;
149
+ if (log == null || log.length === 0 || log.trim().length === 0) {
150
+ return;
151
+ }
152
+ process.stdout.write(
153
+ labelColor.bold(
154
+ `┏ ${label} ${"-".repeat(LABEL_LENGTH - label.length - 1)}`
155
+ ) + "\n\n" + log + "\n" + labelColor.bold(`┗ ${"-".repeat(LABEL_LENGTH)}`) + "\n"
156
+ );
157
+ }
158
+ // Annotate the CommonJS export names for ESM import in node:
159
+ 0 && (module.exports = {
160
+ debounce,
161
+ filterText,
162
+ getAbsOutputDir,
163
+ getBuildDir,
164
+ getBundledDir,
165
+ getDevBuildDir,
166
+ getMainSrc,
167
+ getNodeModulesPath,
168
+ getPreloadSrc,
169
+ getRootPkg,
170
+ installRely,
171
+ logError,
172
+ logProcess,
173
+ setNpmClient
174
+ });
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "akey-umi-plugin-electron-builder",
3
+ "version": "4.0.9",
4
+ "description": "Umi plugin for electron-builder",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "dev": "father dev",
8
+ "build": "father build",
9
+ "build:deps": "father prebundle",
10
+ "prepublishOnly": "npm run build"
11
+ },
12
+ "files": [
13
+ "lib",
14
+ "template"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/BySlin/umi-plugin-electron-builder"
19
+ },
20
+ "keywords": [
21
+ "umi"
22
+ ],
23
+ "authors": [
24
+ "BySlin <2292735470@qq.com>"
25
+ ],
26
+ "license": "MIT",
27
+ "bugs": "https://github.com/BySlin/umi-plugin-electron-builder/issues",
28
+ "homepage": "https://github.com/BySlin/umi-plugin-electron-builder/tree/master#readme",
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "dependencies": {
33
+ "@umijs/bundler-webpack": "latest",
34
+ "@umijs/utils": "latest",
35
+ "electron": "latest",
36
+ "electron-builder": "latest",
37
+ "ts-loader": "latest",
38
+ "vite": "latest",
39
+ "yargs": "latest"
40
+ },
41
+ "devDependencies": {
42
+ "@types/yargs": "latest",
43
+ "father": "latest",
44
+ "prettier": "latest",
45
+ "typescript": "latest",
46
+ "umi": "latest"
47
+ }
48
+ }
@@ -0,0 +1,13 @@
1
+ import { protocol } from 'electron';
2
+ import { URL } from 'url';
3
+ import * as path from 'path';
4
+
5
+ export default (scheme: string) => {
6
+ protocol.registerFileProtocol(scheme, (request, respond) => {
7
+ let pathName = new URL(request.url).pathname;
8
+ pathName = decodeURI(pathName); // Needed in case URL contains spaces
9
+
10
+ const filePath = path.join(__dirname, pathName);
11
+ respond({ path: filePath });
12
+ });
13
+ };
@@ -0,0 +1,51 @@
1
+ import { app, BrowserWindow, protocol } from 'electron';
2
+ import * as path from 'path';
3
+ import createProtocol from './createProtocol';
4
+
5
+ const isDevelopment = process.env.NODE_ENV === 'development';
6
+ let mainWindow: BrowserWindow;
7
+
8
+ protocol.registerSchemesAsPrivileged([
9
+ {
10
+ scheme: 'app',
11
+ privileges: {
12
+ secure: true,
13
+ standard: true,
14
+ supportFetchAPI: true,
15
+ allowServiceWorkers: true,
16
+ },
17
+ },
18
+ ]);
19
+
20
+ function createWindow() {
21
+ mainWindow = new BrowserWindow({
22
+ width: 800,
23
+ height: 600,
24
+ webPreferences: {
25
+ contextIsolation: true,
26
+ preload: path.join(__dirname, 'preload.js'),
27
+ },
28
+ });
29
+ if (isDevelopment) {
30
+ mainWindow.loadURL('http://localhost:8000');
31
+ } else {
32
+ createProtocol('app');
33
+ mainWindow.loadURL('app://./index.html');
34
+ }
35
+ }
36
+
37
+ app.on('ready', async () => {
38
+ createWindow();
39
+ });
40
+
41
+ app.on('window-all-closed', () => {
42
+ if (process.platform !== 'darwin') {
43
+ app.quit();
44
+ }
45
+ });
46
+
47
+ app.on('activate', () => {
48
+ if (mainWindow === null) {
49
+ createWindow();
50
+ }
51
+ });
@@ -0,0 +1,9 @@
1
+ import { contextBridge } from 'electron';
2
+
3
+ const apiKey = 'electron';
4
+
5
+ const api: any = {
6
+ versions: process.versions,
7
+ };
8
+
9
+ contextBridge.exposeInMainWorld(apiKey, api);