angular.js loaded instead of angular.min.js with requirejs

667 views Asked by At

I'm using Webjars to import AngularJS into my web project. For some reason the minified version of AngularJS won't be served even though I'm referencing those in my main. I was expecting to see angular.min.js and angular-route.min.js being loaded, but I'm seeing the regular angular.js and angular-route.js. What am I doing wrong here?

My main.js:

'use strict';

requirejs.config({
  paths: {
    'angular': '../lib/angularjs/angular.min',
    'angular-route': '../lib/angularjs/angular-route.min',
    'async': '../lib/requirejs-plugins/src/async'
  },
  shim: {
    'angular': {
      exports : 'angular'
    },
    'angular-route': {
      deps: ['angular'],
      exports : 'angular'
    }
  }
});

require(['angular', './controllers', './directives', './filters', './services', 'angular-route','./places-autocomplete','async','./gmaps'],
    function(angular, controllers) {
        initialize();

        // Declare app level module which depends on filters, and services
        angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngRoute']).
        config(['$routeProvider', function($routeProvider) {
            ....
        }]);

        angular.bootstrap(document, ['myApp']);

    });

My html loads requirejs like this:

<script>
   @Html(org.webjars.RequireJS.getSetupJavaScript(routes.WebJarAssets.at("").url))
</script>
<script data-main="@routes.Assets.versioned("javascripts/main.js")"
        src="@routes.WebJarAssets.at(WebJarAssets.locate("require.min.js"))"></script>

and the above requirejs.config snippet resides in main.js

2

There are 2 answers

1
Peter Paul Kiefer On BEST ANSWER

I looked into the sources of requirejs. Here's what I found:

requirejs splits each path, you defined in the config object, into its components (i.e. the directories, the filename and the extension). For some reason (node module naming conventions) the last extension is dropped. They do not check if that's a '.js'. Then this path array is used to access a module. Without a plugin rquirejs only handles .js files. It adds a .js if nessecary.

Now you can see what happens in your example. In the first step requirejs drops the .min extension. When it loads the module it joins the path components and adds a .js to the end. Then it loads the full module and not the minified version.

If you add a .js to your paths, then this .js was dropped and the .min is still there.

2
Lau On

Try to add enforceDefine: true,