Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

// @ngInject does not work if first line in file #13

Open
filipesilva opened this issue Sep 20, 2015 · 4 comments
Open

// @ngInject does not work if first line in file #13

filipesilva opened this issue Sep 20, 2015 · 4 comments

Comments

@filipesilva
Copy link

Using this loader

  module: {
    loaders: [{
      test: /\.js$/,
      include: path.resolve(__dirname, 'app/'),
      loaders: ['ng-annotate', 'babel-loader']
    }, 
    (...)

If my file contains, for example

// @ngInject
function controller($stateParams, session, tiles, organizations) {
  let vm = this;
}

It gets compiled to

// @ngInject
'use strict';

Object.defineProperty(exports, '__esModule', {
  value: true
});

function controller($stateParams, session, tiles, organizations) {
  var vm = this;
}
exports["default"] = controller;
module.exports = exports["default"];

Causing the annotation to not work. In contrast, with if the comment is not the first line

let number = 1;

// @ngInject
function controller($stateParams, session, tiles, organizations) {
  let vm = this;
}

export default controller;

It compiles well

    "use strict";

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    var number = 1;

    // @ngInject
    function controller($stateParams, session, tiles, organizations) {
      var vm = this;
    }
    controller.$inject = ["$stateParams", "session", "tiles", "organizations"];

    exports["default"] = controller;
    module.exports = exports["default"];

I tried using the "ngInject" syntax and it works. Any suggestion to get the // @ngInject syntax to work as well in this case?

@andrey-skl
Copy link
Owner

@filipesilva Hello!
The reason is that Babel adds "use strict"; line in each file by default and you got this:

    // @ngInject
    "use strict";

    function controller($stateParams, session, tiles, organizations) {
      var vm = this;
    }

Sorry but I haven't workaround for this. Also I have never seen such problem with my code because my files always has imports in top.
You can vote up the original issue in Babel: babel/babel#2391

@filipesilva
Copy link
Author

I figured as much. Ah well, "ngInject" it is for now.

@orrybaram
Copy link

For those of you having some trouble getting the comment to work, try /** @ngInject */. Most minifiers will ignore comments starting with two *'s .

@andrey-skl
Copy link
Owner

Also I find that babel may drop comments, but you can set option comments: true and it will works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants