Saturday, February 14, 2015

Overview

Angularjs is a great framework. The problem is that minification is a bit more complicated because of angular’s dependency injection. This project shows how you can minify an angular app. You can find more information here.

Installation

Step 1

1
2
3
4
5
6
7
8
### Command Line ###

# Get the dependencies
npm install -g grunt-cli

npm install --save-dev load-grunt-tasks
npm install --save-dev grunt-contrib-clean
npm install --save-dev grunt-contrib-uglify

Step 2

1
2
3
4
5
6
7
8
// ### in public/javascripts/index.js ###

// Make sure that your angular syntax is like this
MyApp.controller("ThingController", ["$scope",
    function($scope){
        $scope.something = "Minification of angular apps works!";
    }
]);

Step 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// ### in Gruntfile.js ###
"use strict";

module.exports = function(grunt){
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        clean: {
            build: {
                src: ["public/javascripts/index.min.js"]
            }
        },// End clean
        
        uglify: {
            options: {
                mangle: {
                    toplevel: true
                },
                compress: true
            },
            my_target: {
                files: {
                    'public/javascripts/index.min.js': ["public/javascripts/index.js"]
                }
            }
        },// End uglify
    });

    grunt.registerTask("default", ["clean", "uglify"]);
};

Step 4

1
2
### Command Line ###
$ grunt

Random Posts