Skip to content

Commit

Permalink
feat: generate script support @umijs/max, remove routerMode config
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Jul 31, 2023
1 parent 650544c commit fef28af
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/demo/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import 'umi/typings';
import './src/.umi/typings';
1 change: 1 addition & 0 deletions examples/max/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default defineConfig({
npmClient: 'pnpm',
plugins: ['@liangskyli/umijs-plugin-electron'],
electron: electron,
history: { type: 'hash' },
});
2 changes: 1 addition & 1 deletion examples/max/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
},
"name": "umi_electron_app",
"version": "0.0.1"
}
}
2 changes: 1 addition & 1 deletion examples/max/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import 'umi/typings';
import './src/.umi/typings';
1 change: 1 addition & 0 deletions packages/umijs-plugin-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/yargs": "^17.0.24",
"electron": "^25.3.2",
"electron-builder": "^24.6.3",
"umi": "^4.0.73"
Expand Down
40 changes: 5 additions & 35 deletions packages/umijs-plugin-electron/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { chalk, fsExtra, lodash } from '@umijs/utils';
import { configureBuildCommand } from 'electron-builder/out/builder';
import * as path from 'path';
import type { IApi } from 'umi';
// @ts-ignore
import { configureBuildCommand } from 'electron-builder/out/builder';
import yargs from 'yargs';
import { buildElectron } from './build-electron';
import { runBuild, runDev } from './compile';
Expand All @@ -14,7 +13,6 @@ import {
getBuildDir,
getBundledDir,
getMainSrc,
getNodeModulesPath,
getPreloadSrc,
getRootPkg,
logProcess,
Expand All @@ -28,7 +26,6 @@ const defaultConfig: ElectronConfig = {
builderOptions: {},
externals: [],
outputDir: 'dist_electron',
routerMode: 'hash',
debugPort: 5858,
preloadEntry: {
'index.ts': 'preload.js',
Expand Down Expand Up @@ -56,7 +53,6 @@ export default function (api: IApi) {
outputDir: zod.string().optional(),
externals: zod.string().array().optional(),
builderOptions: zod.record(zod.string(), zod.any()).optional(),
routerMode: zod.enum(['hash', 'memory', 'browser']).optional(),
debugPort: zod.number().optional(),
preloadEntry: zod.record(zod.string(), zod.string()).optional(),
viteConfig: zod
Expand Down Expand Up @@ -120,17 +116,11 @@ export default function (api: IApi) {
api.modifyConfig((oldConfig) => {
const config = lodash.merge({ electron: defaultConfig }, oldConfig);

const { outputDir, externals, routerMode } =
config.electron as ElectronConfig;
const { outputDir, externals } = config.electron as ElectronConfig;
config.outputPath = path.join(outputDir, 'bundled');
config.alias = config.alias || {};
config.alias['@/common'] = path.join(process.cwd(), 'src/common');

config.history = config.history || {
type: routerMode,
};
config.history.type = routerMode;

const configExternals: any = {
electron: 'require(\'electron\')',
};
Expand Down Expand Up @@ -187,26 +177,6 @@ export default function (api: IApi) {
}
});

//处理内置依赖
const buildDependencies: string[] = [];
for (const dep of buildDependencies) {
const depPackageJsonPath = path.join(
getNodeModulesPath(),
dep,
'package.json',
);
if (fsExtra.existsSync(depPackageJsonPath)) {
buildPkg.dependencies![dep] = require(depPackageJsonPath).version;
} else {
buildPkg.dependencies![dep] = require(path.join(
process.cwd(),
'node_modules',
dep,
'package.json',
))?.version;
}
}

const buildDir = getBuildDir(api);

fsExtra.copySync(buildDir, getBundledDir(api), { overwrite: true });
Expand Down Expand Up @@ -240,15 +210,15 @@ export default function (api: IApi) {
.command(['build', '*'], 'Build', configureBuildCommand)
.parse(process.argv);
const userConfig: Parameters<typeof buildElectron>[0] = {
rawOptions: builderArgs,
rawOptions: builderArgs as any,
config: {
files: ['**'],
extends: null,
...builderOptions,
directories: {
output: absOutputDir,
app: `${absOutputDir}/bundled`,
},
files: ['**'],
extends: null,
},
};

Expand Down
26 changes: 14 additions & 12 deletions packages/umijs-plugin-electron/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { fsExtra } from '@umijs/utils';
import path from 'path';
import type { IApi } from 'umi';
import {
// getNodeModulesPath,
getRootPkg,
installDevDependencies,
setNpmClient,
} from './utils';
import { getRootPkg, installDevDependencies, setNpmClient } from './utils';

/**
* 检查环境是否满足运行,不满足则自动配置环境
Expand Down Expand Up @@ -73,39 +68,46 @@ export default (api: IApi) => {
isUpdateRootPkg = true;
}

const umiCliName = api.appData.umi.cliName;
// 复制模板到主进程目录
if (!rootPkg.scripts['electron:init']) {
rootPkg.scripts['electron:init'] = 'umi electron init';
rootPkg.scripts['electron:init'] = `${umiCliName} electron init`;
isUpdateRootPkg = true;
}

// 以开发环境启动electron
if (!rootPkg.scripts['electron:dev']) {
rootPkg.scripts['electron:dev'] = 'umi dev electron';
rootPkg.scripts['electron:dev'] = `${umiCliName} dev electron`;
isUpdateRootPkg = true;
}

// 打包成文件夹 不封装成安装包
if (!rootPkg.scripts['electron:dir']) {
rootPkg.scripts['electron:dir'] = 'umi build electron --dir';
rootPkg.scripts['electron:dir'] = `${umiCliName} build electron --dir`;
isUpdateRootPkg = true;
}

// 打包electron windows平台
if (!rootPkg.scripts['electron:build:win']) {
rootPkg.scripts['electron:build:win'] = 'umi build electron --win';
rootPkg.scripts[
'electron:build:win'
] = `${umiCliName} build electron --win`;
isUpdateRootPkg = true;
}

// 打包electron mac平台
if (!rootPkg.scripts['electron:build:mac']) {
rootPkg.scripts['electron:build:mac'] = 'umi build electron --mac';
rootPkg.scripts[
'electron:build:mac'
] = `${umiCliName} build electron --mac`;
isUpdateRootPkg = true;
}

// 打包electron linux平台
if (!rootPkg.scripts['electron:build:linux']) {
rootPkg.scripts['electron:build:linux'] = 'umi build electron --linux';
rootPkg.scripts[
'electron:build:linux'
] = `${umiCliName} build electron --linux`;
isUpdateRootPkg = true;
}

Expand Down
24 changes: 11 additions & 13 deletions packages/umijs-plugin-electron/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ export type ConfigType = 'main' | 'preload';

export type LogType = 'normal' | 'error';

export type RouterMode = 'hash' | 'memory' | 'browser';

export interface ElectronConfig {
// 主进程src目录
/** 主进程src目录 */
mainSrc: string;
// preload src目录
/** preload src目录 */
preloadSrc: string;
// node模块
/** node模块 */
externals: string[];
// 打包目录
/** 打包目录 */
outputDir: string;
// 打包参数
/** 打包参数
* see: https://www.electron.build/configuration/configuration
* */
builderOptions: Configuration;
// 路由模式
routerMode: RouterMode;
// 调试端口
/** 调试端口 */
debugPort: number;
// preload配置 key为输入文件名,值为输出文件名
/** preload配置 key为输入文件名,值为输出文件名 */
preloadEntry: { [key: string]: string };
// 主进程vite配置
/** 主进程vite配置 */
viteConfig: (config: InlineConfig, type: ConfigType) => void;
//自定义主进程输出
/** 自定义主进程输出 */
logProcess: (log: string, type: LogType) => void;
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fef28af

Please sign in to comment.