Top Banner
Automating Your Work flow Automating Your Work flow with gulp.js with gulp.js Bo-Yi Wu 2014@COSCUP
94

Automating your workflow with Gulp.js

Sep 08, 2014

Download

Technology

Bo-Yi Wu

Automating your workflow with gulp
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Automating your workflow with Gulp.js

Automating Your Work flow Automating Your Work flow with gulp.jswith gulp.js

Bo-Yi Wu2014@COSCUP

Page 2: Automating your workflow with Gulp.js

2

Software Engineer @ Realtekhttps://github.com/appleboy

http://blog.wu-boy.comhttps://www.facebook.com/appleboy46

Page 3: Automating your workflow with Gulp.js

3

Work FlowWork Flow

https://www.flickr.com/photos/shanecasey51/14627365291

Page 4: Automating your workflow with Gulp.js

4

Work FlowWork Flowfor web developerfor web developer

Page 5: Automating your workflow with Gulp.js

5

Work Flow Setup

DownloadServer SetupPHPRuby

Page 6: Automating your workflow with Gulp.js

6

SetupDownloadServer SetupPHPRuby

Develop Watch Live Reload Lint Coffee Sass/Less

Work Flow

Page 7: Automating your workflow with Gulp.js

7

SetupDownloadServer SetupPHPRuby

Develop Watch Live Reload Lint Coffee Sass/Less

Build Testing Compile Concat Rename Minify Image-min Deployment

Work Flow

Page 8: Automating your workflow with Gulp.js

8

Automation

http://www.exacttarget.com/blog/marketing-automation-infographic/

Page 9: Automating your workflow with Gulp.js

9

TasksTasks

Html / JadeJavaScript / CoffeeScript / LiveScript

– JSHintCSS / Sass / Compass / StylusTesting

– MochaDeploy

– Minify– Rename– Replace

Page 10: Automating your workflow with Gulp.js

10

We need We need JavaScript Task RunnerJavaScript Task Runner

Page 11: Automating your workflow with Gulp.js

11

2013 @ JSDC Conference你不可不知的前端開發工具http://bit.ly/jsdc-grunt

Page 12: Automating your workflow with Gulp.js

12

build system for Grunt.js

Page 13: Automating your workflow with Gulp.js

13

Gulp.js Another streaming build system

Page 14: Automating your workflow with Gulp.js

14

Who Use Gult.js?Who Use Gult.js?

Page 15: Automating your workflow with Gulp.js

15

https://github.com/gulpjs/gulp/issues/540

Page 16: Automating your workflow with Gulp.js

16

Page 17: Automating your workflow with Gulp.js

17

VS.VS.

Gulp.jsGrunt.js

Page 18: Automating your workflow with Gulp.js

18

VS.VS.

Gulp.js

File Based Stream Based

Grunt.js

Page 19: Automating your workflow with Gulp.js

19

VS.VS.

Gulp.js

File Based Stream BasedConfiguration over code code over Configuration

Grunt.js

Page 20: Automating your workflow with Gulp.js

20

File Based vs. Stream Based

Page 21: Automating your workflow with Gulp.js

21

Typical grunt taskTypical grunt task

FileSystem

ReadFiles

Modify WriteFiles

Temp

ReadFiles

Modify WriteFiles

Temp

FileSystem

Page 22: Automating your workflow with Gulp.js

22

GulpGulp uses streams

FileSystem

ReadFiles

Modify Modify WriteFiles

Modify Modify

FileSystem

Page 23: Automating your workflow with Gulp.js

23

Why use gulp.js

Easy to useEfficientHigh QualityEasy to Learn

Page 24: Automating your workflow with Gulp.js

24

Easy to useEasy to useBy preferring code over configuration, gulp

keeps simple things simple and makes complex tasks manageable.

Page 25: Automating your workflow with Gulp.js

25

Easy to useEasy to use

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);

Page 26: Automating your workflow with Gulp.js

26

Easy to useEasy to use

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);gulp.task('coffee', function() { //ready to go});

Page 27: Automating your workflow with Gulp.js

27

Easy to useEasy to use

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);gulp.task('coffee', function() { gulp.src(‘assets/coffee/**/*.coffee’) .pipe(coffee()) .pipe(gulp.dest(‘assets/js/’));});

Page 28: Automating your workflow with Gulp.js

28

https://www.flickr.com/photos/53382811@N07/4984100421

Page 29: Automating your workflow with Gulp.js

29

Run commandRun command$ gulp coffee

Page 30: Automating your workflow with Gulp.js

30

EfficientEfficientBy harnessing the power of node's streams you get fast builds that don't write intermediary files to

disk.

Page 31: Automating your workflow with Gulp.js

31

Gulp uses streams

FileSystem

ReadFiles

coffee uglify WriteFiles

rename

FileSystem

Page 32: Automating your workflow with Gulp.js

32

Efficient

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);gulp.task('coffee', function() { gulp.src(‘assets/coffee/main.coffee’) .pipe(coffee()) .pipe(uglify()) .pipe(rename(‘assets/js/main.min.js’)) .pipe(gulp.dest(‘dist/assets/js/’));});

Read file

Page 33: Automating your workflow with Gulp.js

33

Efficient

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);gulp.task('coffee', function() { gulp.src(‘assets/coffee/main.coffee’) .pipe(coffee()) .pipe(uglify()) .pipe(rename(‘assets/js/main.min.js’)) .pipe(gulp.dest(‘dist/assets/js/’));});

Process in Memory

Process in Memory

Process in Memory

Page 34: Automating your workflow with Gulp.js

34

Efficient

var gulp = require('gulp‘), coffee = require(‘gulp-coffee’);gulp.task('coffee', function() { gulp.src(‘assets/coffee/main.coffee’) .pipe(coffee()) .pipe(uglify()) .pipe(rename(‘assets/js/main.min.js’)) .pipe(gulp.dest(‘dist/assets/js/’));});

Write file

Page 35: Automating your workflow with Gulp.js

35

stream-handbookstream-handbook By By@@ substacksubstack

https://github.com/substack/stream-handbook

Page 36: Automating your workflow with Gulp.js

36

High QualityHigh Qualitygulp's strict plugin guidelines assure plugins stay simple and work the way

you expect.

Page 37: Automating your workflow with Gulp.js

37

Gulp pluginsGulp pluginsmore than 600+ pluginsmore than 600+ plugins

http://gulpjs.com/plugins/http://gulpjs.com/plugins/

Page 38: Automating your workflow with Gulp.js

38

Gulp plugin

gulp-coffee / gulp-livescriptgulp-ruby-sass / gulp-compassgulp-imagemingulp-renamegulp-minify-cssgulp-htmlmingulp-mochagulp-replacegulp-livereload

Page 39: Automating your workflow with Gulp.js

39

gulp black list

gulp-browserify gulp-requirejs gulp-shell gulp-connect gulp-twitter gulp-if-else

https://github.com/gulpjs/plugins/blob/master/src/blackList.json

Page 40: Automating your workflow with Gulp.js

40

Easy to LearnEasy to LearnWith a minimal API surface, you can pick up gulp in no time. Your

build works just like you envision it: a series of streaming pipes.

Page 41: Automating your workflow with Gulp.js

41

Only 4 functions you need to learnOnly 4 functions you need to learn

Page 42: Automating your workflow with Gulp.js

42

Really?

https://www.flickr.com/photos/kplawver/8238939/

Page 43: Automating your workflow with Gulp.js

43

4 functions

gulp.taskgulp.watchgulp.srcgulp.src

Page 44: Automating your workflow with Gulp.js

44

gulp.task(name[, deps], fn)gulp.task(name[, deps], fn)gulp.task(‘hello', function() { console.log('Hello world!'); });gulp.task('css', ['greet'], function () { // Deal with CSS here }); gulp.task('build', ['css', 'js', 'imgs']); gulp.task('default', function () { // Your default task });

Page 45: Automating your workflow with Gulp.js

45

gulp.watch(glob[, opts], tasks)

gulp.watch('js/**/*.js', ['Task']);

Page 46: Automating your workflow with Gulp.js

46

gulp.src(globs[, options])

gulp.src('./*.jade') gulp.src('*.+(js|css)')gulp.src('*.{jpg,jpeg,png,gif}')gulp.src(['js/**/*.js', '!js/**/*.min.js'])

Page 47: Automating your workflow with Gulp.js

47

gulp.dest(path)

gulp.src('./client/templates/*.jade') .pipe(jade()) .pipe(gulp.dest('./app')) .pipe(minify()) .pipe(gulp.dest('./dist'));

Page 48: Automating your workflow with Gulp.js

48

Write your first Gulp TaskWrite your first Gulp Taskgulpfile.[js|coffee|ls]

Support CoffeeScript or LiveScript from Gulp > 3.7.0

Thanks @tkellen

https://github.com/tkellen/node-liftoff

Page 49: Automating your workflow with Gulp.js

49

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

gulp.task('js', function () { return gulp.src('js/*.js') .pipe(jshint()) .pipe(jshint.reporter('default')) .pipe(uglify()) .pipe(concat('app.js')) .pipe(gulp.dest('build'));});

Page 50: Automating your workflow with Gulp.js

50

package.json

{ "devDependencies": { "gulp-concat": "^2.3.3", "gulp-uglify": "^0.3.1", "gulp-jshint": "^1.7.1", "gulp": "^3.8.6" }}

Page 51: Automating your workflow with Gulp.js

51

How many pluginsHow many pluginsyou need?you need?

Page 52: Automating your workflow with Gulp.js

52

gutil = require 'gulp-util'coffee = require 'gulp-coffee'coffeelint = require 'gulp-coffeelint'compass = require 'gulp-compass'w3cjs = require 'gulp-w3cjs'imagemin = require 'gulp-imagemin'cache = require 'gulp-cache'changed = require 'gulp-changed'connect = require 'gulp-connect'size = require 'gulp-size'gulpif = require 'gulp-if'rename = require 'gulp-rename'uglify = require 'gulp-uglify'minifyCSS = require 'gulp-minify-css'htmlmin = require 'gulp-htmlmin'replace = require 'gulp-replace'mocha = require 'gulp-mocha'

Page 53: Automating your workflow with Gulp.js

53

You needgulp-load-plugins

Automatically load in gulp pluginshttps://github.com/jackfranklin/gulp-load-plugins

Page 54: Automating your workflow with Gulp.js

54

var gulp = require('gulp'), $ = require('gulp-load-plugins')();

gulp.task('js', function () { return gulp.src('js/*.js') .pipe($.jshint()) .pipe($.jshint.reporter('default')) .pipe($.uglify()) .pipe($.concat('app.js')) .pipe(gulp.dest('build'));});

Page 55: Automating your workflow with Gulp.js

55

Only pass through changed files

By using gulp-changed only changed files will be passed through.

Page 56: Automating your workflow with Gulp.js

56

gulp.task('coffee', function() { gulp.src('app/coffee/**/*.coffee') .pipe(coffeelint()) .pipe(coffeelint.reporter()) .pipe(coffee()) .pipe(gulp.dest(paths.script));});

Page 57: Automating your workflow with Gulp.js

57

Using gulp-changedUsing gulp-changed

Page 58: Automating your workflow with Gulp.js

58

gulp.task('coffee', function() { gulp.src('app/coffee/**/*.coffee') .pipe(changed(paths.script, {extension: '.js'})) .pipe(coffeelint()) .pipe(coffeelint.reporter()) .pipe(coffee()) .pipe(gulp.dest(paths.script));});

Page 59: Automating your workflow with Gulp.js

59

Running tasks in seriesRunning tasks in series

Page 60: Automating your workflow with Gulp.js

60

var gulp = require('gulp');

gulp.task('one', function(cb) { // do stuff -- async or otherwise cb(err);});gulp.task('two', ['one'], function() { // task 'one' is done now});gulp.task('default', ['one', 'two']);

Page 61: Automating your workflow with Gulp.js

61

Using run-sequenceUsing run-sequenceRun a series of dependent gulp tasks in order

https://github.com/OverZealous/run-sequence

Page 62: Automating your workflow with Gulp.js

62

var runs = require('run-sequence‘);gulp.task('release', function(cb) { return runs(‘clean’, ['build', 'rjs'], cb);});

Page 63: Automating your workflow with Gulp.js

63

Reloading Changes In The BrowserReloading Changes In The Browser

gulp-livereloadhttps://github.com/vohof/gulp-livereload

Page 64: Automating your workflow with Gulp.js

64

var gulp = require('gulp'), sass = require('gulp-sass'), livereload = require('gulp-livereload'), watch = require('gulp-watch');

gulp.task(‘sass', function() { gulp.src(‘sass/*.scss') .pipe(watch()) .pipe(sass()) .pipe(gulp.dest('css')) .pipe(livereload());});

Page 65: Automating your workflow with Gulp.js

65

gulp-webservergulp-webserverStreaming gulp plugin to run a local webserver with

LiveReload

https://github.com/schickling/gulp-webserver

Page 66: Automating your workflow with Gulp.js

66

var gulp = require('gulp'); webserver = require('gulp-webserver');

gulp.task('webserver', function() { gulp.src('app') .pipe(webserver({ livereload: true }));});

Page 67: Automating your workflow with Gulp.js

67

BrowserSyncBrowserSyncTime-saving synchronised browser testing.

http://www.browsersync.io/

Page 68: Automating your workflow with Gulp.js

68

var browserSync = require('browser-sync'), reload = browserSync.reload;

// Watch Files For Changes & Reloadgulp.task('serve', function () { browserSync({ server: { baseDir: 'app' } });

gulp.watch(['*.html'], reload);});

Page 69: Automating your workflow with Gulp.js

69

Sharing Streams Sharing Streams with Stream Factorieswith Stream Factories

Page 70: Automating your workflow with Gulp.js

70

var gulp = require('gulp'), $ = require('gulp-load-plugins')();

gulp.task('bootstrap', function() { return gulp.src('bootstrap/js/*.js') .pipe($.jshint()) .pipe($.jshint.reporter(stylish)) .pipe($.uglify()) .pipe(gulp.dest('public/bootstrap'));});

gulp.task('coffee', function() { return gulp.src('lib/js/*.coffee') .pipe($.coffee()) .pipe($.jshint()) .pipe($.jshint.reporter(stylish)) .pipe($.uglify()) .pipe(gulp.dest('public/js'));});

Page 71: Automating your workflow with Gulp.js

71

uses lazypipe to get the job done

https://github.com/OverZealous/lazypipe

Page 72: Automating your workflow with Gulp.js

72

var gulp = require('gulp'), $ = require('gulp-load-plugins')(), lazypipe = require('lazypipe');

// give lazypipevar jsTransform = lazypipe() .pipe($.jshint) .pipe($.jshint.reporter, stylish) .pipe($.uglify);gulp.task('bootstrap', function() { return gulp.src('bootstrap/js/*.js') .pipe(jsTransform()) .pipe(gulp.dest('public/bootstrap'));});gulp.task('coffee', function() { return gulp.src('lib/js/*.coffee') .pipe($.coffee()) .pipe(jsTransform()) .pipe(gulp.dest('public/js'));});

Page 73: Automating your workflow with Gulp.js

73

Deploy ProcessDeploy Process

Page 74: Automating your workflow with Gulp.js

74

Deploy ProcessDeploy ProcessCoffeeScript / LiveScriptCompass / SassJadeImage-minRequireJSRenameReplaceCopy

Page 75: Automating your workflow with Gulp.js

75

Need more task runnerNeed more task runnerDevelopment

StagingProduction

Page 76: Automating your workflow with Gulp.js

76

Why not use Makefile?Why not use Makefile?Using Gulp.js tool only for development

Page 77: Automating your workflow with Gulp.js

77

Gulp.js generatorGulp.js generator

Page 78: Automating your workflow with Gulp.js

78

A gulp.js generator for modern webapps https://github.com/yeoman/generator-gulp-webapp

Page 79: Automating your workflow with Gulp.js

79

Web Starter Kithttps://developers.google.com/web/starter-kit/

Page 80: Automating your workflow with Gulp.js

80

Page 81: Automating your workflow with Gulp.js

81

SlushThe streaming scaffolding systemGulp as a replacement for Yeoman

http://slushjs.github.io

Page 82: Automating your workflow with Gulp.js

82

slush-html5-template html5 template engine generator

https://github.com/appleboy/slush-html5-template

Page 83: Automating your workflow with Gulp.js

83

FeaturesFeatures● The latest html5boilerplate.com source code. ● Includes Normalize.scss v3.0.x and v1.1.x. ● The latest jQuery and Modernizr via Bower

package manager. ● Support CoffeeScript, RequireJS, Sass or

Compass, html minification (via htmlcompressor).

● Support JavaScript test framework Mocha. ● Support streaming build system Gulp. ● Support Minify PNG and JPEG images with

image-min. ● Support browser-sync Keep multiple browsers

& devices in sync when building websites.

Page 84: Automating your workflow with Gulp.js

84

How to install?How to install?$ npm install -g slush

$ npm install -g slush-html5-template

Page 85: Automating your workflow with Gulp.js

85

Scaffold your first projectScaffold your first project

$ mkdir app$ cd app && slush html5-template$ npm start

Page 86: Automating your workflow with Gulp.js

86

Page 87: Automating your workflow with Gulp.js

87

Gulp TechnologyGulp Technology

Page 88: Automating your workflow with Gulp.js

88

OrchestratorOrchestrator

A module for sequencing and executing tasks and dependencies in maximum concurrency

https://github.com/orchestrator/orchestrator

Page 89: Automating your workflow with Gulp.js

89

vinyl-fsvinyl-fsVinyl adapter for the file system

https://github.com/wearefractal/vinyl-fs

Page 90: Automating your workflow with Gulp.js

90

gulp-cheatsheet gulp-cheatsheet A cheatsheet for gulp.js

https://github.com/osscafe/gulp-cheatsheet

Page 91: Automating your workflow with Gulp.js

91

Page 92: Automating your workflow with Gulp.js

92

Page 93: Automating your workflow with Gulp.js

93

Any Question?

Page 94: Automating your workflow with Gulp.js

94

謝謝在場聽眾及全體 COSCUP工作人員