bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Uglify fails - Unknown provider: aProvider <- a

Open mastix opened this issue 12 years ago • 42 comments

Hi guys,

I ran into an issue yesterday when I've added the angular-bootstrap JS to my project (which has been set up using the Yeoman Angular Generator) and then it failed due to uglification.

I've added the lib via bower (uncompressed!) and it got included into the vendor.js for concatination and minification.

When the file (ui-bootstrap-tpls.js; uncompressed) is concatenated and bundled in one big vendor.js, everything looks fine, but as soon as the uglify task kicks in (with mangle: true = default), this fails (although the grunt ngmin task should rewrite the code to use Angular's inline annotation method):

The error message was pretty much this one:

Error: [$injector:unpr] Unknown provider: aProvider <- a

Pretty much like this bug: https://github.com/yeoman/generator-angular/issues/378

Is this a bug in the angular-bootstrap code and how it's implemented? Does anybody have an idea why this is happening and how it can be fixed (without turning mangle off)?

Just a hint would be cool! :+1:

I've set mangle to false in the uglify.js code... and everything works fine, but I'd really like to turn it back to true again. :)

Regards,

mastix

mastix avatar May 23 '14 07:05 mastix

I've encountered this problem literally 5 minutes ago too without any idea.

fno avatar May 23 '14 09:05 fno

Haha... what a coincidence! :) You can set mangle to false, so variable names won't be shortened, then everything should work... but that leads to bigger JS files... :(

mastix avatar May 23 '14 09:05 mastix

@mastix @fno this problem does not happen with the minified version we are providing, check our demo page: http://angular-ui.github.io/bootstrap/

I'm not sure what are the exact minification steps you are taking, but if you can prepare a minimal project showing that uglify is breaking our deliverables I can surely take a look. Otherwise this one is hardly actionable....

pkozlowski-opensource avatar May 23 '14 11:05 pkozlowski-opensource

OK, here is more info: http://stackoverflow.com/questions/17238759/angular-module-minification-bug

and what can be done on the library level: https://github.com/gruntjs/grunt-contrib-uglify#no-mangling

I don't think we can do anything meaningful in the library so I'm going to close this issue. Will be happy to re-open if you guys think there is anything actionable on our side.

pkozlowski-opensource avatar May 23 '14 11:05 pkozlowski-opensource

Hi,

sure, as soon as mangling is disabled, everything is cool, but that also increases the file size a lot.

I'm using ng-min before uglify (also on angular-bootstrap), but for some reason all of my controllers and included js files work fine, but the ui.bootrap file does not.

ng-min does the following:

angular.module('whatever').controller('MyCtrl', function ($scope, $http) { ... });

gets turned into:

angular.module('whatever').controller('MyCtrl', ['$scope', '$http', function ($scope, $http) { ... }]);

Now minifiers can handle AngularJS's dependency injection annotations since the attributes are added as string params in the array... but for some reason that does not work for angular-bootstrap.

So there might be actually something to check on your side!?

Thanks for your reply & help!!!

mastix

mastix avatar May 23 '14 12:05 mastix

@mastix our deliverables already got all the minification-safe annotations (if you can spot places where it is not the case it is a bug that should be fixed - but I don't think so since the minified version we provide does work) so there is no reason to pass this code through the ng-min. This might actually cause problems.

Then, I'm not saying that some parts of our code make it uglify - unfriendly. If this is the case I'm all for fixing things. But in order to act we need to know which part of the code is causing problems.

@mastix @fno for now I assume that this is your custom build that is causing problems here, but I would like to be proven otherwise. So if you see anything strange please do let us know.

pkozlowski-opensource avatar May 23 '14 16:05 pkozlowski-opensource

i dont know about the "custom" build as it seems to stem from a very popular approach using yeoman, bower and grunt while using the angular-generator.

when you install angular-bootstrap via bower into this setup, the ui-bootstrap-tpls.js is automatically added to the whole vendor.js pipeline (ngmin, uglify, etc) ... this seems to be the root cause of this.

so to reproduce:

npm install -g yo npm install -g generator-angular bower install angular-bootstrap grunt && grunt serve

as im fairly new to the js/angular ecosystem it seems not quite obvious as to where to place this problem exactly.

thanks for your time.

fno avatar May 26 '14 11:05 fno

Don't have a solution yet... :( Kind of postponed it, until there's one available. This must be fixed before my project goes live - can't deliver my js with the long variable names! :)

mastix avatar May 26 '14 19:05 mastix

well you could move the ui-bootstrap-tpls.js out of the uglify/concatenation block in your index.html

fno avatar May 27 '14 07:05 fno

What worked for me is to exclude Angular itself from the mangling if you concatenate everything into one file. I'm not sure if it's going to work in your case, but you could give it a try.

Grunt

grunt.initConfig({
  uglify: {
    options: {
      mangle: {
        except: ['angular']
      }
    }
  }
});

Gulp

.pipe(uglify({
    mangle: {
        except: ['angular']
    }
}))

mvhecke avatar May 27 '14 08:05 mvhecke

@mastix @fno I would really love to help here but you need to help me to help you... Couple of things:

  • there is really, really no reason to run our deliverables through ng-min. I guess this is what is causing your issues;
  • if you need assistance please, please provide minimal reproduce scenario in plunker - or just post a gist of the file you are trying to load.

pkozlowski-opensource avatar May 27 '14 17:05 pkozlowski-opensource

@pkozlowski-opensource It's hard to upload it somewhere since it's kind of a process (grunt -> that minifies files) and not a resulting file.

I don't think that this is a "real" problem with your files, all I know is that when we I use ngmin on my files, they're getting converted correctly, so uglifying works like a charm there. You're file is part of this process as well (and I have no idea how to exclude it from the ngmin-step) and so ngmin is being called for that file as well.

I don't want this file to be served with a single request... it should be packaged into the whole js file.

It would be great if someone had an idea.

Thanks guys!

mastix avatar May 27 '14 18:05 mastix

Btw - that's the error message: screen shot 2014-05-27 at 20 15 04

mastix avatar May 27 '14 18:05 mastix

@pkozlowski-opensource

as i already mentioned above its probably not your minified files but the way that your project is integrated into a very widespread development approach. to my knowledge other angular directives that install via bower dont exhibit this problem. i may be totally wrong here, but i think that @mastix has the same problem of attributing the real source of error here.

  1. start an ng project via yeoman: "yo angular mynewproject"
  2. install angular bootstrap via bower: "bower install angular-bootstrap"
  3. add the 'ui.bootstrap' dependencie in your app.js
  4. "compile" your project and serve it: "grunt serve:dist" (you can also grunt build and deploy to somewhere else, doesnt change the error)

what seems to be happening is that bower places the ui-bootstrap-tpls.js file into the block that is later minified and concatenated as "vendor.js". the error is fixed by moving it out of the block or disabling "mangle". i'm too much of an angularjs novice to correctly attribute the real source of error, its probably a configuration somewhere that decides where to automatically place the includes.

fno avatar May 28 '14 07:05 fno

@fno I am unable to duplicate this.

$ sudo npm install -g yo generator-angular
$ yo angular test
$ cd test
$ bower install --save angular-bootstrap
$ grunt serve:dist --force # to prevent imagemin errors

And everything works. I then added a directive to the main.html page and it showed it just fine. Also, while I agree with @pkozlowski-opensource about not needing to run the file through ng-min, I have done it many times as here without issue. So I don't know what's going on with your code. :-(

Is there any more information you can provide?

joshdmiller avatar May 28 '14 10:05 joshdmiller

The problem seems to be occurring because of the concatenation.

In Index.html if you separate out ui-bootstrap-tpls.js

    <!-- build:js(.) scripts/vendor2.js -->
    <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
    <!-- endbuild -->

And in your root bower.json add

...
"overrides": {
  "angular-bootstrap": {
    "main": []
  }
}
...

Everything will work just fine and, for the most part, your files are uglified. Obviously this is just a workaround.

PeterTrotter avatar Jun 24 '14 16:06 PeterTrotter

I got a similar bug recently, it was not caused by ngmin, uglify or ui-bootstrap per se. I my case the bug was caused by me not using array syntax when passing a controller as a parameter in modal options.

Because of ngmin I am not used to using array syntax for my controllers, services etc. so i forgot to add array syntax to the modalOptions.controller property:

// causes bug when uglifying (ngmin will not change this to array syntax)
modalOptions.controller = function($scope) { /* ... */ };
$modal.open(modalOptions)
// will work with ngmin and uglify
modalOptions.controller = ['$scope', function($scope) { /* ... */ }];
$modal.open(modalOptions)

knutesten avatar Jul 03 '14 15:07 knutesten

For me this was angular ui-router and I was injecting a property in the controller of a state and it wasn't having it.

sethen avatar Jul 09 '14 03:07 sethen

@knutesten Thank you very much for posting your solution. Solved the problem for me as well :+1:

michelbb avatar Aug 04 '14 16:08 michelbb

@knutesten Rockin! This was the ticket for me also. :sparkles:

bonpixel avatar Aug 14 '14 20:08 bonpixel