电子封装器和gloabl电子模块

时间:2017-05-28 12:20:52

标签: electron electron-packager

我已经安装了电子和电子包装器,所有这些都处于全局模式。当我构建我的应用程序时,电子打包器搜索本地电子模块。如何迫使电子封装商使用我安装的全球电子模块?

1 个答案:

答案 0 :(得分: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”开关设置您希望的电子版本。