BySlin / umi-plugin-electron-builder

umi的electron插件
MIT License
156 stars 25 forks source link

执行打包命令 npm run electron:build:win报错什么意思 #78

Closed junegod closed 1 year ago

junegod commented 1 year ago

fatal - AssertionError [ERR_ASSERTION]: Invalid config keys: model, initialState, layout, locale, antd, request, access at Config.validateConfig (D:\IdeaProjects\e-law\node_modules\@umijs\core\dist\config\config.js:209:31) at Config.getConfig (D:\IdeaProjects\e-law\node_modules\@umijs\core\dist\config\config.js:67:12) at Service.resolveConfig (D:\IdeaProjects\e-law\node_modules\@umijs\core\dist\service\service.js:352:55) at Service.run (D:\IdeaProjects\e-law\node_modules\@umijs\core\dist\service\service.js:276:42) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Service.run2 (D:\IdeaProjects\e-law\node_modules\umi\dist\service\service.js:65:12) at async Object.run (D:\IdeaProjects\e-law\node_modules\umi\dist\cli\cli.js:57:7) { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' } fatal - A complete log of this run can be found in: fatal - D:\IdeaProjects\e-law\node_modules.cache\logger\umi.log fatal - Consider reporting a GitHub issue on https://github.com/umijs/umi/issues

{
  "name": "frontend-nz",
  "version": "0.0.1",
  "private": true,
  "description": "An out-of-box UI solution for enterprise applications",
  "scripts": {
    "analyze": "cross-env ANALYZE=1 max build",
    "build": "max build",
    "deploy": "npm run build && npm run gh-pages",
    "dev": "npm run start:dev",
    "gh-pages": "gh-pages -d dist",
    "i18n-remove": "pro i18n-remove --locale=zh-CN --write",
    "postinstall": "max setup",
    "lint": "npm run lint:js && npm run lint:prettier && npm run tsc",
    "lint-staged": "lint-staged",
    "lint-staged:js": "eslint --ext .js,.jsx,.ts,.tsx ",
    "lint:fix": "eslint --fix --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src ",
    "lint:js": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src",
    "lint:prettier": "prettier -c --write \"src/**/*\" --end-of-line auto",
    "openapi": "max openapi",
    "playwright": "playwright install && playwright test",
    "prepare": "husky install",
    "prettier": "prettier -c --write \"src/**/*\"",
    "serve": "umi-serve",
    "start": "cross-env UMI_ENV=dev max dev",
    "start:dev": "cross-env REACT_APP_ENV=dev MOCK=none UMI_ENV=dev max dev",
    "start:no-mock": "cross-env MOCK=none UMI_ENV=dev max dev",
    "start:pre": "cross-env REACT_APP_ENV=pre UMI_ENV=dev max dev",
    "start:test": "cross-env REACT_APP_ENV=test MOCK=none UMI_ENV=dev max dev",
    "test:e2e": "node ./tests/run-tests.js",
    "tsc": "tsc --noEmit",
    "rebuild-deps": "electron-builder install-app-deps",
    "electron:init": "max electron init",
    "electron:dev": "cross-env UMI_ENV=dev max dev electron",
    "electron:build:win": "umi build electron --win",
    "electron:build:mac": "max build electron --mac",
    "electron:build:linux": "max build electron --linux"
  },
  "lint-staged": {
    "**/*.{js,jsx,ts,tsx}": "npm run lint-staged:js",
    "**/*.{js,jsx,tsx,ts,less,md,json}": [
      "prettier --write"
    ]
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 10"
  ],
  "dependencies": {
    "@ahooksjs/use-url-state": "^3.5.1",
    "@ant-design/charts": "^1.4.2",
    "@ant-design/icons": "^5.0.1",
    "@ant-design/pro-components": "^2.4.4",
    "@ant-design/pro-provider": "^2.7.0",
    "@ant-design/pro-utils": "^2.7.0",
    "@designable/core": "^1.0.0-beta.45",
    "@designable/shared": "^1.0.0-beta.45",
    "@dnd-kit/sortable": "^7.0.2",
    "@emotion/css": "^11.11.0",
    "@formily/antd-v5": "^1.0.1-rc.2",
    "@formily/core": "^2.2.21",
    "@formily/react": "^2.2.21",
    "@formily/reactive": "^2.2.21",
    "@pind/designable-core": "^2.0.0-beta.6",
    "@pind/designable-react-settings-form": "^2.0.0-beta.6",
    "@pind/designable-shared": "^2.0.0-beta.6",
    "@umijs/route-utils": "^4.0.1",
    "ahooks": "^3.7.6",
    "antd": "^5.3.3",
    "classnames": "^2.3.2",
    "dayjs": "^1.11.7",
    "dingtalk-jsapi": "^3.0.14",
    "echarts": "^5.4.2",
    "html2canvas": "^1.4.1",
    "intersection-observer": "^0.12.2",
    "jspdf": "^2.5.1",
    "lodash": "^4.17.0",
    "omit.js": "^2.0.2",
    "rc-menu": "^9.1.0",
    "rc-util": "^5.16.0",
    "react": "^17.0.0",
    "react-beautiful-dnd": "^13.1.1",
    "react-big-calendar": "^1.6.9",
    "react-cookies": "^0.1.1",
    "react-dev-inspector": "^1.7.0",
    "react-dom": "^17.0.0",
    "react-grid-layout": "^1.3.4",
    "react-helmet-async": "^1.2.0",
    "react-resizable": "^3.0.4",
    "styled-components": "^5.3.6",
    "styled-css-grid": "^1.2.1"
  },
  "devDependencies": {
    "@ant-design/compatible": "^5.1.1",
    "@ant-design/pro-cli": "^3.1.0",
    "@ant-design/pro-table": "^3.6.1",
    "@babel/parser": "^7.19.3",
    "@babel/types": "^7.19.3",
    "@playwright/test": "^1.17.0",
    "@types/classnames": "^2.3.1",
    "@types/express": "^4.17.0",
    "@types/history": "^4.7.0",
    "@types/lodash": "^4.14.0",
    "@types/node": "^16.18.36",
    "@types/react": "^17.0.0",
    "@types/react-cookies": "^0.1.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-helmet": "^6.1.0",
    "@types/react-resizable": "^3.0.3",
    "@types/styled-components": "^5.1.26",
    "@umijs/fabric": "^2.11.1",
    "@umijs/max": "^4.0.64",
    "@umijs/openapi": "^1.3.0",
    "@umijs/utils": "^4.0.34",
    "cross-env": "^7.0.0",
    "cross-port-killer": "^1.3.0",
    "detect-installer": "^1.0.0",
    "electron": "^23.3.0",
    "electron-builder": "^23.6.0",
    "electron-edge-js": "^23.0.0",
    "eslint": "^7.32.0",
    "gh-pages": "^3.2.0",
    "husky": "^7.0.4",
    "lint-staged": "^10.0.0",
    "mockjs": "^1.1.0",
    "prettier": "^2.5.0",
    "swagger-ui-dist": "^4.12.0",
    "typescript": "^4.5.0",
    "umi-plugin-electron-builder": "^4.0.4",
    "umi-presets-pro": "^1.0.1",
    "umi-serve": "^1.9.10"
  },
  "engines": {
    "node": ">=12.0.0"
  }
}
junegod commented 1 year ago

config.ts

// https://umijs.org/config/
import { defineConfig } from '@umijs/max';
import defaultSettings from './defaultSettings';
import proxy from './proxy';
import routes from './routes';

import { convertLegacyToken } from '@ant-design/compatible/lib';
import { theme } from 'antd/lib';
import { BASE_PATH } from "./constant";

const { defaultAlgorithm, defaultSeed } = theme;
const mapToken = defaultAlgorithm(defaultSeed);
const v4Token = convertLegacyToken(mapToken);

const { REACT_APP_ENV } = process.env;

export default defineConfig({
  lessLoader: {
    modifyVars: v4Token,
  },
  /**
   * @name 开启 hash 模式
   * @description 让 build 之后的产物包含 hash 后缀。通常用于增量发布和避免浏览器加载缓存。
   * @doc https://umijs.org/docs/api/config#hash
   */
  hash: true,

  /**
   * 根路径
   */
  base: BASE_PATH,

  /**
   * @name 兼容性设置
   * @description 设置 ie11 不一定完美兼容,需要检查自己使用的所有依赖
   * @doc https://umijs.org/docs/api/config#targets
   */
  // targets: {
  //   ie: 11,
  // },
  /**
   * @name 路由的配置,不在路由中引入的文件不会编译
   * @description 只支持 path,component,routes,redirect,wrappers,title 的配置
   * @doc https://umijs.org/docs/guides/routes
   */
  // umi routes: https://umijs.org/docs/routing
  routes,

  /**
   * @name 主题的配置
   * @description 虽然叫主题,但是其实只是 less 的变量设置
   * @doc antd的主题设置 https://ant.design/docs/react/customize-theme-cn
   * @doc umi 的theme 配置 https://umijs.org/docs/api/config#theme
   */
  theme: {
    // 如果不想要 configProvide 动态设置主题需要把这个设置为 default
    // 只有设置为 variable, 才能使用 configProvide 动态设置主色调
    'root-entry-name': 'variable',
  },
  /**
   * @name moment 的国际化配置
   * @description 如果对国际化没有要求,打开之后能减少js的包大小
   * @doc https://umijs.org/docs/api/config#ignoremomentlocale
   */
  ignoreMomentLocale: true,
  /**
   * @name 代理配置
   * @description 可以让你的本地服务器代理到你的服务器上,这样你就可以访问服务器的数据了
   * @see 要注意以下 代理只能在本地开发时使用,build 之后就无法使用了。
   * @doc 代理介绍 https://umijs.org/docs/guides/proxy
   * @doc 代理配置 https://umijs.org/docs/api/config#proxy
   */
  proxy: proxy[REACT_APP_ENV || 'dev'],
  /**
   * @name 快速热更新配置
   * @description 一个不错的热更新组件,更新时可以保留 state
   */
  fastRefresh: true,
  //============== 以下都是max的插件配置 ===============
  /**
   * @name 数据流插件
   * @@doc https://umijs.org/docs/max/data-flow
   */
  model: {},
  /**
   * 一个全局的初始数据流,可以用它在插件之间共享数据
   * @description 可以用来存放一些全局的数据,比如用户信息,或者一些全局的状态,全局初始状态在整个 Umi 项目的最开始创建。
   * @doc https://umijs.org/docs/max/data-flow#%E5%85%A8%E5%B1%80%E5%88%9D%E5%A7%8B%E7%8A%B6%E6%80%81
   */
  initialState: {},
  /**
   * @name layout 插件
   * @doc https://umijs.org/docs/max/layout-menu
   */
  layout: {
    locale: false,
    ...defaultSettings,
  },
  /**
   * @name 国际化插件
   * @doc https://umijs.org/docs/max/i18n
   */
  locale: {
    // default zh-CN
    default: 'zh-CN',
    antd: true,
    // default true, when it is true, will use `navigator.language` overwrite default
    baseNavigator: true,
  },
  /**
   * @name antd 插件
   * @description 内置了 babel import 插件
   * @doc https://umijs.org/docs/max/antd#antd
   */
  antd: {},
  /**
   * @name 网络请求配置
   * @description 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
   * @doc https://umijs.org/docs/max/request
   */
  request: {},
  /**
   * @name 权限插件
   * @description 基于 initialState 的权限插件,必须先打开 initialState
   * @doc https://umijs.org/docs/max/access
   */
  access: {},
  //================ pro 插件配置 =================
  presets: ['umi-presets-pro'],
  /**
   * @name openAPI 插件的配置
   * @description 基于 openapi 的规范生成serve 和mock,能减少很多样板代码
   * @doc https://pro.ant.design/zh-cn/docs/openapi/
   */
  openAPI: [
    {
      requestLibPath: "import { request } from '@umijs/max'",
      schemaPath: 'https://xxx.com/p/v2/api-docs',
      projectName: 'swagger',
    },
    // {
    //   requestLibPath: "import { request } from '@umijs/max'",
    //   // 或者使用在线的版本
    //   // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
    //   schemaPath: join(__dirname, 'oneapi.json'),
    //   mock: false,
    // },
  ],

  /**
   * 定义全局常量
   */
  define: {},
  npmClient: "pnpm",
  history: {
    type: "hash"
  },
  plugins: [ "umi-plugin-electron-builder", './config/buildConfig/BuildVersionJson.ts' ],
  electronBuilder: {
    //可选参数
    buildType: 'webpack', //webpack或vite,当编译出现问题,可尝试切换为webpack
    //并行构建,默认关闭,如开启出现问题,请关闭此功能
    parallelBuild: false, //并行构建,开启时主进程和渲染进程同时编译
    mainSrc: 'src/main', //默认主进程目录
    preloadSrc: 'src/preload', //默认preload目录,可选,不需要可删除
    routerMode: 'hash', //路由 hash或memory,仅electron下有效,推荐使用hash
    outputDir: 'dist_electron', //默认打包目录
    externals: ['serialport'], //node原生模块配置,打包之后找不到包也需要配置在这里
    rendererTarget: 'web', //构建目标electron-renderer或web,使用上下文隔离时,必须设置为web
    debugPort: 5858, //主进程调试端口
    //2.1.0新增
    preloadEntry: {
      //默认值 key为preload文件名 值为preload输出文件名
      //输出文件名不能为main.js会和主进程文件名冲突
      //文件名为preload目录下多文件名
      //多级目录时key为xxxx/xxxx.ts
      //使用时输出文件会和主进程在同一目录下 preload: path.join(__dirname, 'preload.js')
      'index.ts': 'preload.js',
    },
    //2.1.10新增 开启自定义主进程日志时
    logProcess(log: string, type: "normal" | "error") {
      if (type === 'normal') {
        console.log(log);
      } else if (type === 'error') {
        console.error(log);
      }
    },
    builderOptions: {
      //配置参考 https://www.electron.build/configuration/configuration
      appId: 'com.test.test',
      productName: '测试',
      publish: [
        {
          provider: 'generic',
          url: 'http://localhost/test',
        },
      ],
    }, //electronBuilder参数
  },
});
junegod commented 1 year ago

运行都没问题,就是打包不行

BySlin commented 1 year ago

运行都没问题,就是打包不行

把命令里的umi改成max,改成max build electron:build:win

junegod commented 1 year ago

运行都没问题,就是打包不行

把命令里的umi改成max,改成max build electron:build:win

感谢,没有max命令呢,哈哈

junegod commented 1 year ago

这个路径错误在哪配置

event - Build index.html
Error: ENOENT: no such file or directory, lstat 'D:\IdeaProjects\e-law\dist_electron\electron'
    at Object.lstatSync (node:fs:1574:3)
    at Object.lstatSync (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+@umijs+utils@4.0.71\node_modules\@umijs\utils\compiled\fs-extra\index.js:1:34279)
    at o (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+@umijs+utils@4.0.71\node_modules\@umijs\utils\compiled\fs-extra\index.js:1:19065)
    at getStatsSync (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+@umijs+utils@4.0.71\node_modules\@umijs\utils\compiled\fs-extra\index.js:1:19100)
    at Object.checkPathsSync (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+@umijs+utils@4.0.71\node_modules\@umijs\utils\compiled\fs-extra\index.js:1:20007)
    at Object.copySync (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+@umijs+utils@4.0.71\node_modules\@umijs\utils\compiled\fs-extra\index.js:1:591)
    at buildDist (D:\IdeaProjects\e-law\node_modules\.pnpm\registry.npmmirror.com+umi-plugin-electron-builder@4.0.4_@types+node@16.18.36_styled-componen_t5wbqwqc3244l222wftvxo4ndy\node_modules\umi-plugin-electron-builder\lib\index.js:206:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errno: -4058,
  syscall: 'lstat',
  code: 'ENOENT',
  path: 'D:\\IdeaProjects\\e-law\\dist_electron\\electron'
}
找不到批处理文件。
BySlin commented 1 year ago

用yarn试试,把配置文件的npmClient改成yarn 删了node_modules,再yarn装依赖,然后再打包试试

junegod commented 1 year ago

搞定了,感谢 需要的可以参考下步骤 一定要安装dev的依赖,max命令在dev里还有很多依赖

# 每次打包前都要执行,因为build会删除dev依赖
npm i  --legacy-peer-deps  --include=dev
# 记得打包改成max命令,不要用umi
npm run electron:build:win

我这里用了electron-egg-js,需要在config.ts 里配置下externals: ["electron-edge-js"]

electronBuilder: {
    externals: ["electron-edge-js"],
    builderOptions: {
      //配置参考 https://www.electron.build/configuration/configuration
      appId: 'com.test.test',
      productName: '测试',
      publish: [
        {
          provider: 'generic',
          url: 'http://localhost/test',
        },
      ],
    }, //electronBuilder参数
  }

如果遇到报错:

Error: Could not find any Visual Studio installation to use

安装visual studio installer 进行配置

c60428a7fcb11cc7f892cc5907fa042

使用

bd986e48e897c2a5aebff7fa8a94e3a

安装下

a484e9c4230b55407f67f838048324f

再打包就能成功了 感谢再次