SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
Front-End Automation
with Grunt
Belén Albeza
@ladybenko
www.belenalbeza.com

Friday, 25 October 13
A workflow example
Lint

Test
Open
browser

Watch
assets
Friday, 25 October 13

Compile
assets
Run
server
Recompile
assets

Reload
browser
We do this already…
• python -m SimpleHTTPServer
• open index.html
• sass --watch sass:css
• jshint main.js
• ./conquer_the_world.sh

• Etc.
Friday, 25 October 13
What if we integrate all
these tasks?
• We can setup long flows and run it with
just one task

• We can stop the flow if a task fails (for

instance, abort when linting spots an error)

• Everyone in the dev team can follow the
same workflow

Friday, 25 October 13
Friday, 25 October 13
Grunt
Friday, 25 October 13
What is Grunt?
• A JavaScript task runner
npm install -g grunt-client
• Lots of plugins for front-end and Node
development

Friday, 25 October 13
Gruntfile.js
• A JavaScript file with our Grunt config
• Think of it as a Makefile, Rakefile, etc.
• We can use plugins that provide common
tasks…

• …or we can cook our own tasks!
Friday, 25 October 13
module.exports(function (grunt) {
[‘a-cool-grunt-plugin’,
‘another-plugin’
].forEach(grunt.loadNpmTasks);
grunt.initConfig({
// ...
});
});

Friday, 25 October 13
How to run tasks
•

grunt mytask will run all the targets of

•

grunt mytask:target will run the specific

mytask
target of mytask
grunt clean
grunt sass:dev

Friday, 25 October 13
Linter
• A Linter analyses our code
• Looks for syntax errors (awesome for
script languages)

• Looks for formatting errors (ex: “don’t use
more than 80 chars per line!”)

• Looks for bad practises (ex: “you can’t use
this variable without declaring it first”)

Friday, 25 October 13
Install JSHint plugin
• npm install grunt-contrib-jshint
• grunt jshint

Friday, 25 October 13
grunt.initConfig({
jshint: {
all: [‘Gruntfile.js’,
‘js/**/*.js’]
}
});

Friday, 25 October 13
Sass
• CSS, as a language, sucks
• Sass is a nice language that compiles to CSS
• We have variables, inheritance/mixins, a
clean syntax, partials...

Friday, 25 October 13
@import _reset
$main-color: #cff
$fg-color: #000
@mixin std-button
background: $main-color
color: $fg-color
a.button, button
+std-button

Friday, 25 October 13
Install Sass plugin
• npm install
• grunt sass

Friday, 25 October 13

grunt-contrib-sass
grunt.initConfig({
sass: {
dev: {
options: {
style: ‘expanded’,
lineComments: true
},
files: {
‘css/main.css’: ‘sass/main.sass’
}
}
}
});
Friday, 25 October 13
// 1-to-1 file mapping. Ex:
// sass/unicorn.sass -> css/unicorn.css
files: {
expand: true,
cwd: ‘sass’,
src: [‘**/*.sass’],
dest: ‘css’,
ext: ‘.css’
});

Friday, 25 October 13
Watch
• You may have several daemons listening for
changes to files to do something

• Examples: Sass/LESS stylesheets,

CoffeeScript files, Handlebars templates,
etc.

• With Grunt you can group all of them in a
single place

Friday, 25 October 13
Install Watch plugin
• npm install
• grunt watch

Friday, 25 October 13

grunt-contrib-watch
grunt.initConfig({
watch: {
sass: {
files: [‘sass/**/*.sass’],
tasks: [‘sass:dev’]
}
}
});

Friday, 25 October 13
Build your own flows
• You can create tasks than run other tasks
• Use them to set-up workflows, like “regular
development” or “build a release”.

Friday, 25 October 13
grunt.registerTask(‘server’, [‘clean’,
‘jshint’, ‘sass:dev’, ‘jasmine’,
‘connect:server’, ‘open’, ‘watch’]);
grunt.registerTask(‘release’, [‘clean’,
‘jshint’, ‘sass:prod’, ‘jasmine’,
‘copy:release’]);

Friday, 25 October 13
Create your own tasks
• If you don’t find the right plugin, you can
create your very own task…

• …and wrap it in a plugin so others can use
it as well ;)

Friday, 25 October 13
grunt.registerTask(‘version’,
‘shows version number’, function () {
// implement our own task
var pkg = grunt.file.readJSON(
‘package.json’);
grunt.log.writeln(pkg.version);
});

Friday, 25 October 13
Other useful plugins
• grunt-contrib-clean: deletes files and

directories (usefull for temporary files)

• grunt-contrib-jasmine: run Jasmine tests
from the console and in the browser

• grunt-contrib-copy: copy files (useful to
make builds)

• grunt-contrib-connect: run a local server
Friday, 25 October 13
Browse more plugins
• http://gruntjs.com/plugins
• There are plugins to minify CSS, reload a
browser tab, create zips, erase files, build
assets (Handlebars, Coffee, LESS, etc.)…

OS grunt plugin!
• And our Firefoxgrunt-firefoxos:)
npm install

Friday, 25 October 13
Sample code
https://github.com/
belen-albeza/grunt-demo

Friday, 25 October 13
Demo

Friday, 25 October 13
A different use case
• This is just not only for front-end or Node
development...

• Think of other projects you can automate!

Friday, 25 October 13
A book!

Friday, 25 October 13
A book in Leanpub
• Leanpub is a publishing platform
• You write your manuscripts in Markdown
(plain text) and put them into a shared
folder in Dropbox

• Then, they build a pretty eBook from your
plain text files

Friday, 25 October 13
Problem
• You need to put your manuscript files in
Dropbox…

• …but I want my own version control in
Github…

• …and I have other files (PSD’s, sample

code, etc.) that I don’t want to put into that
folder

Friday, 25 October 13
Grunt to the rescue
Lint sample
code
Convert
from Github
MD to
Leanpub MD

Friday, 25 October 13

Zip sample
code
Copy MD
files to
Dropbox
More about Grunt
• Grunt’s official “Getting Started Guide”
www.gruntjs.com/getting-started

• How to create your own tasks http://
gruntjs.com/creating-tasks

• “Power-Up Your Front-End Development
with Grunt” www.leanpub.com/grunt

Friday, 25 October 13
Thanks!
Questions?

@ladybenko
Friday, 25 October 13

Mais conteúdo relacionado

Mais procurados

Improving your workflow with gulp
Improving your workflow with gulpImproving your workflow with gulp
Improving your workflow with gulp
frontendne
 

Mais procurados (20)

Bower & Grunt - A practical workflow
Bower & Grunt - A practical workflowBower & Grunt - A practical workflow
Bower & Grunt - A practical workflow
 
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
 
Grunt and Bower
Grunt and BowerGrunt and Bower
Grunt and Bower
 
Grunt training deck
Grunt training deckGrunt training deck
Grunt training deck
 
Grunt
GruntGrunt
Grunt
 
Improving your workflow with gulp
Improving your workflow with gulpImproving your workflow with gulp
Improving your workflow with gulp
 
Getting Started With Grunt for WordPress Development
Getting Started With Grunt for WordPress DevelopmentGetting Started With Grunt for WordPress Development
Getting Started With Grunt for WordPress Development
 
Automate your WordPress Workflow with Grunt.js
Automate your WordPress Workflow with Grunt.jsAutomate your WordPress Workflow with Grunt.js
Automate your WordPress Workflow with Grunt.js
 
Advanced front-end automation with npm scripts
Advanced front-end automation with npm scriptsAdvanced front-end automation with npm scripts
Advanced front-end automation with npm scripts
 
Migration from Drupal 7 to Drupal 8 - How Docker can save our lives!
Migration from Drupal 7 to Drupal 8 - How Docker can save our lives!Migration from Drupal 7 to Drupal 8 - How Docker can save our lives!
Migration from Drupal 7 to Drupal 8 - How Docker can save our lives!
 
Intro to Gulp
Intro to GulpIntro to Gulp
Intro to Gulp
 
Dial up your flow
Dial up your flowDial up your flow
Dial up your flow
 
Headless approach and Acquia - Case study - Chris Ozog
Headless approach and Acquia - Case study - Chris OzogHeadless approach and Acquia - Case study - Chris Ozog
Headless approach and Acquia - Case study - Chris Ozog
 
Gulp - the streaming build system
Gulp - the streaming build systemGulp - the streaming build system
Gulp - the streaming build system
 
Daniel Steigerwald: EsteJS - javascriptové aplikace robusně, modulárně a komf...
Daniel Steigerwald: EsteJS - javascriptové aplikace robusně, modulárně a komf...Daniel Steigerwald: EsteJS - javascriptové aplikace robusně, modulárně a komf...
Daniel Steigerwald: EsteJS - javascriptové aplikace robusně, modulárně a komf...
 
Modern Development Tools
Modern Development ToolsModern Development Tools
Modern Development Tools
 
Grunt understanding
Grunt understandingGrunt understanding
Grunt understanding
 
Firebase and AngularJS
Firebase and AngularJSFirebase and AngularJS
Firebase and AngularJS
 
Grunt js and WordPress
Grunt js and WordPressGrunt js and WordPress
Grunt js and WordPress
 
Intro to go web assembly
Intro to go web assemblyIntro to go web assembly
Intro to go web assembly
 

Semelhante a Front-end development automation with Grunt

eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
PatrickCrompton
 

Semelhante a Front-end development automation with Grunt (20)

Front End Development Automation with Grunt
Front End Development Automation with GruntFront End Development Automation with Grunt
Front End Development Automation with Grunt
 
Drupal 8 - Hosting, Performance and Drush
Drupal 8 - Hosting, Performance and DrushDrupal 8 - Hosting, Performance and Drush
Drupal 8 - Hosting, Performance and Drush
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013XPages Blast - Lotusphere 2013
XPages Blast - Lotusphere 2013
 
Unit and functional testing with Siesta
Unit and functional testing with SiestaUnit and functional testing with Siesta
Unit and functional testing with Siesta
 
Off the Treadmill: Building a Drupal Platform for Your Organization
Off the Treadmill: Building a Drupal Platform for Your OrganizationOff the Treadmill: Building a Drupal Platform for Your Organization
Off the Treadmill: Building a Drupal Platform for Your Organization
 
Angular boilerplate generator
Angular boilerplate generatorAngular boilerplate generator
Angular boilerplate generator
 
Sutol 2016 - Automation is developer's friend
Sutol 2016 - Automation is developer's friendSutol 2016 - Automation is developer's friend
Sutol 2016 - Automation is developer's friend
 
Writing native Linux desktop apps with JavaScript
Writing native Linux desktop apps with JavaScriptWriting native Linux desktop apps with JavaScript
Writing native Linux desktop apps with JavaScript
 
Mastering ElasticSearch with Ruby and Tire
Mastering ElasticSearch with Ruby and TireMastering ElasticSearch with Ruby and Tire
Mastering ElasticSearch with Ruby and Tire
 
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
eSynergy Andy Hawkins - Enabling DevOps through next generation configuration...
 
Past, Present, and Pachyderm - All Things Open - 2013
Past, Present, and Pachyderm - All Things Open - 2013Past, Present, and Pachyderm - All Things Open - 2013
Past, Present, and Pachyderm - All Things Open - 2013
 
Modern web technologies
Modern web technologiesModern web technologies
Modern web technologies
 
node.js in action
node.js in actionnode.js in action
node.js in action
 
Miscelaneous Debris
Miscelaneous DebrisMiscelaneous Debris
Miscelaneous Debris
 
Ansible
AnsibleAnsible
Ansible
 
DevOops Redux Ken Johnson Chris Gates - AppSec USA 2016
DevOops Redux Ken Johnson Chris Gates  - AppSec USA 2016DevOops Redux Ken Johnson Chris Gates  - AppSec USA 2016
DevOops Redux Ken Johnson Chris Gates - AppSec USA 2016
 
Using filesystem capabilities with rsync
Using filesystem capabilities with rsyncUsing filesystem capabilities with rsync
Using filesystem capabilities with rsync
 
Cd syd
Cd sydCd syd
Cd syd
 
Ruby meetup 7_years_in_testing
Ruby meetup 7_years_in_testingRuby meetup 7_years_in_testing
Ruby meetup 7_years_in_testing
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

Front-end development automation with Grunt

  • 1. Front-End Automation with Grunt Belén Albeza @ladybenko www.belenalbeza.com Friday, 25 October 13
  • 2. A workflow example Lint Test Open browser Watch assets Friday, 25 October 13 Compile assets Run server Recompile assets Reload browser
  • 3. We do this already… • python -m SimpleHTTPServer • open index.html • sass --watch sass:css • jshint main.js • ./conquer_the_world.sh • Etc. Friday, 25 October 13
  • 4. What if we integrate all these tasks? • We can setup long flows and run it with just one task • We can stop the flow if a task fails (for instance, abort when linting spots an error) • Everyone in the dev team can follow the same workflow Friday, 25 October 13
  • 7. What is Grunt? • A JavaScript task runner npm install -g grunt-client • Lots of plugins for front-end and Node development Friday, 25 October 13
  • 8. Gruntfile.js • A JavaScript file with our Grunt config • Think of it as a Makefile, Rakefile, etc. • We can use plugins that provide common tasks… • …or we can cook our own tasks! Friday, 25 October 13
  • 10. How to run tasks • grunt mytask will run all the targets of • grunt mytask:target will run the specific mytask target of mytask grunt clean grunt sass:dev Friday, 25 October 13
  • 11. Linter • A Linter analyses our code • Looks for syntax errors (awesome for script languages) • Looks for formatting errors (ex: “don’t use more than 80 chars per line!”) • Looks for bad practises (ex: “you can’t use this variable without declaring it first”) Friday, 25 October 13
  • 12. Install JSHint plugin • npm install grunt-contrib-jshint • grunt jshint Friday, 25 October 13
  • 14. Sass • CSS, as a language, sucks • Sass is a nice language that compiles to CSS • We have variables, inheritance/mixins, a clean syntax, partials... Friday, 25 October 13
  • 15. @import _reset $main-color: #cff $fg-color: #000 @mixin std-button background: $main-color color: $fg-color a.button, button +std-button Friday, 25 October 13
  • 16. Install Sass plugin • npm install • grunt sass Friday, 25 October 13 grunt-contrib-sass
  • 17. grunt.initConfig({ sass: { dev: { options: { style: ‘expanded’, lineComments: true }, files: { ‘css/main.css’: ‘sass/main.sass’ } } } }); Friday, 25 October 13
  • 18. // 1-to-1 file mapping. Ex: // sass/unicorn.sass -> css/unicorn.css files: { expand: true, cwd: ‘sass’, src: [‘**/*.sass’], dest: ‘css’, ext: ‘.css’ }); Friday, 25 October 13
  • 19. Watch • You may have several daemons listening for changes to files to do something • Examples: Sass/LESS stylesheets, CoffeeScript files, Handlebars templates, etc. • With Grunt you can group all of them in a single place Friday, 25 October 13
  • 20. Install Watch plugin • npm install • grunt watch Friday, 25 October 13 grunt-contrib-watch
  • 21. grunt.initConfig({ watch: { sass: { files: [‘sass/**/*.sass’], tasks: [‘sass:dev’] } } }); Friday, 25 October 13
  • 22. Build your own flows • You can create tasks than run other tasks • Use them to set-up workflows, like “regular development” or “build a release”. Friday, 25 October 13
  • 23. grunt.registerTask(‘server’, [‘clean’, ‘jshint’, ‘sass:dev’, ‘jasmine’, ‘connect:server’, ‘open’, ‘watch’]); grunt.registerTask(‘release’, [‘clean’, ‘jshint’, ‘sass:prod’, ‘jasmine’, ‘copy:release’]); Friday, 25 October 13
  • 24. Create your own tasks • If you don’t find the right plugin, you can create your very own task… • …and wrap it in a plugin so others can use it as well ;) Friday, 25 October 13
  • 25. grunt.registerTask(‘version’, ‘shows version number’, function () { // implement our own task var pkg = grunt.file.readJSON( ‘package.json’); grunt.log.writeln(pkg.version); }); Friday, 25 October 13
  • 26. Other useful plugins • grunt-contrib-clean: deletes files and directories (usefull for temporary files) • grunt-contrib-jasmine: run Jasmine tests from the console and in the browser • grunt-contrib-copy: copy files (useful to make builds) • grunt-contrib-connect: run a local server Friday, 25 October 13
  • 27. Browse more plugins • http://gruntjs.com/plugins • There are plugins to minify CSS, reload a browser tab, create zips, erase files, build assets (Handlebars, Coffee, LESS, etc.)… OS grunt plugin! • And our Firefoxgrunt-firefoxos:) npm install Friday, 25 October 13
  • 30. A different use case • This is just not only for front-end or Node development... • Think of other projects you can automate! Friday, 25 October 13
  • 31. A book! Friday, 25 October 13
  • 32. A book in Leanpub • Leanpub is a publishing platform • You write your manuscripts in Markdown (plain text) and put them into a shared folder in Dropbox • Then, they build a pretty eBook from your plain text files Friday, 25 October 13
  • 33. Problem • You need to put your manuscript files in Dropbox… • …but I want my own version control in Github… • …and I have other files (PSD’s, sample code, etc.) that I don’t want to put into that folder Friday, 25 October 13
  • 34. Grunt to the rescue Lint sample code Convert from Github MD to Leanpub MD Friday, 25 October 13 Zip sample code Copy MD files to Dropbox
  • 35. More about Grunt • Grunt’s official “Getting Started Guide” www.gruntjs.com/getting-started • How to create your own tasks http:// gruntjs.com/creating-tasks • “Power-Up Your Front-End Development with Grunt” www.leanpub.com/grunt Friday, 25 October 13