SlideShare una empresa de Scribd logo
1 de 3
Descargar para leer sin conexión
Nguyễn Ngọc Dũng
https://nndung179.wordpress.com Page 1
Compressed JS with NodeJS & GruntJS
This tutorial will show you how to use grunt to compress js files from your
project.
1. Install node js
2. Install Grunt CLI globally
npm install -g grunt-cli
3. For example, I have this structure of web:
4. At folder JSCompressed create files:
a. package.json
{
"name" : "menory",
"title" : "test",
"version" : "1.0.0",
"devDependencies": {
"grunt": "0.4.1",
"grunt-contrib-concat": "0.1.3",
"grunt-contrib-cssmin" : "0.6.1",
"grunt-contrib-watch" : "0.5.3",
"grunt-contrib-uglify" : "0.2.0"
}
}
5. Open cmd
a. + cd to JSCompressed folder
b. Execute: npm install
6. Configuration Gruntfile.js
a. Gruntfile.js
Nguyễn Ngọc Dũng
https://nndung179.wordpress.com Page 2
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
js: {
src: [
'lib/jquery.js',
'lib/bootstrap.min.js'
],
dest: 'combined.js'
}
},
uglify: {
js: {
files: {
'combined.js':
['combined.js']
}
}
},
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('default', ['concat:js',
'uglify:js']);
};
b. Explaination Gruntfile.js
concat: {
js: {
src: [
'lib/jquery.js',
'lib/bootstrap.min.js'
],
dest: 'combined.js'
}
},
Nguyễn Ngọc Dũng
https://nndung179.wordpress.com Page 3
This block of codes means: you will copy all source code fron js files into
1 file call combined.js (A)
Note: at src attribute, you must place js files with the order of
dependency.
uglify: {
js: {
files: {
'combined.js': ['combined.js']
}
}
},
This block of codes means: from combined.js (A), you will compress you
js here.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
Load all module you have installed at step 5
grunt.registerTask('default', ['concat:js',
'uglify:js']);
This block of codes is very important, It means that after you do all
configurations. You will register as task for Grunt to execute all tasks
you have config above.
7. Result
The result of this procress will be a file with jquery & bootstrap in one
file under compressed format.

Más contenido relacionado

La actualidad más candente

Web technologies for desktop development
Web technologies for desktop developmentWeb technologies for desktop development
Web technologies for desktop development
Darko Kukovec
 

La actualidad más candente (20)

Nightwatch.js (vodQA Shots - Pune 2017)
Nightwatch.js (vodQA Shots - Pune 2017)Nightwatch.js (vodQA Shots - Pune 2017)
Nightwatch.js (vodQA Shots - Pune 2017)
 
Building a REST API with Node.js and MongoDB
Building a REST API with Node.js and MongoDBBuilding a REST API with Node.js and MongoDB
Building a REST API with Node.js and MongoDB
 
NodeJS
NodeJSNodeJS
NodeJS
 
進階使用Nodejs 淺談no sql(mongodb)
進階使用Nodejs 淺談no sql(mongodb)進階使用Nodejs 淺談no sql(mongodb)
進階使用Nodejs 淺談no sql(mongodb)
 
(C)NodeJS
(C)NodeJS(C)NodeJS
(C)NodeJS
 
Node.js and Ruby
Node.js and RubyNode.js and Ruby
Node.js and Ruby
 
virtualization course content
virtualization course contentvirtualization course content
virtualization course content
 
What is Node.js
What is Node.jsWhat is Node.js
What is Node.js
 
Brig:Node.js + QML 華麗大冒險
Brig:Node.js + QML 華麗大冒險Brig:Node.js + QML 華麗大冒險
Brig:Node.js + QML 華麗大冒險
 
Fundamental of Node.JS - Internship Presentation - Week7
Fundamental of Node.JS - Internship Presentation - Week7Fundamental of Node.JS - Internship Presentation - Week7
Fundamental of Node.JS - Internship Presentation - Week7
 
Gerenciando múltiplas versões do PostgreSQL com pgvm
Gerenciando múltiplas versões do PostgreSQL com pgvmGerenciando múltiplas versões do PostgreSQL com pgvm
Gerenciando múltiplas versões do PostgreSQL com pgvm
 
S&T What I know about Node 110817
S&T What I know about Node 110817S&T What I know about Node 110817
S&T What I know about Node 110817
 
Quick Introduction to Node.js
Quick Introduction to Node.jsQuick Introduction to Node.js
Quick Introduction to Node.js
 
Nodejs presentation
Nodejs presentationNodejs presentation
Nodejs presentation
 
Cscript exe
Cscript exeCscript exe
Cscript exe
 
Deep dive-oz
Deep dive-ozDeep dive-oz
Deep dive-oz
 
Web technologies for desktop development
Web technologies for desktop developmentWeb technologies for desktop development
Web technologies for desktop development
 
Capodieci - Proof of... what?
Capodieci - Proof of... what?Capodieci - Proof of... what?
Capodieci - Proof of... what?
 
Node in Real Time - The Beginning
Node in Real Time - The BeginningNode in Real Time - The Beginning
Node in Real Time - The Beginning
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 

Destacado

3 phelan ifa prague 2012-al
3 phelan ifa prague 2012-al3 phelan ifa prague 2012-al
3 phelan ifa prague 2012-al
ifa2012_2
 
1 judd-ifa 2012 pp(3)
1 judd-ifa 2012 pp(3)1 judd-ifa 2012 pp(3)
1 judd-ifa 2012 pp(3)
ifa2012_2
 
Ophelia and gertrude
Ophelia and gertrudeOphelia and gertrude
Ophelia and gertrude
bhavnabaraiya
 
Melt consulting
Melt consultingMelt consulting
Melt consulting
matquittre
 
Πρόσκληση εκπαιδευτών
Πρόσκληση εκπαιδευτώνΠρόσκληση εκπαιδευτών
Πρόσκληση εκπαιδευτών
iekkastorias
 
Laboratorio excel
Laboratorio excelLaboratorio excel
Laboratorio excel
josetjitt
 

Destacado (20)

Duality in AdS/CFT, Chicago 7 Nov. 2014
Duality in AdS/CFT, Chicago 7 Nov. 2014Duality in AdS/CFT, Chicago 7 Nov. 2014
Duality in AdS/CFT, Chicago 7 Nov. 2014
 
Strings, Quantum Gravity, and Reduction
Strings, Quantum Gravity, and ReductionStrings, Quantum Gravity, and Reduction
Strings, Quantum Gravity, and Reduction
 
Стандарт технического сопровождения LinuxWizard
Стандарт технического сопровождения LinuxWizardСтандарт технического сопровождения LinuxWizard
Стандарт технического сопровождения LinuxWizard
 
3 phelan ifa prague 2012-al
3 phelan ifa prague 2012-al3 phelan ifa prague 2012-al
3 phelan ifa prague 2012-al
 
1 judd-ifa 2012 pp(3)
1 judd-ifa 2012 pp(3)1 judd-ifa 2012 pp(3)
1 judd-ifa 2012 pp(3)
 
5 Our Cosmic Origins
5 Our Cosmic Origins5 Our Cosmic Origins
5 Our Cosmic Origins
 
Estevez amb to_mex
Estevez amb to_mexEstevez amb to_mex
Estevez amb to_mex
 
Somo iOS 9 workshop
Somo iOS 9 workshopSomo iOS 9 workshop
Somo iOS 9 workshop
 
Jeimy lucero gomez1
Jeimy lucero gomez1Jeimy lucero gomez1
Jeimy lucero gomez1
 
Ophelia and gertrude
Ophelia and gertrudeOphelia and gertrude
Ophelia and gertrude
 
INTEGRATE Chicago - Joseph Truncale
INTEGRATE Chicago - Joseph TruncaleINTEGRATE Chicago - Joseph Truncale
INTEGRATE Chicago - Joseph Truncale
 
Melt consulting
Melt consultingMelt consulting
Melt consulting
 
CREA Black Holes
CREA Black HolesCREA Black Holes
CREA Black Holes
 
AVAILABILITY
AVAILABILITYAVAILABILITY
AVAILABILITY
 
Kevingrazon
KevingrazonKevingrazon
Kevingrazon
 
Jenifer bohorquez 2
Jenifer bohorquez 2Jenifer bohorquez 2
Jenifer bohorquez 2
 
Miller Arenas 2
Miller  Arenas 2Miller  Arenas 2
Miller Arenas 2
 
Πρόσκληση εκπαιδευτών
Πρόσκληση εκπαιδευτώνΠρόσκληση εκπαιδευτών
Πρόσκληση εκπαιδευτών
 
Laboratorio excel
Laboratorio excelLaboratorio excel
Laboratorio excel
 
Liberal Arts Conference - Core Texts in the Sciences
Liberal Arts Conference - Core Texts in the SciencesLiberal Arts Conference - Core Texts in the Sciences
Liberal Arts Conference - Core Texts in the Sciences
 

Similar a Compressed js with NodeJS & GruntJS

Similar a Compressed js with NodeJS & GruntJS (20)

Using GruntJS
Using GruntJSUsing GruntJS
Using GruntJS
 
Get Grulping with JavaScript Task Runners (Matt Gifford)
Get Grulping with JavaScript Task Runners (Matt Gifford)Get Grulping with JavaScript Task Runners (Matt Gifford)
Get Grulping with JavaScript Task Runners (Matt Gifford)
 
Grunt All Day
Grunt All DayGrunt All Day
Grunt All Day
 
GruntJS + Wordpress
GruntJS + WordpressGruntJS + Wordpress
GruntJS + Wordpress
 
Grunt & Front-end Workflow
Grunt & Front-end WorkflowGrunt & Front-end Workflow
Grunt & Front-end Workflow
 
Modern Development Tools
Modern Development ToolsModern Development Tools
Modern Development Tools
 
Automating Front-End Workflow
Automating Front-End WorkflowAutomating Front-End Workflow
Automating Front-End Workflow
 
Grunt understanding
Grunt understandingGrunt understanding
Grunt understanding
 
Node js training (1)
Node js training (1)Node js training (1)
Node js training (1)
 
Introduction to using Grunt & Bower with WordPress theme development
Introduction to using Grunt & Bower with WordPress theme developmentIntroduction to using Grunt & Bower with WordPress theme development
Introduction to using Grunt & Bower with WordPress theme development
 
Introduction to Express and Grunt
Introduction to Express and GruntIntroduction to Express and Grunt
Introduction to Express and Grunt
 
Let Grunt do the work, focus on the fun!
Let Grunt do the work, focus on the fun!Let Grunt do the work, focus on the fun!
Let Grunt do the work, focus on the fun!
 
Node JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web AppNode JS Express : Steps to Create Restful Web App
Node JS Express : Steps to Create Restful Web App
 
Grunt
GruntGrunt
Grunt
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 
Prosit google-cloud
Prosit google-cloudProsit google-cloud
Prosit google-cloud
 
Get Grulping with JavaScript Task Runners
Get Grulping with JavaScript Task RunnersGet Grulping with JavaScript Task Runners
Get Grulping with JavaScript Task Runners
 
Front end development gurant
Front end development gurantFront end development gurant
Front end development gurant
 
Overview of Node JS
Overview of Node JSOverview of Node JS
Overview of Node JS
 
Get Gulping with Javascript Task Runners
Get Gulping with Javascript Task RunnersGet Gulping with Javascript Task Runners
Get Gulping with Javascript Task Runners
 

Más de David Nguyen (13)

ACOMP_2014_submission_70
ACOMP_2014_submission_70ACOMP_2014_submission_70
ACOMP_2014_submission_70
 
jQuery Super Basic
jQuery Super BasicjQuery Super Basic
jQuery Super Basic
 
Javascript native OOP - 3 layers
Javascript native OOP - 3 layers Javascript native OOP - 3 layers
Javascript native OOP - 3 layers
 
MVC4 – knockout.js – bootstrap – step by step – part 1
MVC4 – knockout.js – bootstrap – step by step – part 1MVC4 – knockout.js – bootstrap – step by step – part 1
MVC4 – knockout.js – bootstrap – step by step – part 1
 
Facebook API
Facebook APIFacebook API
Facebook API
 
Quick sort
Quick sortQuick sort
Quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Heap Sort
Heap SortHeap Sort
Heap Sort
 
Chứng minh số node của Heap chiều cao h
Chứng minh số node của Heap chiều cao hChứng minh số node của Heap chiều cao h
Chứng minh số node của Heap chiều cao h
 
Hướng dẫn sử dụng Mind Manager 8
Hướng dẫn sử dụng Mind Manager 8 Hướng dẫn sử dụng Mind Manager 8
Hướng dẫn sử dụng Mind Manager 8
 
KTMT Lý Thuyết Tổng Quát
KTMT Lý Thuyết Tổng QuátKTMT Lý Thuyết Tổng Quát
KTMT Lý Thuyết Tổng Quát
 
KTMT Số Nguyên - Số Chấm Động
KTMT Số Nguyên - Số Chấm ĐộngKTMT Số Nguyên - Số Chấm Động
KTMT Số Nguyên - Số Chấm Động
 
Mô Hình MVC 3.0
Mô Hình MVC 3.0Mô Hình MVC 3.0
Mô Hình MVC 3.0
 

Último

DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Último (20)

Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 

Compressed js with NodeJS & GruntJS

  • 1. Nguyễn Ngọc Dũng https://nndung179.wordpress.com Page 1 Compressed JS with NodeJS & GruntJS This tutorial will show you how to use grunt to compress js files from your project. 1. Install node js 2. Install Grunt CLI globally npm install -g grunt-cli 3. For example, I have this structure of web: 4. At folder JSCompressed create files: a. package.json { "name" : "menory", "title" : "test", "version" : "1.0.0", "devDependencies": { "grunt": "0.4.1", "grunt-contrib-concat": "0.1.3", "grunt-contrib-cssmin" : "0.6.1", "grunt-contrib-watch" : "0.5.3", "grunt-contrib-uglify" : "0.2.0" } } 5. Open cmd a. + cd to JSCompressed folder b. Execute: npm install 6. Configuration Gruntfile.js a. Gruntfile.js
  • 2. Nguyễn Ngọc Dũng https://nndung179.wordpress.com Page 2 module.exports = function (grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), concat: { js: { src: [ 'lib/jquery.js', 'lib/bootstrap.min.js' ], dest: 'combined.js' } }, uglify: { js: { files: { 'combined.js': ['combined.js'] } } }, }); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['concat:js', 'uglify:js']); }; b. Explaination Gruntfile.js concat: { js: { src: [ 'lib/jquery.js', 'lib/bootstrap.min.js' ], dest: 'combined.js' } },
  • 3. Nguyễn Ngọc Dũng https://nndung179.wordpress.com Page 3 This block of codes means: you will copy all source code fron js files into 1 file call combined.js (A) Note: at src attribute, you must place js files with the order of dependency. uglify: { js: { files: { 'combined.js': ['combined.js'] } } }, This block of codes means: from combined.js (A), you will compress you js here. grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-uglify'); Load all module you have installed at step 5 grunt.registerTask('default', ['concat:js', 'uglify:js']); This block of codes is very important, It means that after you do all configurations. You will register as task for Grunt to execute all tasks you have config above. 7. Result The result of this procress will be a file with jquery & bootstrap in one file under compressed format.