jemz
jemz

Reputation: 5123

how to create grunt file to minified my js files

I have no idea how to minified my JSfiles using grunt.I hope someone can help me how to create grunt file in order to minified my js files step by step.

Upvotes: 0

Views: 78

Answers (1)

Mario Araque
Mario Araque

Reputation: 4572

First, run npm init to create a package.json.

npm init

Then, create a Gruntfile.js file:

module.exports = function(grunt) {

  grunt.initConfig({
    uglify: {
      my_target: {
        files: {
          'path/to/your/minified.js': ['src/script1.js', 'src/script2.js', 'src/script3.js']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-uglify');

  grunt.registerTask('default', ['uglify']);

}; 

To minify your JS files I recommend grunt-uglify.

Here is the documentation, but I will share the installation step by step.

Install the plugin via npm:

npm install grunt-contrib-uglify --save-dev

Then, add this in your Gruntfile.js:

grunt.loadNpmTasks('grunt-contrib-uglify');

The basic configuration is like this:

grunt.initConfig({
  uglify: {
    my_target: {
      files: {
        'path/to/your/minified.js': ['src/script1.js', 'src/script2.js', 'src/script3.js']
      }
    }
  }
});

Your minified.js file is the result of the minification of all your js files.

Hope it helps.

Upvotes: 1

Related Questions