【问题标题】:Angular ng annotate does not work with babelAngular ng annotate 不适用于 babel
【发布时间】:2015-05-19 12:40:32
【问题描述】:

我正在使用 browserify 和 babel 编译我的 js 文件,我也想要 ng annotate 插件,但是它不起作用,有什么想法吗?

gulp 任务:

  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;

【问题讨论】:

  • “不工作”是什么意思,会发生什么?
  • 它没有发生,dist 代码中没有 $inject。
  • 为什么不在 browserify 转换之前运行 ng-annotate?

标签: javascript angularjs gulp


【解决方案1】:

我认为在您的情况下,Babel 正在剥离 cmets。 Ng-annotate 与编译后的 es5 代码一起工作,并且必须以某种方式知道要注释什么。您可以使用 comments: truecompact: false 运行 babel 以保留 cmets 或使用字符串文字:

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

【讨论】:

  • comments: true 实际上是默认值。在我的特殊情况下,我发现我正在使用 babel 将 ES6 模块转换为 commonjs,而 babel 在我的评论和输出的 function YourClass 之间插入了一些语句,这让 ng-annotate 感到沮丧。我的解决方案还最终将 "ngInject" 文字放在了函数的顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 2017-08-02
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多