【问题标题】:electron-packager and gloabl electron module电子封装器和全球电子模块
【发布时间】:2017-10-28 20:43:44
【问题描述】:

我已经安装了 electron 和 electron-packager,它们都处于全局模式。当我构建我的应用程序时,电子打包程序会搜索本地电子模块。如何强制 electron-packager 使用我安装的全局电子模块?

【问题讨论】:

    标签: electron electron-packager


    【解决方案1】:

    简短的回答是,您所描述的不是您“应该”使用电子打包器的方式。通常,目的是在您正在处理的项目目录下构建本地包(exe 等)。例如,在 Windows 平台上构建的电子/角度项目可能具有以下类型的结构:

    C:.
    +---ClientSide
    ¦   +---index.html
    ¦   +---app
    ¦   ¦   +---app.component.ts
    ¦   ¦   +---app.module.ts
    ¦   ¦   +---main.ts
    ¦   ¦   +---AppContent/
    ¦   ¦   +---help/
    ¦   +---Styles
    ¦   +---test
    ¦       +---AppContent/
    +---dist/
    +---edist
    |   \---Application-win32-ia32 [*location of binary source for the install]
    +---Installer
        +---Application/
    gulpfile.js
    karma.conf.js
    main.js
    package.json
    README.md
    webpack.config.js
    

    在这种情况下,package.json 文件通常包含对这两个包的引用,如下所示:

    .. .. ..
      "devDependencies": {
        "@angular/animations": "4.4.4",
        "@angular/common": "4.4.4",
        "@angular/compiler": "4.4.4",
    .. .. ..
    .. .. ..
        "electron": "1.7.9",
        "electron-packager": "9.1.0",
    .. .. ..
    

    然后,在您的本地 gulpfile.js 中,您通常会包含一个调用来运行引用本地电子版本的打包程序。比如:

    'use strict';
    ...   ...
    var packager = require('electron-packager');
    var electronPackage = require('electron/package.json');
    var pkg = require('./package.json');
    // pull the electron version from the package.json file
    var electronVersion = electronPackage.version;
    ...   ...
    
    var opts = {
        name: pkg.name,
        platform: 'win32',
        arch: 'ia32',                           // ia32, x64 or all
        dir: './',                       // source location of app
        out: './edist/',              // destination location for app os/native binaries
        ignore: config.electronignore,          // don't include these directories in the electron app build
        icon: config.icon,
        asar: {unpackDir: config.electroncompiled}, // compress project/modules into an asar blob but don't use asar to pack the native compiled modules
        overwrite: true,
        prune: true,
        electronVersion: electronVersion ,       // Tell the packager what version of electron to build with
        appCopyright: pkg.copyright,            // copyright info
        appVersion: pkg.version,         // The version of the application we are building
        win32metadata: {                        // Windows Only config data
            CompanyName: pkg.authors,
            ProductName: pkg.name,
            FileDescription: pkg.description,
            OriginalFilename: pkg.name + '.exe'
        }
    };
    
    
    // Build the electron app
    gulp.task('build:electron', function (cb) {
    
        console.log('Launching task to package binaries for ' + opts.name + ' v' + opts['appVersion']);
    
        packager(opts, function (err, appPath) {
            console.log(' <- packagerDone() ' + err + ' ' + appPath);
            console.log(' all done!');
            cb();
        });
    });
    

    如果您不想构建与本地存在的相同版本的电子,您可以将该参数更改为您希望打包程序使用的任何电子版本。如,替换这行代码:

    // pull the electron version from the package.json file
    var electronVersion = electronPackage.version;
    

    这样的:

    // Use a specific electron version
    var electronVersion = '1.7.8';
    

    如果您要从命令行运行 electron-packager,您可以使用与我在 API 选项中显示的所有相同的选项。您可以查看选项的完整列表 in their online github user docs 。在您的情况下,如果您使用的是命令行,请使用“--electron-version”开关来设置您想要的电子版本。

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2018-04-26
      • 1970-01-01
      • 2018-05-15
      • 2016-06-11
      • 2023-03-03
      • 2018-03-15
      • 1970-01-01
      • 2019-12-27
      相关资源
      最近更新 更多