Giter Site home page Giter Site logo

angularts-minify-demo's Introduction

AngularTS Minify Demo

This is just a very basic sample of how to cope with minifying code to work with AngularJS dependency injection from TypeScript.

It uses two things:

This is a little package that goes through JavaScript code looking for AngularJS function patterns and automatically generating the annotated versions.

For example,

angular.module("x",[])
  .run(function($rootsScope) {
    $rootScope.appName = "X";
  });

becomes

angular.module("x",[])
  .run(["$rootScope", function($rootsScope) {
    $rootScope.appName = "X";
  }]);

(There is another package, ngmin, that works similarly, but it adds lines to the code it outputs, which throws off source-maps.)

However, neither ng-annotate nor ngmin will annotate class constructors, and when you're writing AngularJS in TypeScript, using classes is one of the best bits. So we need thing...

Uglify is the best minifier for JavaScript that isn't written in Java, and this year it added support for the AngularJS @ngInject annotation that is supported by Google's Closure compiler. You explicitly add a comment with the @ngInject slug before your constructor, and when Uglify passes by doing its minification thing, it generates the $inject value for the function.

Putting it together

So your compilation phase is basically:

  • Run TypeScript
  • Run ng-annotate over the TypeScript output to annotate the inline functions
  • Run Uglify with the angular option over the ng-annotate output to create your final .min.js

The Gruntfile for this project does all this using the Grunt modules for each component.

As a bonus, it also passes the output source-map from the TypeScript compilation into the Uglify process, so that the source-map for the minified file points right back to your .ts files. That's why I used ng-annotate rather than ngmin, you see.

angularts-minify-demo's People

Contributors

markrendle avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.