Espacio Daycry - Espacio de programación

  • Inicio
  • Categorias
    • - Codeigniter
    • - Symfony
    • - HTML5
    • - Linux / Ubuntu
    • - PHP
    • - Jquery
  • PortFolio - Proyectos Codeiniter
    • - Encuestas Online
    • - Estadísticas - GLPI
    • - Gestión de colas
    • - Web Service - REST

lunes, 23 de julio de 2018

CSS & JS concatenation and minification

Posted by daycry at 19:49 Labels: css , grunt , Javascript , minified
Combine and minify your CSS and JS contents. Benefit of combining contents is that the browser will make one request to the server instead of many. While minification reduces the size the file.

There are various tools for combining and minification of contents. PHP Laravel framework comes with built in tool for combining and minification of assets. While you can use GruntJS with CodeIgniter.

GruntJS is a NodeJS tool so you have to install it with NodeJS package manager NPM. To use GruntJS, you will need to follow the following steps.


  • Open your command line tool and install npm by running the following commands
install npm

  • Then install GRUNT by running the following command
install -g grunt-cli

  • After successful installation, go to your project root directory
cd {project_root_directory_name}
  • Create gruntfile and package.json files
touch package.json Gruntfile.js
  • Initialize grunt
npm init
  • Install grunt plugins
npm install grunt --save-dev
npm install grunt-contrib-concat --save-dev
npm install grunt-contrib-uglify --save-dev
npm install grunt-contrib-cssmin –save
  • See grunt packages in package.json file
{
  "name": "project name",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "grunt": "~0.4.5",
    "grunt-cli": "~0.1.13",
    "grunt-contrib-concat": "~0.5.0"
  },
  "devDependencies": {
    "grunt-contrib-concat": "~0.5.0",
    "grunt-contrib-uglify": "~0.7.0",
    "grunt-contrib-cssmin": "~0.11.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "name of author",
  "license": "BSD-2-Clause"
}
  • Finally see the grunt task runner in Gruntjs file
/*
 * grunt-cli
 * http://gruntjs.com/
 *
 * Copyright (c) 2012 Tyler Kellen, contributors
 * Licensed under the MIT license.
 * https://github.com/gruntjs/grunt-init/blob/master/LICENSE-MIT
 */
 
'use strict';
 
module.exports = function(grunt) {
 
    grunt.initConfig({
        concat: {
            css: {
                src: ['assets/css/bootstrap.min.css', 
      'assets/css/styles.css'],
                dest: 'assets/css/main.css'
              },
 
            js: {
                src: ['assets/css/bootstrap.min.js', 
      'assets/css/script.js'],
                dest: 'assets/js/main.js'
              }
            },
        uglify: {
            js: {
                src: 'assets/js/main.js',
                dest: 'assets/js/main.min.js'
                }
            },
        cssmin: {
            css: {
                src: 'assets/css/main.css',
                dest: 'assets/css/main.min.css'
                }
            }
        });
 
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.registerTask('build', ['concat', 'uglify', 'cssmin']);
};
You can modify the above file according to your requirements.

For concatenation run: grunt concat
For js minification run: grunt uglify
For css minification run: grunt cssmin
You can also build all at once by running: grunt build
Tweet

No hay comentarios :

Publicar un comentario

Entrada más reciente Entrada antigua Inicio
Ver versión para móviles
Suscribirse a: Enviar comentarios ( Atom )

Sígueme en las Redes Sociales



Follow @daycry9

Daycry web


Donaciones

Suscribirse a

Entradas
Atom
Entradas
Comentarios
Atom
Comentarios

Datos personales

daycry
Ver todo mi perfil

Entradas populares

  • Crear archivos PHP ejecutables por terminal UBUNTU
    En este apartado vamos a explicar como ejercutar archivos PHP a través del terminal de Ubuntu. Lo primero que tendríamos que hacer es inst...
  • Pâginación PHP con Librería Zebra Pagination
    En este blog voy a comentar un tema que se utilizan en casi todas las páginas web que existen, y es el tema de la paginación. La paginaci...
  • PHPExcel - Codeigniter
    Este post trata de la integración de la librería PHPExcel en Codeigniter, aunque se podría aplicar a cualquier librería, como por ejemplo mP...
  • PHP- Operaciones con fechas - Sumar Horas, minutos y segundos
    Ejemplo para añadir o sumar un número determinado de hora/s, minuto/s, segundo/s a una fecha en php. Con la función strtotime se puede ...
  • Codeigniter - Múltiples conexiones a base de datos
    Este tema es uno de los temas primordiales sobre el framework Codeigniter, ya que en alguna ocación nos hemos visto obligados a recoger dato...

© Espacio Daycry - Espacio de programación 2013 . Powered by Bootstrap , Blogger templates and RWD Testing Tool