2

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

4

1 回答 1

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 选项中显示的所有相同的选项。您可以在他们的在线 github 用户文档中查看完整的选项列表。在您的情况下,如果您使用的是命令行,请使用“ --electron-version”开关设置您想要的电子版本。

于 2017-10-11T16:25:30.890 回答