3

I have set up an Angular application with requireJS. Before deploying, I bundle everything into one and unique file. Usually, external libraries doesn't have a requireJS 'define' wrapper.

For example, the Angular Parallax library is written like this:

# https://github.com/durated/angular-parallax/blob/master/angular-parallax.min.js
angular.module("duParallax",["duScroll","duParallax.directive"...

Here is my main.js:

require.config({
  baseUrl: '/angular-dev',
  paths: {
    angular: '//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min',
    angularScroll: 'bower_components/angular-scroll/angular-scroll.min',
    angularParallax: 'bower_components/ng-parallax/angular-parallax.min'
  },
  shim: {
    'angular': { exports: 'angular' },
    'angularScroll': { deps: ['angular'] },
    'angularParallax': { deps: ['angular'] }
  }
});

I'm using grunt-contrib-requirejs to then bundle all the libraries (not CDN ones) to one and unique file. This is what I get after bundling everything:

angular.module("duParallax",["duScroll","duParallax.directive"...
define("angularParallax", ["angular"], function(){});

But what I expect is the code to be wrapped within the define:

define("angularParallax", ["angular"], function(){
  angular.module("duParallax",["duScroll","duParallax.directive"...
});

How can I achieve the latest without changing the library itself?

Edit: According to this other stackoverflow question, this is how it should work. But I'm also using grunt-contrib-requirejs to bundle my code and that might be the problem.

Shim from grunt-contrib-requirejs not wrapping library

Community
  • 1
  • 1
Vincent Peres
  • 1,003
  • 1
  • 13
  • 28
  • 1
    Sorry for the 'priority', this is legacy that I will remove. Angular is loaded from a CDN as you can see, so it's not bundled within the rest of the libraries. It means that in my generated file, "angular" is not defined yet, that's why I expect it to be wrapped within "define" and then loaded when "angular" become available. – Vincent Peres Feb 20 '14 at 13:34
  • That bit you've added "I'm using grunt-contrib-requirejs to then bundle all the libraries" clarifies the issue quite a lot, so I'm retracting my comment. Yeah, it is not doing what it should do. Unfortunately, I'm not using this tool so I don't readily know what you'd have to do. – Louis Feb 20 '14 at 13:39
  • Thanks for your help, I hope it will clarify for others! – Vincent Peres Feb 20 '14 at 13:44

1 Answers1

2

By default r.js will not wrap non-AMD compliant libraries. You need to add the following option to the grunt-requirejs-contrib settings:

wrapShim: true
Vincent Peres
  • 1,003
  • 1
  • 13
  • 28