andrey-skl / ng-annotate-loader

Webpack loader to annotate angular applications
MIT License
120 stars 28 forks source link

ngInject is it absolutly necessary ? #27

Closed kevincaradant closed 8 years ago

kevincaradant commented 8 years ago

Hi

If i write this:

export default function routes($stateProvider, $translateProvider, $compileProvider, $httpProvider, $urlRouterProvider) {
    "ngInject";
    $compileProvider.debugInfoEnabled(true);

It works

But i wish something like that: I don't want to write everytime 'ngInject';

export default function routes($stateProvider, $translateProvider, $compileProvider, $httpProvider, $urlRouterProvider) {
    $compileProvider.debugInfoEnabled(true);

But i get this error:

bundle-f89f47.js:56 Uncaught Error: [$injector:modulerr] Failed to instantiate module demo due to:
Error: [$injector:strictdi] i is not using explicit annotation and cannot be invoked in strict mode
abruzzihraig commented 8 years ago

I've met the same issue 2 months ago, it seems a bug in the underlying library ng-annotate rather than this loader. I am still waiting for someone could fix that.

andrey-skl commented 8 years ago

I believe it is the same issue as #13. Could you add any stuff before that line and test if ngAnnotate start working?

kevincaradant commented 8 years ago

I tried : loaders: ['ng-annotate?add=true&map=false', 'babel-loader?comments=true']. It's always the same error. What stuff do you talk ?

andrey-skl commented 8 years ago

@kevincaradant sorry for being unclear, I mean something like this:

console.log('foo');

export default function routes($stateProvider, $translateProvider, $compileProvider, $httpProvider, $urlRouterProvider) {
    $compileProvider.debugInfoEnabled(true);

Because ngAnnotate had an issue with annotating first line of code

kevincaradant commented 8 years ago

Oh no i take a simple example but in my case before this line : "ngInject";. I have some imports, and the begin of my ES6 class.

This is a real file:

NOT WORK:

import Page2 from './page2.html';
import RightMenu from 'components/rightMenu/rightMenu.html';
console.log('foo');
class Page2Config {
    static initRoute($stateProvider) {
        $stateProvider.state('page2', {
            url: '/page2',
            views: {
                mainView: {
                    templateUrl: Page2,
                    controller: 'page2Controller as page2'
                },
                rightMenuView: {
                    templateUrl: RightMenu,
                    controller: 'rightMenuController'
                }
            }
        });
    }
}

export default Page2Config.initRoute;

NOT WORK

import Page2 from './page2.html';
import RightMenu from 'components/rightMenu/rightMenu.html';

class Page2Config {
    static initRoute($stateProvider) {
        $stateProvider.state('page2', {
            url: '/page2',
            views: {
                mainView: {
                    templateUrl: Page2,
                    controller: 'page2Controller as page2'
                },
                rightMenuView: {
                    templateUrl: RightMenu,
                    controller: 'rightMenuController'
                }
            }
        });
    }
}

export default Page2Config.initRoute;

WORK !

import Page2 from './page2.html';
import RightMenu from 'components/rightMenu/rightMenu.html';

class Page2Config {
    static initRoute($stateProvider) {
        'ngInject';
        $stateProvider.state('page2', {
            url: '/page2',
            views: {
                mainView: {
                    templateUrl: Page2,
                    controller: 'page2Controller as page2'
                },
                rightMenuView: {
                    templateUrl: RightMenu,
                    controller: 'rightMenuController'
                }
            }
        });
    }
}

export default Page2Config.initRoute;
andrey-skl commented 8 years ago

@kevincaradant Okay now I see. I don't want to dissapoint you, but that's how ngAnnotate works - since it doesn't support ES6, it takes ES5 transpiled code.

In your case this code has no markers to let ngAnnotate know that this function should be annotated. ngAnnotate parses code and checks if function passes to .controller(fn), to .directive(fn) and so on, so your case is just not supported. https://github.com/olov/ng-annotate#explicit-annotations-with-nginject

I have nothing to do with this because ng-annotate-loader just wraps ngAnnotate.

I'm afraid that even more modern tools like babel-plugin-angularjs-annotate, babel-plugin-ng-annotate won't help there.

I'm sorry again, but I have to close this issue since it won't be fixed here.

kevincaradant commented 8 years ago

OK np, i was just asking and you have perfectly answered at my question. I believed that was possible natively but i have had wrong ;)

Thank you :)