AngularJS and minification

3,747 views
Skip to first unread message

chris...@gmail.com

unread,
Jun 23, 2012, 2:38:25 PM6/23/12
to ang...@googlegroups.com
Hi,

I was dabbling with the closure compiler to minify JS code and then realized the way angular templates(html) refer to Javascript variables and functions(for ex: Controller function). How to leverage minification with AngularJS?

Exporting the variables/functions would be a tedious process and Externing the Controller files does not serve the purpose of minificaton. Is there anything else that I could do?

Thanks,
Chris.

Witold Szczerba

unread,
Jun 24, 2012, 5:44:40 AM6/24/12
to ang...@googlegroups.com
You have to annotate your injection points (there are two ways to do
that) to tell angular explicitly what are the arguments or it will
discover automatically. When you minimize the code, Angular no longer
is able to do the auto detection, but annotated injection points work
fine.
See the Developer Guide, Dependency Injection chapter:
http://docs.angularjs.org/guide/di

Regards,
Witold Szczerba
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/ZqL7v_3v600J.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

chris...@gmail.com

unread,
Jun 24, 2012, 2:41:19 PM6/24/12
to ang...@googlegroups.com
I am not sure if that answers my question. I am not talking about services/dependencies here, I have always used the $inject array so that is not the issue.

To give you a specific example. HTML templates refer to controller functions and variables within the controller. But when the controller is minified all the names change including the controller function. How to get around this?

Closure compiler suggests Exports and Externs, it turns out to be tedious and not very effiecient respectively. Any way to get around this? How do I get around this according to your suggestion, if it is possible?

Thanks,
Chris.

Peter Bacon Darwin

unread,
Jun 24, 2012, 4:50:52 PM6/24/12
to ang...@googlegroups.com
Easiest is to define you controllers using the controller method of modules:  http://jsfiddle.net/UG2Fc/

To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/f9edd4BuQy4J.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

levyuk

unread,
Jan 19, 2013, 8:11:33 AM1/19/13
to ang...@googlegroups.com
Hi Peter,

I just minified your example using the advanced closure compiler and this is the code it produced

angular.b("myApp",[]).a("MyCtrl",["$scope","$log",function(b,a){a.log("hello")}]);

As you can see it's renamed module and controller.

How do you avoid this?

Cheers
Jon

Peter Bacon Darwin

unread,
Jan 19, 2013, 9:04:04 AM1/19/13
to ang...@googlegroups.com
Why would you want to?  The point of the minification process is to remove unnecessary long names for things.


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.

Pawel Kozlowski

unread,
Jan 19, 2013, 9:06:12 AM1/19/13
to ang...@googlegroups.com
HI!

AFAIK minification with advanced options won't produce correct results.
The AngularJS team explicitly discourages usage of advanced options in
this video: http://www.youtube.com/watch?v=ZhfUv0spHCY

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Jonathan Levy

unread,
Jan 19, 2013, 9:07:34 AM1/19/13
to ang...@googlegroups.com

But the code won't run without the angular.module

Peter Bacon Darwin

unread,
Jan 19, 2013, 9:07:27 AM1/19/13
to ang...@googlegroups.com
Oh, looking back over the thread.  I guess you are worried about not being able to call these functions?
What you have to be aware of is that the closure compiler is clever and will try to squeeze every last drop out of your source.  In this case, I guess, it decided that nothing else was going to be using these methods so it could change them.  The angular project uses SIMPLE_OPTIMIZATIONS.

Jonathan Levy

unread,
Jan 19, 2013, 9:08:30 AM1/19/13
to ang...@googlegroups.com

Ah right OK. I'll watch the video to see what they talk about

Reply all
Reply to author
Forward
0 new messages