电子包装商和全球电子模块

electron-packager and gloabl electron module

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

简短的回答是你所描述的不是你"should"使用电子包装器的方式。通常,目的是在您正在处理的项目目录下构建一个本地包(exe 等)。例如,在 Windows 平台上构建的 electron/angular 项目可能具有以下结构:

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”开关来设置你想要的电子版本。