olov / ng-annotate

Add, remove and rebuild AngularJS dependency injection annotations
MIT License
2.03k stars 150 forks source link

ngAnnotate mangles sourcemaps #212

Closed conartist6 closed 8 years ago

conartist6 commented 8 years ago

ngAnnotate sourcemaps are generally bad citizens that tend to expose the buggiest side of sourcemap implementations especially when they are one of many source mapping steps which need to be combined into a single map definitive map.

The reason is that ngAnnotate tokenizes the code and then emits a mapping for every token. When other upstream transformations generate mappings that are less granular than ngAnnotate's all hell breaks loose. I've crashed sourcemap debuggers with the results and seen dev tools do the wildest things.

The solution is simple, emit only line mappings when at all possible, like such:

var HelloController = function($scope) {
    $scope.greet = "Goodbye cruel world!";
}
//^ input
var HelloController = function($scope) {
    $scope.greet = "Goodbye cruel world!";
}
HelloController.$inject = ['$scope'];
//^ output

Maps to -> line 1 pos 0 maps to line 1 pos 0 line 2 pos 4 maps to line 2 pos 4 line 3 pos 0 maps to line 3 pos 0 line 5 pos 0 maps to line 4 pos 0

In my testing output like the above works great. I have a patch that does the above, but mine assumes that it will always be using $inject and never creating an inline injection parameter. I'd be willing to help get it right for both cases, but I'd like to be sure there's interest first.

olov commented 8 years ago

As long as you're ready to maintain the code (i.e. fix problems with it) I'd love to get further sourcemap improvements into ng-annotate so hack away! :-)

conartist6 commented 8 years ago

Sure, I can do that. I'll see what I can work up.

olov commented 8 years ago

:+1:

olov commented 8 years ago

There are also two other open sourcemap issues - one that I think should be easy to fix (perhaps as a side-effect of your planned PR) and the other one bigger. #201 and #202. :-)

olov commented 8 years ago

https://github.com/olov/ng-annotate/issues/245