Angular ng annotate does not work with babel

2019-05-24 06:16发布

问题:

I am using browserify and babel to compile my js files and i want the ng annotate plugin too but it is not working, any ideas why?

The gulp task:

  browserify(config.js.src, { debug: true })
    .transform(babel.configure({ ignore: /vendor\// }))
    .bundle()
    .pipe(source(config.js.mainFileName))
    .pipe(buffer())
    .pipe(sourcemaps.init({ loadMaps: true }))
    .pipe(ngAnnotate())
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest(config.js.dist));

class HomeController {
  // @ngInject
  constructor($http) {
    this.name = 'avi';
  }
}

export default HomeController;

回答1:

I think in your case Babel is stripping comments. Ng-annotate works with compiled es5 code and have to somehow know what to annotate. You can either run babel with comments: true and compact: false to preserve comments or use string literals:

constructor($http) {
    "ngInject";
    this.name = 'avi';
}