为什么`package.json`中列出的`electron-packager`包节点模块不会作为依赖?

时间:2016-11-14 16:31:13

标签: node.js electron

我一直在努力解决这个问题一周,所以我想我会联系这个神奇的SO社区寻求帮助。我有一个需要生成OS X应用程序(最终是Windows)的Electron项目。最初我们使用webpack捆绑所有内容(electron和前端代码),但是,添加了一些使用ffi的功能后,这不再是一个选项。在切换到使用gulp来转换和uglify electron代码后,该应用正在开发中。

我遇到的问题是,在使用electron-packager时,我的node_modules none 正在构建期间被捆绑。根据文档,electron-packager 应该捆绑dependencies package.json部分中列出的所有内容。我构建了一个简单的概念验证应用程序来简化事务和模块仍然没有捆绑electron代码。这个新的简化项目只是试图包含bunyanffi也失败了)但是,在构建之后,应用程序抱怨无法找到bunyan模块。有人可以帮忙吗?我错过了一些小而重要的东西吗?这是该项目的一些细节:

环境详情

  • OSX版本:10.11.6(15G1108)
  • 节点版本:5.2.0(使用nvm
  • npm版本:3.3.12

package.json

{
    "name": "build-with-native-modules",
    "version": "1.0.0",
    "description": "A test for Electron packages with native Node modules.",
    "main": "el/main.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "clean": "./node_modules/.bin/rimraf build",
      "start": "npm run clean && gulp && ./node_modules/.bin/electron ./build/dist/el/main.js",
      "electron:rebuild": "./node_modules/.bin/electron-rebuild",
      "build:osx": "gulp build:osx",
      "cinstall": "rm -Rf ./node_modules && npm cache clean && npm i && npm run electron:rebuild"
    },
    "postinstall": "install-app-deps",
    "author": "Zachary Abresch <zachary.abresch@gmail.com>",
    "license": "MIT",
    "dependencies": {
      "bunyan": "^1.8.4",
      "jquery": "^3.1.1"
    },
    "devDependencies": {
      "babel-cli": "^6.18.0",
      "babel-core": "^6.18.2",
      "babel-eslint": "^7.1.0",
      "babel-loader": "^6.2.7",
      "babel-preset-es2015": "^6.18.0",
      "babel-preset-stage-0": "^6.16.0",
      "babel-register": "^6.18.0",
      "electron": "1.4.6",
      "electron-packager": "^8.2.0",
      "electron-rebuild": "^1.3.0",
      "eslint": "^3.9.1",
      "eslint-config-airbnb": "^13.0.0",
      "eslint-plugin-import": "^2.2.0",
      "eslint-plugin-jsx-a11y": "^2.2.3",
      "eslint-plugin-react": "^6.6.0",
      "gulp": "^3.9.1",
      "gulp-babel": "^6.1.2",
      "gulp-util": "^3.0.7",
      "rimraf": "^2.5.4",
      "webpack": "^1.13.3",
      "webpack-dev-server": "^1.16.2"
    }
  }

gulpfile.babel.js

  import gulp from 'gulp';
  import babel from 'gulp-babel';
  import gutil from 'gulp-util';
  import webpack from 'webpack';
  import packager from 'electron-packager';

  import webpackConfig from './webpack.config.babel';
  import pack from './package.json';

  gulp.task('electron:babel', () => {
    gulp.src('src/el/**/*.js', { base: 'src' })
    .pipe(babel())
    .pipe(gulp.dest('build/dist'));
  });

  gulp.task('move:html', () => {
    gulp.src('src/ui/**/*.html', { base: 'src' })
    .pipe(gulp.dest('build/dist'));
  });

  gulp.task('move:package', () => {
    gulp.src('package.json', { base: './' })
      .pipe(gulp.dest('build/dist'));
  });

  gulp.task('ui:webpack', (done) => {
    const myConfig = Object.create(webpackConfig);

    webpack(myConfig, (err, stats) => {
      if (err) throw new gutil.PluginError('ui:webpack', err);
      gutil.log('[ui:webpack]', stats.toString({ colors: true }));
      done();
    });
  });

  const osxBuildOptions = {
    arch: 'x64',
    'app-copyright': '8x8, Inc.',
    'app-version': '1.0.0',
    'build-version': '1.0.0',
    dir: './build/dist',
    name: pack.name,
    out: './build/bundle',
    overwrite: true,
    version: pack.devDependencies.electron,
  };

  gulp.task('build:osx',
    ['move:package', 'electron:babel', 'ui:webpack', 'move:html'],
  (done) => {
    packager(osxBuildOptions, (err, appPath) => {
      if (err) throw new gutil.PluginError('electron-packager', err);
      gutil.log(`[build:osx] App built in: ${appPath}`);
      done();
    });
  });

  gulp.task('default', ['electron:babel', 'ui:webpack', 'move:html']);

webpack.config.babel.js

import path from 'path';

  module.exports = {
    target: 'electron',
    entry: './src/ui/renderer.js',
    output: {
      path: path.resolve(__dirname, 'build/dist/ui'),
      filename: 'ui.bundle.js',
    },
    module: {
      loaders: [
        {
          loader: 'babel-loader',
          include: [
            path.resolve(__dirname, 'src/ui'),
          ],
          test: /\.js/,
        },
      ],
    },
  };

我正在考虑尝试electron-builder,但我觉得这应该有效。如果有人有任何信息或见解我会非常感激。如果您需要任何其他信息,请在评论中告诉我。谢谢!

0 个答案:

没有答案