grunt, gulp & fabs: build systems and development-workflow for modern web-applications

30
Grunt, Gulp & fabs Build-System and Development-Workflow for modern Web-Applications

Upload: philipp-burgmer

Post on 27-Jan-2015

110 views

Category:

Technology


0 download

DESCRIPTION

Slides for my talk at Karlsruher Entwicklertag 2014. Visit us at http://www.thecodecampus.de Folien zu meinem Vortrag beim Karlsruher Entwicklertag 2014. Für die meisten Sprachen hat sich in den letzten Jahren und Jahrzehnten ein Tool als Build-System etabliert. Maven für Java, Make für Linux, SBT für Scala usw. und so fort. Doch was ist mit Web-Anwendungen die z.B. auf AngularJS basieren, bei denen die gesamte UI im Browser läuft, die immer komplexer werden, die immer mehr Code enthalten und aus immer mehr einzelnen Dateien bestehen. Wer oder besser gesagt womit baut man hier seine Anwendung? Wer führt Tests aus und wie liefere ich solche eine Anwendung aus? Welche Hürden sind hier zu nehmen? Mit Grunt hat sich 2013 ein modernes JavaScript Pendant zu Ant etabliert. Doch bei genauerer Betrachtung ist Grunt zwar moderner, aber trotzdem ähnlich abstrakt und low-level wie Ant. Hier kommen Bower und fabs ins Spiel: Bower kümmert sich um Dependencies und fabs konfiguriert Grunt um einen feature-reichen, anpassbaren Build zur Verfügung zu stellen. In meinem Vortrag beim Karlsruher Entwicklertag 2014 habe ich gezeigt wie mit Grunt, Bower und fabs ein moderner Build und Development-Workflow mit LiveReload, Test-Ausführung, Code-Minimierung und vielem mehr aufgebaut werden kann um auch große Web-Anwendungen mit mehreren Entwicklern schnell voran treiben und zuverlässig ausliefern zu können.

TRANSCRIPT

Page 1: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Grunt, Gulp & fabs

Build-System and Development-Workflowfor modern Web-Applications

Page 2: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Philipp BurgmerSoftware Engineer / Consultant / TrainerFocus: Frontend, Web Technologies

WeigleWilczek [email protected]

ABOUT ME

Page 3: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

WeigleWilczek / W11kSoftware Design, Development & MaintenanceConsulting, Trainings & Project Kickoff

Web Applications with AngularJSNative Rich Clients with Eclipse RCP

ABOUT US

Page 4: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

HOW DO YOU START A NEW WEB-APP?

Page 5: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,edit, switch, refresh, test, switch, edit, switch, refresh, test, switch,...

AND HOW DOES IT LOOK LIKEAFTER ONE NIGHT OF

Page 6: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

and no structure in sight

A LOT OF FILES TO MANAGE

Page 7: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

43 Dependencies to other Libraries169 JavaScript Files112 HTML Templates...

AFTER 3 MONTHS

Page 8: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Manage Dependencies to 3th Party Libraries(Versioning, Conflicts, Download and Inclusion)Include Own JavaScript and CSS FilesStructure and Modularize CodeToo Much Overhead During DevelopmentFind Even Simple Errors Only at Runtime

PROBLEMS

Page 9: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

SOLUTION?THE RIGHT TOOLS MAKE ALL THE DIFFERENCE!

(http://www.nodejs.org)

Runtime forDev Tools

(http://www.gruntjs.com)

Task Runner

Bower(http://www.bower.io)

DependencyManagement

Page 10: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Command Line ToolNode PackageInstall Binary via npm install -g bowerNo Project Specific Version

BOWERA PACKAGE MANAGER FOR THE WEB

Page 11: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Define Dependencies in bower.json{ "name": "fabs-presentation", "dependencies": { "angular": "1.2.16", "twbs-bootstrap-sass": "3.1.1", "w11k-slides": "0.4.1" }}

Download Dependencies via bower installHalf the Problem Remains: bower Does Not Manage InclusionHint: Use Fixed Versions Within a Project and Variable Within a Library

BOWERA PACKAGE MANAGER FOR THE WEB

Page 12: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Command Line ToolLets You Automate Almost EverythingSplit into 2 Node PackagesInstall Binary Globally via npm install -g grunt-cliDefine Dependency to Project Specific Version in package.jsonInstall Locally via npm install

GRUNTTHE JAVASCRIPT TASK RUNNER

Page 13: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Configure Your Tasks and Targets in Gruntfile.jsEverything Is a Plugin (via NPM and package.json )Countless Plugins AvailableTasks and Configuration Are Pure JavaScriptRegister Custom Tasks in Gruntfile.js ,No Need to Write a Plugin

GRUNTTHE JAVASCRIPT TASK RUNNER

Page 14: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

var grunt = require('grunt');

module.exports = function () { grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-connect');

grunt.registerTask('dev', ['watch', 'connect:dev']); grunt.registerTask('default', ['dev']);

grunt.initConfig({ watch: { src: { files: ['js/**/*.js', '!js/**/*.test.js'] }}, connect: { dev: { options: { port: 9000 }}}});};

SHORTEN DEV-LIFECYCLEWITH LIVERELOAD

Page 15: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

var indexHtmlTask = function () { var jsFiles = utils.filterForJS(this.filesSrc);

grunt.file.copy('src/index.html', 'build-output/index.html', { process: function (contents) { return grunt.template.process(contents, { data: { scripts: jsFiles }}); } });};

grunt.registerMultiTask('indexHtml', 'Process index.html template', indexHtmlTask);

<% scripts.forEach(function ( file ) { %><script type="text/javascript" src="<%= file %>"></script><% }); %>

indexHtml: { dev: { files: ['js/**.*.js', '!js/**/*.test.js'] }}

INCLUDE JAVASCRIPT FILESWITH CUSTOM TASK

Page 16: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Substitute for CompilerSyntax CheckCoding Rules ValidationStatic Code Analysis

watch: { src: { files: ['js/**/*.js', '!js/**/*.test.js'], tasks: ['jshint:src'] }}

A JAVASCRIPT CODE QUALITY TOOL

Page 17: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

TestingSASS and/or LESS SupportUsing Mock-Data During DevelopmentRunning Frontend with Real Backend

BUT WHAT ABOUT

Page 18: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Karma & grunt-karma for Unit/Spec TestsProtractor & grunt-protractor for End-2-End Testsgrunt-contrib-sass & grunt-contrib-less

NPM Packages starting with grunt-*Plugin Overview at gruntjs.com/plugins (http://gruntjs.com/plugins)

More than 2800 Plugins, but a Lot of Duplicates

NO PROBLEMTHERE ARE PLUGINS FOR EVERYTHING

Page 19: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

ConcatenationMinificationCache BustingRunning Tests Against Minified Code

THERE IS STILL MORE TO DOBEFORE WE CAN DEPLOY OUR APP

Page 20: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Project Specific BuildHuge Gruntfile.jsTask DependenciesHard to Do It RightSame Problems as with Ant

THE REAL PROBLEMS

Page 21: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

github.com/w11k/fabs (https://github.com/w11k/fabs)

Inspired by ng-boilerplate (https://github.com/ngbp/ngbp)

Based on Our Best PracticesConfigurableExtendable via Lifecycle-HooksGrunt Based

FABSFABULOUS ANGULARJS BUILD SYSTEM

Page 22: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Manage Frontend Dependencies with BowerDev-Mode with Server, Proxies and LiveReloadSASS and LESS SupportSpec and End-2-End TestsMocks for Tests and Dev-ModeProject- and Developer-ConfigurationBuild and Serve Distribution incl. Cache Busting

FEATURES

Page 23: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

fabs-boilerplate (https://github.com/w11k/fabs-boilerplate)

This PresentationReal World Projectfabs (https://github.com/w11k/fabs)

DEMO

Page 24: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Highly Interdependent TasksHard to MaintainHard to Extend (Even With Hooks)Grunt Is Slow (Slower than ...)

FABS & GRUNT PROBLEMS

Page 25: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

gulpjs.com (http://gulpjs.com)

Uses Streams Instead fo Temp FilesRuns Independent Tasks 'Parallel'Code Over ConfigurationNode Package: Install with npm install -g gulpPlugins Are Regular Node Packages

GULP.JS

Page 26: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

var gulp = require('gulp'), rename = require('gulp-rename'), concat = require('gulp-concat'), uglify = require('gulp-uglify');

gulp.task('process-scripts', function() { return gulp.src('src/scripts/*.js') .pipe(concat('1.0.1/main.js')) .pipe(gulp.dest('dest/scripts/')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('dest/scripts/'))});

gulp.task('watch', function() { gulp.watch('src/scripts/*.js', ['process-scripts'])});

PROCESS JAVASCRIPT WITH GULP

Page 27: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

A Lot of Small Tools / Plugins, Each for a Specific TaskDifficult to Keep Track of All These Tools / PluginsNo De-Facto Standard Build Tool like Maven for JavaImpossible to Write One for All Web-Appsfabs for AngularJS ApplicationsWorking on a Gulp Port of fabs

CONCLUSION

Page 28: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

MEET THE SPEAKER

@ Speaker LoungeSecond Floor Directly About Reception

Page 29: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications

Philipp [email protected]

www.w11k.com (http://www.w11k.com)

www.thecodecampus.de (http://www.thecodecampus.de)

Page 30: Grunt, Gulp & fabs: Build Systems and Development-Workflow for Modern Web-Applications