1

I have src of my application. I use AngularJS. I use RequireJS as module loader. I use Grunt as task runner. When I run application using src: everything is good. When I build application with Grunt, application is not working. I got no errors in console.

Main thing I noticed: my code (code of my application: app.js and files under js/) does not appear in output file which is set in grunt task settings. Also, I don't think there is something about AngularJS.

Main config file:

require.config({
  paths: {
    'angular' : '../components/angular/angular',
    /* etc..... */
    'jquery': '../components/jquery/dist/jquery',
    'application': './app'
  },
  shim: {
    /* etc */
    application: {
      deps: ['angular']
    },
    angular: {
      exports : 'angular'
    }
  },
  baseUrl: '/js'
});

require(['application', 'angular', 'ngRoute', 'bootstrap' /* ngRoute and bootstrap from etc :) */], function (app) {
  app.init();
});

My app in app.js is:

define([
  'require', 'angular', 'main/main', 'common/common'
], function (require) {
  'use strict';
  var angular = require('angular');
  var app = angular.module('myApp', ['ngRoute', 'main', 'common']);
  app.init = function () {
    angular.bootstrap(document, ['myApp']);
  };
  app.config(['$routeProvider',
    function ($routeProvider) {
      $routeProvider
        ./* ... some code */

    }
  ]);

  return app;
});

I add main RequireJS config file at the end of body tag:

<script type="text/javascript" src="components/requirejs/require.js" data-main="js/bootstrap.js"></script>

Now I have problem. I have Grunt as build system. I have this task:

grunt.initConfig({
  requirejs: {
    compile: {
      options: {
        baseUrl: "public/js",
        mainConfigFile: "public/js/bootstrap.js",
        name: 'bootstrap',
        out: "build/js/bootstrap.js",
        optimize: 'none'
      }
    }
  },
  // etc

I have no optimisation, so I get ~11k lines of code in output file.

As I said. Main problem is: no AngularJS code and no application code in output file.

Why? I set up mainConfigFile correctly. Problem is in RequireJS config file? But everything is okay, when I am running my app on src.

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87

1 Answers1

1

It would be better if you can provide the exactly error output you get. And where you got it (from browser's console or from terminal during build process)

For now I will suggest some adjustments what could possibly help with your case.

angular: {
    exports : 'angular'
}

Here you have already export angular.js into global local variable (inside every require and define block).

And by doing var angular = require('angular'); you are possibly asynchronously override angular variable inside your app.js module.

For 'require' being added into define block, as r.js always reading what module got to be loaded in very first step, and then merged into single file. And this may confuse r.js to merging requireJS into itself.

Suggest this adjustment for your app.js:

define([ // Removed 'require' because no needed , it is already global and usable anywhere
  'angular', 'main/main', 'common/common'
], function () {
  'use strict';
  // var angular = require('angular'); // This is a very common mistake. You are not going to call angular this way, requireJS difference with commonJS. 
  var app = angular.module('myApp', ['ngRoute', 'main', 'common']);
  app.init = function () {
    angular.bootstrap(document, ['myApp']);
  };
  app.config(['$routeProvider',
    function ($routeProvider) {
      $routeProvider
        ./* ... some code */

    }
  ]);

  return app;
});

And last but not least data-main="js/bootstrap.js" I think it should be js/main.js or a typo.

EDIT added explanations for 'require' in define block, and angular local variable.

Linh Pham
  • 3,005
  • 23
  • 34
  • Thank you for your answer. _As I mentioned in my question: I don't get any errors about mistakes in console or anywhere_. About your code: you remove require dependency and require call, right? I will try it at the evening, but I am not sure it will help. At least I don't understand _now_ how it will help. – Sharikov Vladislav Jan 19 '16 at 09:24
  • As I said: I have this symptom: I don't see any mentions of AngularJS or my source code in output file. Also, about bootstrap.js. I named main config file - bootstrap.js, so data-main is `js/bootstrap.js` – Sharikov Vladislav Jan 19 '16 at 09:56
  • Understood for `js/bootstrap.js` because in your question you called it main config :) --- regarding with the adjustment, it is to avoid to override your `angular` variable. When you define `var angular = require('angular')`. I will update my answer to explain it better. – Linh Pham Jan 19 '16 at 09:59
  • So, you just suggest something and it is not solution of my main answer? Better to post it as comment may be. Anyway, you might be correct. I will update my code anyway. I just don't understand how this will solve my main problem: no AngularJS or source code in output file. – Sharikov Vladislav Jan 19 '16 at 10:09
  • It is **possibly a solution**, but I do not have you code in hand. Maybe problem cause else where in other modules. Since we have no error output, I cannot confirm it but only you, when you apply my suggestion/solution. (It is just matter of a way to call it) – Linh Pham Jan 19 '16 at 10:13
  • Again. Symptom: AngularJS and source code doesn't appear in output file. How `grunt-contrib-requirejs` know about what to copy into output? Configuration set in mainConfigFile. I provided it: first block code. I set dependencies of my app: there is `jquery` and `bootstrap` (this libs appears in final output file), also I have `application` and `angular` as dependencies there. This libs not appear in output file. Why? Because the way how I include `angular` in my app.js? I don't even get `app.js` code in output. How it can fluence? – Sharikov Vladislav Jan 19 '16 at 10:20
  • No output is possibly because you are added `'require'` into `define` dependencies. This is ok for running rawly, but when it come to `'r.js'`, it is likely you are telling `r.js` to merge `requireJS` into itself and caused the conflict. --- (Check the answer again, I updated it with better explainations) – Linh Pham Jan 19 '16 at 10:23
  • 1
    Fine. Now really good explanation. I will try it as soon as possible. Voted up anyway. Thank you. – Sharikov Vladislav Jan 19 '16 at 10:33
  • Well, it didn't help me. I just tried to remove deps 1 by 1 and realised, that I have problem with boostrap dependency. I renamed it to bootstrap-lib and get worked builded project. Now I have new challenge :) 1.5 mb of main js file – Sharikov Vladislav Jan 19 '16 at 18:59
  • It is good to heard you found a solution :) --- Maybe try to use `angularAMD` with `ngload!` and some other on-demand loading for js file --- My main.js was pretty much the same weight, but after some adjustment it is 400kb+ for page initial. – Linh Pham Jan 20 '16 at 08:19
  • 1
    I solved this. I started using uglify2 optimization. Now I have 350 kb of my project src`s, libs and r.js lib. – Sharikov Vladislav Jan 20 '16 at 18:11