2

I am writing my first Electron/Angular/RequireJS single page desktop app. I started with the electron-boilerplate and it works great. However, I need to add AngularJs to the equation--the boiler plate already uses RequireJS. I've tried a number of solutions but I either get errors, or dependencies not being executed. So I tried to implement the simple example from a previous stack overflow question, "Simple requireJS with angularJS - angular is not defined". However, what I see is that the files are being parsed, but it never executes the code inside. It's almost like one or more of the dependencies are not met so they do not execute. My files are as follows--I have added some logging so I can easily see the execution path.

app.html -- The starting point that includes RequireJS.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Electron Boilerplate</title>
</head>
<body ng-app="ReqApp" ng-controller="GreetCtrl">

    <h1>{{greeting}}!</h1>
    <script data-main="require_config" src="vendor/require.js"></script>

</body>
</html>

require_config.js -- This contains the RequireJS configuration.

console.log("REQUIRE_CONFIG.JS-------------"+requirejs.version);    
requirejs.config({
    // alias libraries paths
  paths: {
    'domReady':      './vendor/requirejs-domready',
    'angular':       './node_modules/angular/angular',
    'GreetCtrl':     './js/GreetCtrl',
    'app':           './js/app'
  },
  // angular does not support AMD out of the box, put it in a shim
  shim: { 'angular': { exports: 'angular' }
  },
  // kick start application
  deps: ['./bootstrap']
});

bootstrap.js -- File from the RequireJS deps to manually bootstrap AngularJS.

console.log("BOOTSTRAP.JS1-------------");
define([
    'require',
    'angular',
    'app'
], function (require, ng, app) {
    'use strict';

    console.log("BOOTSTRAP.JS2+++++++++++++");
    require(['domReady!'], function (document) {
        console.log("BOOTSTRAP.JS+++++++++++++");
        ng.bootstrap(document, ['ReqApp']);
    });
});

./js/app.js -- This initializes the AngularJS module for the application.

console.log("APP.JS-------------"+requirejs.version);
define([
  'angular',
  'GreetCtrl'
], function (ng) {
  'use strict';

  console.log("APP.JS+++++++++++++");
  return ng.module('ReqApp', [ 'ReqApp.GreetCtrl' ]);
});

./js/GreetCtrl.js

console.log("GREETCTRL.JS1-------------");
define(['angular'], function(angular) {
    console.log("GREETCTRL.JS1+++++++++++++");
    angular.module( 'ReqApp.GreetCtrl', [])
    .controller( 'GreetCtrl', function GreetCtrl ($scope) {
         $scope.greeting = "Hello World";
    });
});

The console output from running this example is:

[16982:0917/115914:ERROR:browser_main_loop.cc(173)] Running without the SUID sandbox! See https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment for more information on developing with the sandbox on.
[17010:0917/115915:INFO:renderer_main.cc(200)] Renderer process started
[17014:0917/115915:INFO:renderer_main.cc(200)] Renderer process started
[16982:0917/115915:ERROR:browser_gpu_channel_host_factory.cc(146)] Failed to create channel.
[16982:0917/115915:INFO:CONSOLE(43)] "REQUIRE_CONFIG.JS-------------2.1.20", source: file:///home/swdev/projects/build/require_config.js (43)
[16982:0917/115915:INFO:CONSOLE(8)] "BOOTSTRAP.JS1-------------", source: file:///home/swdev/projects/build/bootstrap.js (8)

The pages output is:

{{greeting}}!

As you can see from the output it seems that only the require_config.js and bootstrap.js files are parsed but the code within the define is not executed. My questions are:

  1. Why is the bootstrap.js function never called?
  2. Why are the app.js and GreetCtrl.js files not even parsed?
  3. Since Electron uses node I have to use requirejs, do I need to remove my use of require from bootstrap.js and replace it with requirejs as well? NOTE: I have tested changing this but it did nothing for my problem.
  4. I did notice that the stack overflow example that I followed defines ng-app="ReqApp" within the HTML body definition. My limited understanding is that this should be removed since we are manually bootstrapping AngularJS. What is the correct approach for my use case (Election(node)/RequireJS/AngularJS? NOTE: I have tested by removing the definition from the body definition and it did nothing for my problem.

I have spent a few days attempting to solve this problem, so any help would be much appreciated!


Additional Attempts: Played some more to simplify part of the problem. I can't seem to get RequireJS to execute or parse some dependencies. My simplification is:

./app.js

<!doctype html>
<html>
<head>
    <title>Electron Boilerplate</title>
</head>
<body >
    <script data-main="require_config" src="vendor/require.js"></script>
    <h1>Hello World!</h1>
</body>
</html>

./require_config.js

console.log("REQUIRE_CONFIG.JS-------------"+requirejs.version);
requirejs.config({
    paths: {
    'app': './js/app'
  },
  // kick start application
  deps: ['./bootstrap']
});

./bootstrap.js

console.log("BOOTSTRAP.JS1-------------");
define( ['app'], function (app) {
    'use strict';
    console.log("BOOTSTRAP.JS2+++++++++++++");
});

./js/app.js

console.log("APP.JS-------------");
define([], function () {
  'use strict';
  console.log("APP.JS+++++++++++++");
});

The resulting output is still the same as my original output above. boostrap.js and require_config.js are processed. The bootstrap.js define/function is never called and app.js is never parsed.

1 Answer 1

0

After contacting the developer of the Electron boilerplate I think he set me straight. The main problem was my lack of understanding regarding all the moving pieces of the boilerplate. When attempting to extend the boilerplate I did not realize/understand at the time that his app was written using ES6 which was transpiled into a compatible RequireJS format. Therefore the problems I had was not with RequireJS at all. The modules I wrote were RequireJS compliant, then tranpiled which meant RequireJS could not deal with them.

The correct approach is to write ES6 modules and the problem goes away. A hack workaround is to add your modules using RequieJS compliant syntax then modify the boilerplate build.js copyFromAppDir to exclude these files from being transpiled. But again, to follow the boilerplates original intent and design, the code should be written with ES6.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.