search
HomeWeb Front-endJS Tutorial5 Minutes to Min-Safe Angular Code with Grunt

5 Minutes to Min-Safe Angular Code with Grunt

Web performance optimization is the primary issue that every web application developer pays attention to. Task runners such as Grunt play a key role in the development process. They automate tasks such as code stitching and compression, which is also the focus of this tutorial. We will use a range of Grunt plugins to ensure AngularJS applications can be compressed safely. Before discussing AngularJS and compression, I want to emphasize that developers of all skill levels can benefit from this tutorial, but it is better to have the basics of Grunt. In this article, we will use Grunt to generate new folders, so Grunt beginners can also have a good understanding of how it works.

Key Points

  • Task runners like Grunt automate code stitching and compression, optimizing page speed during development. The Grunt plug-in ensures that AngularJS applications can be compressed safely.
  • By default, AngularJS applications are not compressible and must be written in array syntax. When UglifyJS is running, it renames the parameters, but the existence of DI annotations in the array prevents them from being renamed, ensuring that the renamed parameters still have access to the necessary dependencies.
  • Grunt can be used to automate the annotation, splicing, and compression processes of AngularJS applications. After installing the necessary plug-ins and configuring Grunt to read the "package.json" file, the task will be loaded and registered. Then configure the plugin to locate specific files.
  • Grunt helps write safer Angular code by automating tasks that capture and prevent errors. It can run unit tests on the code every time the file is saved, sending an error alert to the developer immediately. Automation saves time and ensures that important tasks are not ignored.

Angular application compression problem

ArticleJS applications are not compressible safe by default. They must be written in array syntax. If you're not sure what the array syntax is, you've probably written code to use it. Let's look at two examples of AngularJS controllers that are passing the $scope and $http parameters. In the first example below, the module's factory and controller are wrapped in an array starting with a DI annotation, which, as you can see, does not follow the DRY (Don't repeat yourself) principle.

var form = angular.module('ControllerOne', [])
form.factory('Users', ['$http', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
}]);

form.controller('InputController', ['$scope', '$http', 'Users', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function () {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
                .success(function (data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });
        }
    };   
}]);

In the next example, the crud.config module code is still not compressible safe, but the code is shorter than previous ones. It just names the services and then passes the necessary dependencies into the function as parameters without writing them out as strings first. As long as there is no compression, this code will run normally. Therefore, it is easy to understand why people often choose this syntax when writing AngularJS code.

var form = angular.module('ControllerTwo', [])
form.factory('Users', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
});

form.controller('InputController', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function() {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
            .success(function(data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });        
        }
    };
});

Now that you have understood the physical differences between these two pieces of code, I will quickly explain why this syntax is not suitable for compression.

How array notation works

As mentioned above, array notation begins with DI annotations, which plays a key role in making code compression safe. When UglifyJS is run, it renames our parameters from $scope and $http to a and b respectively. The existence of DI annotations passed as strings into an array prevents them from being renamed. Therefore, these renamed parameters still have access to the necessary dependencies. If these annotations do not exist, the code will break. As you can see, it is very inefficient to write code manually in this way. To help you avoid this, I will now show you how to annotate, splice, and compress AngularJS applications in a fully optimized way using Grunt and prepare them for production.

Use Grunt

The entire repository of the project can be found on GitHub, including the files we will locate. For those who are used to using Grunt, you can continue and create your own build, or add this code to an existing project. If you are using an empty directory, you must make sure there is a "package.json" file in the directory. This file can be created by running the command npm init. Once you have the "package.json" file in your project, you can download the plugin by running the following command:

var form = angular.module('ControllerOne', [])
form.factory('Users', ['$http', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
}]);

form.controller('InputController', ['$scope', '$http', 'Users', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function () {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
                .success(function (data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });
        }
    };   
}]);

This will install Grunt into your project, and the three plugins we will use:

  • grunt-contrib-concat
  • grunt-contrib-uglify
  • grunt-ng-annotate

While ng-annotate can be used without Grunt, you will soon see how seamless Grunt makes the process of annotating, splicing, and compressing your code. It provides a simple and effective solution for compressing AngularJS code. If you are tracking this project from scratch, you should have a Gruntfile.js in the project root directory, which will contain all the Grunt code. If you haven't already, create it now.

Three steps to generate compression-safe code

Step 1 – Configure Grunt to read the "package.json" file

To access the plugin we installed earlier, you first need to configure the pkg property of the Gruntfile to read the contents of the "package.json" file. The config object starts at the top of the Grunt wrapper function and extends from line 3 to line 5 in the following example, but will soon include most of the code.

var form = angular.module('ControllerTwo', [])
form.factory('Users', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
});

form.controller('InputController', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function() {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
            .success(function(data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });        
        }
    };
});

Step 2 – Loading and Registering Grunt Tasks

After configuring Grunt to read our "package.json" files, plugins need to be loaded so that Grunt can access them. This is done by passing the plugin's name as a string into grunt.loadNpmTask() . Be sure to make sure that these plugins are loaded inside the wrapper function but outside the object. If these conditions are not met, Grunt will not work properly. What we need to do next is create a default task that will be performed when Grunt is called without a specific target. You should be careful about the order in which these tasks are added, as they will run according to their configuration. Here, ngAnnotate is configured to run first, then concat and UglifyJS, which I believe is the best way to build your code. Also, it is important to remember that config must be placed after the plugin is loaded. Based on what we just discussed, Gruntfile.js should look like this: grunt.registerTask()

Step 3 – Configuring the plug-in
var form = angular.module('ControllerOne', [])
form.factory('Users', ['$http', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
}]);

form.controller('InputController', ['$scope', '$http', 'Users', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function () {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
                .success(function (data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });
        }
    };   
}]);

ngAnnotate

Now that our Gruntfile is ready, let's go back to the

object and specify the file we want the ngAnnotate plugin to locate. To do this, we first have to create a part for ngAnnotate and create a target, called

in this case. In this target, you specify the file to which you want to add DI annotations, and the folder to which it should be generated. In this example, Grunt takes the three files specified in

and generates them into a new folder named config. Once the configuration is complete, you can run spApp and see how the code is generated. Additionally, you can visit the GitHub page of grunt-ng-annotate and see the different options it allows you to specify. public/js public/min-safe grunt ngAnnotateSplit

var form = angular.module('ControllerTwo', [])
form.factory('Users', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
});

form.controller('InputController', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function() {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
            .success(function(data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });        
        }
    };
});
Now that you have generated a folder with the AngularJS code with the new annotations, let's continue by compiling or splicing this code into a single file. Same way we created the section for ngAnnotate, we will now do the same for concat and UglifyJS. Like ngAnnotate, both tasks accept a target, in this case

. Many configuration options can be passed into these tasks, but we simply specify

and

to point to the correct file. As you might guess, these plugins will get the file contents passed to the js object and process them into the folder specified after src. Let's try to understand what's going on here. You can test this by running dest in your terminal, it should result in the creation of src. dest

var form = angular.module('ControllerOne', [])
form.factory('Users', ['$http', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
}]);

form.controller('InputController', ['$scope', '$http', 'Users', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function () {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
                .success(function (data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });
        }
    };   
}]);

Compression

The last thing we need to do is remove useless space from the code by compressing it. This is where the UglifyJS plugin comes into play. When using UglifyJS, we want Grunt to complete the final process of compressing the application. Therefore, we want to locate the file containing all the new splicing codes, in this case public/min/app.js. To test this, run grunt uglify and view your newly compressed file. The following are the relevant configurations for this task:

var form = angular.module('ControllerTwo', [])
form.factory('Users', function($http) {
    return {
        get: function() {
            return $http.get('/api/users');
        },
        create: function(userData) {
            return $http.post('/api/users', userData);
        },
        delete: function(id) {
            return $http.delete('/api/users/' + id);
        }
    };
});

form.controller('InputController', function($scope, $http, Users) {
    formData = {};
    $scope.createUser = function() {
        if ($scope.formData != undefined) {
            Users.create($scope.formData)
            .success(function(data) {
                $scope.users = data;
                $scope.formData = {};
                $scope.myForm.$setPristine(true);
            });        
        }
    };
});

In this course, we used all of these tasks separately. Now, let's use the default task we created earlier. It allows Grunt to run all specified tasks one by one in registration order. Now, just run grunt in your project and your code will be annotated, spliced, and compressed.

Conclusion

I hope that with this short tutorial you will be able to understand array notation well and why it is essential to make AngularJS application compression safe. If you are new to Grunt, I highly recommend you try these plugins as well as others as they can save a lot of time. As always, feel free to comment below or if you have any questions please email me at the address in my profile.

FAQ (FAQ) on Compression-Safe Angular Code with Grunt

(The same FAQ part should be included here as in the original text, but the language is smoother and more natural)

The above is the detailed content of 5 Minutes to Min-Safe Angular Code with Grunt. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool