electron + angular2 加载供应商库

electron + angular2 loading vendor lib

我使用 angularCLI 创建了一个新的 angular2 应用程序(只是为了让您了解我的目录结构)。

运行 ng serve 将我的所有文件放在 dist 文件夹中,并且 运行 在浏览器中安装 hello world 应用程序没有问题。

我正在尝试 运行 electron 中的同一个应用程序,但它无法找到所有供应商文件(包括@angular),因为它在脚本 src 中使用了文件协议:

这个

<script src="vendor/es6-shim/es6-shim.js"></script>
<script src="vendor/reflect-metadata/Reflect.js"></script>
<script src="vendor/systemjs/dist/system.src.js"></script>
<script src="vendor/zone.js/dist/zone.js"></script>

产生这个

file:///vendor/es6-shim/es6-shim.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/reflect-metadata/Reflect.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/systemjs/dist/system.src.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/zone.js/dist/zone.js Failed to load resource: net::ERR_FILE_NOT_FOUND

如何在电子使用的 file: 协议中添加正确的路径?

我的gulpfile.js:

var gulp = require('gulp'),  
    del = require('del'),
    runSeq = require('run-sequence');

gulp.task('clean-electron', function(){  
    return del('dist/electron-package/**/*', {force: true});
});

gulp.task('copy:electron-manifest', function(){  
   return gulp.src('./package.json')
       .pipe(gulp.dest('./dist/electron-package'))
});

gulp.task('copy:electron-scripts', function(){  
    return gulp.src('./src/electron_main.js')
        .pipe(gulp.dest('./dist/electron-package'));
});

gulp.task('copy:vendor-for-electron', function() {
    return gulp.src('./dist/vendor/**/*')
        .pipe(gulp.dest('./dist/electron-package/vendor'))    
});

gulp.task('copy:spa-for-electron', function(){  
    return gulp.src(["./dist/*.*", "./dist/app/**/*"])
        .pipe(gulp.dest('dist/electron-package'));
});

gulp.task('electron', function(done){  
    return runSeq('clean-electron', ['copy:spa-for-electron', 'copy:vendor-for-electron', 'copy:electron-manifest', 'copy:electron-scripts' ], done);
});

我最接近的是这样做:

我的index.html:

<script src="vendor/es6-shim/es6-shim.js"></script>
<script src="vendor/reflect-metadata/Reflect.js"></script>
<script src="vendor/systemjs/dist/system.src.js"></script>
<script src="vendor/zone.js/dist/zone.js"></script>

<script>
 console.log("In my script tag:");
 var systemConfigPath = 'system-config.js';
 var mainPath = 'main.js';
 if (window.location.protocol == "file:"){
  require(__dirname + '/vendor/es6-shim/es6-shim.js');
  require(__dirname + '/vendor/reflect-metadata/Reflect.js');
  require(__dirname + '/vendor/systemjs/dist/system.src.js');
  require(__dirname + '/vendor/zone.js/dist/zone.js');

  systemConfigPath = __dirname + '/' + systemConfigPath; 
  mainPath = __dirname + '/' + mainPath ;
} 

System.import(systemConfigPath).then(function () {
    System.import(mainPath);
}).catch(console.error.bind(console));

但这仍然给我带来问题,因为供应商文件引用同一目录中的其他文件:

编辑:

我现在正在尝试使用 webpack 构建我的电子应用程序(没有成功)。

如果您想查看代码,我还创建了一个 github repo

好的!所以我不确定这是最好的答案,因为它仍然会产生一些愚蠢的错误,但我们开始吧...

我的 index.html 现在看起来像这样:

<body>
  <electron-angular-boilerplate-app>Loading...</electron-angular-boilerplate-app>

  <!--will give errors in electron... oh well-->
  <script src="vendor/es6-shim/es6-shim.js"></script>
  <script src="vendor/reflect-metadata/Reflect.js"></script>
  <script src="vendor/systemjs/dist/system.src.js"></script>
  <script src="vendor/zone.js/dist/zone.js"></script>

  <script>
    // if require is defined, we are on node / electron:
    if (!(typeof(require) == "undefined")){
      require('./vendor/es6-shim/es6-shim.js');
      require("./vendor/reflect-metadata/Reflect.js");
      require("./vendor/systemjs/dist/system.src.js");
      require("./vendor/zone.js/dist/zone.js");
      require("./system-config.js");
      require("./main.js");
    } else {
      System.import('system-config.js').then(function () {
        System.import('main');
      }).catch(console.error.bind(console));      
    }

  </script>
</body>

这允许我的 angular cli 应用程序到 运行 和我的电子应用程序到 运行。 <script src=... 标签仍​​然会在 electron 中产生错误,因为它无法找到它们。我还必须从 electron 中删除 System.import 行,所以希望以后不会造成任何问题。

和 运行 它,我们只需要确保应用程序已构建并且 运行 electron 在 ./dist 文件夹中:

ng build && electron ./dist

这是我的工作代码的分支:

https://github.com/jdell64/electronAngularBoilerplate/tree/so-37447020-answer

来自How should I configure the base href for Angular 2 when using Electron?的答案是改变你

 <base href="/">

 <base href="./">