Skip to content

Instantly share code, notes, and snippets.

@gogocurtis
Forked from polinagogo/min.md
Last active June 9, 2017 23:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gogocurtis/d463d543c20a47762c9f91160677f749 to your computer and use it in GitHub Desktop.
Save gogocurtis/d463d543c20a47762c9f91160677f749 to your computer and use it in GitHub Desktop.
Making sure we have minification safe angular code

For regular Javascript name your default function

ie:

Instead of doing this:

export default function (campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Name your function:

export default function myDirective(campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Instead of doing this

export default (campaignService) => {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });    
    },
    controller: function($scope,$browser) {}
  };
}

Do this: (Explicitly annotate the dependencies)

If you use one of the special angular functions (ie: controller) in the return you will need to anotate that function explicitly as well:

import { CampService } from "../ancillary/utils/typings";

// ANNOTATE dependencies for the exported service
service.$inject = ["campaignService"];
export default function service(campaignService: CampService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });    
    },
    // Annotate resolve functions
    resolve: {
      nil : ["$q", function($q) {} ]
    }
    // 
    // annotate the special angular functions too
    //
    controller: ["$scope", function($scope) { ... }]
  };
}

Instead of annotating like this (which is not going to work as expected):

/*@ngInject*/
import "./styles.less";
export default function myDirective(campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}

Annotate like this:

import "./styles.less";
myDirective.$inject = ["campaignService"]
export default function myDirective(campaignService) {
  return {
    scope: {},
    template: require('./my-directive.html'),
    link: function(scope) {
      campaignService.getCampaigns().then((campaigns) => {
        scope.campaigns = campaigns;
      });
    }
  };
}
@godspeedyoo
Copy link

"For regular Javascript the same rules as typescript apply." This is confusing, I initially read that to mean to apply the explicit injection pattern to javascript files, which you indicate we should not do.

@gogocurtis
Copy link
Author

Hi.

Jen Mei has requested that we use explicit annotations for both javascript and typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment