BySlin / umi-plugin-electron-builder

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

使用2.x版本报错Error: Cannot find module 'umi-plugin-electron-builder/lib/createProtocol' #73

Closed Adherentman closed 1 year ago

Adherentman commented 1 year ago

yarn electron:dev:

DONE  Compiled successfully in 2021ms                                                                                                                                                                     17:03:37

✖ Electron-main
  Compiled with some errors in 783.16ms

✔ Electron-renderer

  App running at:
  - Local:   http://localhost:8000 (copied to clipboard)
  - Network: http://172.20.2.53:8000
(node:41038) [DEP_WEBPACK_MODULE_ERRORS] DeprecationWarning: Module.errors was removed (use getErrors instead)
(Use `node --trace-deprecation ...` to show where the warning was created)
┏ Main -----------------------

  For help, see: https://nodejs.org/en/docs/inspector

┗ ----------------------------
┏ Main -----------------------

  App threw an error during load

┗ ----------------------------
┏ Main -----------------------

  Error: Cannot find module 'umi-plugin-electron-builder/lib/createProtocol'
      at /Users/xuzihao/Downloads/1mu-2b-client/src/.umi/electron/main.js:2:26280
      at /Users/xuzihao/Downloads/1mu-2b-client/src/.umi/electron/main.js:2:26395
      at /Users/xuzihao/Downloads/1mu-2b-client/src/.umi/electron/main.js:2:27017
      at Object.<anonymous> (/Users/xuzihao/Downloads/1mu-2b-client/src/.umi/electron/main.js:2:27043)
      at Module._compile (internal/modules/cjs/loader.js:1152:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:1173:10)
      at Module.load (internal/modules/cjs/loader.js:992:32)
      at Module._load (internal/modules/cjs/loader.js:885:14)
      at Function.f._load (electron/js2c/asar_bundle.js:5:12633)
      at loadApplicationPackage (/Users/xuzihao/Downloads/1mu-2b-client/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:110:16)

┗ ----------------------------

.umirc.ts

// https://umijs.org/config/
import { defineConfig } from 'umi';

import fs from 'fs';
import path from 'path';
import lessToJs from 'less-vars-to-js';

import { routes } from './src/config/routes';

const { API_HOST } = process.env;

export default defineConfig({
  define: {
    API_HOST: API_HOST,
  },
  // history: { type: 'hash' },
  // hash: true,
  ignoreMomentLocale: true,
  antd: {},
  dva: {
    hmr: true,
  },
  locale: {
    // default zh-CN
    default: 'zh-CN',
    // default true, when it is true, will use `navigator.language` overwrite default
    antd: true,
    title: true,
    baseNavigator: true,
    baseSeparator: '-',
  },
  dynamicImport: {
    loading: '@/components/PageLoading/index',
  },
  targets: {
    ie: 11,
  },
  routes,
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: lessToJs(
    fs.readFileSync(
      path.join(__dirname, './src/assets/css/antd-variables.less'),
      'utf8',
    ),
  ),
  title: false,
  proxy: {
    '/api': {
      target: 'https://1mu-test.oss-cn-hangzhou.aliyuncs.com/',
      changeOrigin: true,
      pathRewrite: { '^/api': '' },
    },
  },
  chainWebpack(memo /* ,  { webpack } */) {
    // const oneOfsMap = memo.module.rule('sass').oneOfs.values();
    // oneOfsMap.forEach(item => {
    //   item
    //     .use('sass-resources-loader')
    //     .loader('sass-resources-loader')
    //     .options({
    //       resources: './src/assets/scss/_var.scss',
    //     })
    //     .end();
    // });

    // 内置的 svg Rule 添加 exclude
    memo.module
      .rule('svg')
      .exclude.add(/iconsvg/)
      .end();

    // 添加 svg-sprite-loader Rule
    memo.module
      .rule('svg-sprite-loader')
      .test(/.svg$/)
      .include.add(/iconsvg/)
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader');

    // 添加 svgo Rule
    memo.module
      .rule('svgo')
      .test(/.svg$/)
      .include.add(/iconsvg/)
      .end()
      .use('svgo-loader')
      .loader('svgo-loader')
      .options({
        // externalConfig 配置特殊不是相对路径,起始路径是根目录
        externalConfig: './src/assets/iconsvg/svgo.yml',
      });

    /*
    // 添加 svgr Rule (yarn add @svgr/webpack --dev) (移除)
    memo.module
      .rule('svgr')
      .test(/.svg$/)
      .include.add(/iconsvg/).end()
      .use('@svgr/webpack')
      .loader(require.resolve('@svgr/webpack'));
    */
  },
  mfsu: {},
  webpack5: {},
  electronBuilder: {
    buildType: 'webpack',
    routerMode: 'memory', // 路由 只能是hash或memory
    outputDir: 'dist_electron', // 默认打包目录
    externals: [], // 不配置的无法使用
    rendererTarget: 'electron-renderer', // 构建目标electron-renderer 或 web
    builderOptions: {
      productName: 'example', // 项目名,也是生成的安装文件名,即electron-admin-antd-react.exe
      appId: 'xx.com', // 包名
      copyright: 'Copyright © xxxx', // 版权
      extends: null,
      nsis: {
        oneClick: false, // 是否一键安装
        allowElevation: true, // 允许请求提升。 如果为false,则用户必须使用提升的权限重新启动安装程序。
        allowToChangeInstallationDirectory: true, // 允许修改安装目录
        installerIcon: './build/icons/icon.ico', // 安装图标
        uninstallerIcon: './build/icons/icon.ico', // 卸载图标
        installerHeaderIcon: './build/icons/icon.ico', // 安装时头部图标
        createDesktopShortcut: true, // 创建桌面图标
        createStartMenuShortcut: true, // 创建开始菜单图标
        shortcutName: 'electron-admin-antd-react', // 图标名称
      },
      dmg: {
        // macOSdmg
        contents: [
          {
            x: 410,
            y: 150,
            type: 'link',
            path: '/Applications',
          },
          {
            x: 130,
            y: 150,
            type: 'file',
          },
        ],
      },
      mac: {
        // mac
        icon: './build/icons/mac/icon.ico',
        artifactName: '${productName}-v${version}-mac.${ext}',
      },
      win: {
        // win 相关配置
        icon: './build/icons/icon.ico',
        artifactName: '${productName}-v${version}-win32-setup.${ext}',
        target: [
          {
            target: 'nsis', // 利用nsis制作安装程序
            arch: [
              // 这个意思是打出来32 bit + 64 bit的包,但是要注意:这样打包出来的安装包体积比较大,所以建议直接打32的安装包。
              // "x64",
              'ia32',
            ],
          },
        ],
      },
      linux: {
        icon: './build/icons',
        artifactName: '${productName}-v${version}-linux.${ext}',
      },
      publish: [
        {
          provider: 'generic',
          url: '',
        },
      ],
    },
  },
});
BySlin commented 1 year ago

createProtocol.ts 把这个放入main目录

import { protocol } from 'electron';
import { URL } from 'url';
import * as path from 'path';

export default (scheme: string) => {
  protocol.registerFileProtocol(scheme, (request, respond) => {
    let pathName = new URL(request.url).pathname;
    pathName = decodeURI(pathName); // Needed in case URL contains spaces

    const filePath = path.join(__dirname, pathName);
    respond({ path: filePath });
  });
};
import createProtocol from 'umi-plugin-electron-builder/lib/createProtocol';

改成

import createProtocol from './createProtocol';
Adherentman commented 1 year ago

感谢!