Skip to content

Instantly share code, notes, and snippets.

@olov
Created May 19, 2014 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olov/eedc1788d221ac579868 to your computer and use it in GitHub Desktop.
Save olov/eedc1788d221ac579868 to your computer and use it in GitHub Desktop.
ng-annotate + assetgraph
Process (almost) all files with ng-annotate options {add: true, regexp: "^$"}
I say almost because there should be no risk that already minified files (i.e. matching the filename pattern *.min.* or something similar) need processing by ng-annotate. So we're saving the nature and build times by excluding them.
Add means "add annotations but don't remove or rebuild those already existing, if any". That's what you want. The weird-looking regexp-option really just means disable the support for short declaration forms.
This is a short declaration form:
myMod.controller("MyCtrl", function($scope, $timeout) {
});
And this is a long declaration form (which is officially recommended by the angular team):
angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {
});
As you can see, executed on tens of millions of non-angular-app-code, having short form support enabled (with no limitations) will eventuelly probably cause a false positive (thus breaking the build). So I think that assetgraph should disable that by default, and then let the user opt-in on enabling it by overriding the regexp-option through an assetgraph configuration. The user would be able to enable it for everything with {regexp: ".*"}, or only enable it for modules called myMod with {regexp: "^myMod$"}. I want to stress here that this regexp is only there for optionally limiting matching of the short form module identifier name. All of the heavy lifting matching happens on the AST level.
You did mention that assetgraph doesn't process a project unless it found ng-app in an html file. Perhaps that, together with not processing already minified files (which I assume should mean most major third party libraries in most real world builds), could mean that you'd be able to keep short declaration forms enabled by default. It seems quite unlikely (although not impossible) to stuble upon false positive trigging code that looks like it (but isn't) in an angularjs application codebase.
@Munter
Copy link

Munter commented May 19, 2014

Good suggestions.

I can certainly skip all filenames matching .min and hide the short declarations form behind a configuration switch.

Further more I can limit the collection of javascript files to only the ones actually in the immediate dependency graph of the html-file having the ng-app attribute. In that way I won't have to run annotation on dependencies of non-app pages that are in the graph, like 404 pages, login pages etc.

@olov
Copy link
Author

olov commented May 19, 2014

Great. If so then I would actually try it with ng-annotate's default options (just pass {add: true}) and then only if real-world issues arise, make short declarations opt-in.

@Munter
Copy link

Munter commented May 19, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment