Introduction to Grunt.js on Taiwan JavaScript Conference

153
你不可不知的前端開發工具 2013 JavaScript Conference 5/18,19

description

Blog: http://blog.wu-boy.com/2013/05/2013-javascript-conference-front-tool-grunt-js/ 高雄 KSDG 分享: 打造團隊共同開發環境 http://blog.wu-boy.com/2013/05/how-to-integrate-front-end-tools-via-gruntjs/

Transcript of Introduction to Grunt.js on Taiwan JavaScript Conference

Page 1: Introduction to Grunt.js on Taiwan JavaScript Conference

你不可不知的前端開發工具2013 JavaScript Conference 5/18,19

Page 2: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 2

Who am I

Bo-Yi Wu@appleboyhttp://blog.wu-boy.comhttps://github.com/appleboy任職於瑞昱半導體RealTek(IC Design House)

- TV多媒體部門

- Backbone.js, CodeIgniter, Larvel Node.js, MongoDB, Mariadb MySQL, Twitter Bootstrap, Handlebars.js, HAProxy for load balancer, Galera Cluster for MySQL ...

Page 3: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 3

工欲善其事 ,必先利其器

Page 4: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 4

昨天UP Chen講了七的工具

Page 5: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 5

今天來聊聊程式相關套件工具

Page 6: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 6

寫後端程式不再是PHP,Ruby,Pyhton..等專屬我們可以用我們可以用Node.jsNode.js取而代之取而代之

Page 7: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 7

Node.js發行速度快API常常更新

Page 8: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 8

該如何管理該如何管理 Node.jsNode.js版本

Page 9: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 9

Node Version Manager: nvmnvmhttps://github.com/creationix/nvm

Page 10: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 10

How to use?

Page 11: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 11

How to use

● nvm install 0.10.5● nvm ls● nvm ls-remote● nvm use 0.10.5● nvm install stable (support from my github)● nvm install latest (support from my github)

https://github.com/appleboy/nvm

Page 12: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 12

前端工程師會接觸到JS,CSS,htmlJS,CSS,html

Page 13: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 13

先來看看一般專案首頁index.html

Page 14: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 14

Page 15: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 15

看看header載入哪些檔案

Page 16: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 16

Popular Open Source

Normalize.css

Page 17: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 17

升級套件版本很頭痛

Page 18: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 18

如果載入了 10個套件就必須手動更新 10次

Page 19: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 19

引入套件管理程式

Page 20: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 20

package manager for the web: BowerBower

http://bower.io/

Page 21: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 21

定義下載路徑設定 : .bowerrc

Page 22: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 22

{ 'directory': 'assets/vendor', 'json': 'component.json'}

Page 23: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 23

定義專案套件需求 : component.json

$ bower install <pkg>#<version>

Page 24: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 24

{ "name": "html5-template-engine", "version": "1.0.0", "dependencies": { "jquery": "~1.9.1", "normalize-scss": "~2.1.1", "modernizr": "~2.6.2", "requirejs": "~2.1.5" }}

Page 25: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 25

以後不必擔心套件版本問題

Page 26: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 26

JavaScript產生器 :CoffeeScript

Page 27: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 27

為什麼要使用CoffeeScript

Page 28: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 28

Why Use CoffeeScript?

● 解決 JavaScript Coding Style問題– 讓 Javascript看起來像是同一個人寫的

● 通過 Javascript Lint測試– http://www.javascriptlint.com/

Page 29: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 29

不用再宣告 var變數

CoffeeScript# Assignment:number = 42opposite = true

JavaScriptvar number, opposite;number = 42;opposite = true;

Page 30: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 30

不用再使用任何括號

CoffeeScript# Conditions:number = -42 if opposite# Functions:square = (x) -> x * x

JavaScriptif (opposite) { number = -42;

}

square = function(x) { return x * x;};

Page 31: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 31

coffee -b -w -c -o assets/js assets/coffee

Page 32: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 32

CSS產生器 :Compass

Page 33: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 33

為什麼要使用 compass

Page 34: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 34

Why Use Compass?

● 解決跨瀏覽器 CSS Hack– IE瀏覽器還是要手動 Hack

● 支援 CSS Sprite– 不需要靠 designer合併圖檔 ,減少 network

request● 撰寫 CSS3非常容易

– Box, Shadow, Border Radius, Text Shadow.. etc● 可自行定義 function, Variable … etc.

Page 35: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 35

SCSS/SASS選擇Scss跟 CSS長的一樣

Sass對於設計師而言很難學

Page 36: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 36

無痛轉移原有 CSS架構Sass-convert -F css -T scss your.css new.css

Sass-convert -F css -R css_folder_path

Page 37: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 37

隨時監控 scss狀態compass watch scss_folder_path

Page 38: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 38

Make CSS3 so Easy

Page 39: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 39

Border radius

SCSS#border-radius { @include border-radius(25px); }

CSS#border-radius {

-webkit-border-radius: 25px;

-moz-border-radius: 25px;

-ms-border-radius: 25px;

-o-border-radius: 25px;

border-radius: 25px;

}

Page 40: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 40

Box-shadow

SCSS#box-shadow-default {

@include single-box-shadow; }

CSS#box-shadow-default { -webkit-box-shadow: 0px 0px 5px #333333; -moz-box-shadow: 0px 0px 5px #333333; box-shadow: 0px 0px 5px #333333;}

Page 41: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 41

Compass Assets設定config.rb

Page 42: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 42

background-image

Page 43: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 43

config.rb: images path

SCSS#logo {

backgroung-image: image-url('logo.png'); }

CSS

http_path = "/"relative_assets = true

#logo { backgroung-image: url('../images/logo.png?1293690212'); }

Page 44: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 44

config.rb: images path

SCSS#logo {

backgroung-image: image-url('logo.png'); }

CSS

http_path = "/"relative_assets = false

#logo { backgroung-image: url('/images/logo.png?1293690212'); }

Page 45: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 45

inline-image

Page 46: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 46

config.rb: images path

SCSS#logo {

backgroung-image: inline-image('logo.png'); }

CSS#logo { backgroung-image: url('data:image/png;base64,xxxxxxxxxx'); }

Page 47: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 47

Image-SpritesRef: google sprites

Page 48: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 48

Image-Sprites

SCSS@import "my-icons/*.png";@include all-my-icons-sprites;

CSS.my-icons-sprite,my-icons-edit, .my-icons-save { background: url('/images/my-icons-s34fe0604ab.png') no-repeat; }.my-icons-edit { background-position: 0 0; }.my-icons-save { background-position: 0 -32px; }

Page 49: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 49

JavaScritp模組工具 :RequireJS

Page 50: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 50

為什麼要使用RequireJS

Page 51: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 51

Page 52: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 52

網站 include超多外部套件

Page 53: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 53

每個套件都有相依性問題

Page 54: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 54

RequireJS解決相依性問題

Page 55: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 55

main.js

Page 56: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 56

app.js

Page 57: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 57

<script data-main="js/main" src="js/require.js"></script>

取代全部 javascript tag

Page 58: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 58

index.html

Page 59: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 59

RequireJS只有這樣而已?

Page 60: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 60

作者另外開發的編譯工具 :r.jshttps://github.com/jrburke/r.js/

Page 61: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 61

使用前

Page 62: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 62

使用後

r.js -o build/app.build.js

Page 63: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 63

前端必備工具 : Livereloadhttp://livereload.com/

Page 64: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 64

雙螢幕寫程式必備工具

Page 65: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 65

每天按Ctrl+F5至少 100次保守估計

Page 66: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 66

Guardfile設定檔

Page 67: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 67

# A sample Guardfile# More info at https://github.com/guard/guard#readme

guard 'livereload' do watch(%r{app/.+\.(html|htm)$}) watch(%r{app/assets/css/.+\.css}) watch(%r{app/assets/js/.+\.js}) watch(%r{app/assets/templates/.+\.handlebars})end

Page 68: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 68

簡易Web Server: Node Expresshttp://expressjs.com/

Page 69: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 69

不需要安裝Apache,Nginx,lighttpdhttp://expressjs.com/

Page 70: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 70

var app, express, fs, port;fs = require('fs');express = require('express');app = express();port = 4000;app.use(express["static"](__dirname + '/'));app.use(express.directory(__dirname + '/'));app.use(express.errorHandler());

app.use(function(req, res, next) { console.log('%s %s', req.method, req.url); return next();});

app.use(app.router);app.listen(port);console.log('Server listening on http://localhost:' + port);

Page 71: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 71

上述工具重點整理

Page 72: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 72

Bower, Compass ...

● bower install● compass watch .● coffee -b -w -c -o js/ coffeescript/● r.js -o build/app.build.js● node build/server.js● guard start

Page 73: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 73

指令有點多有點雜

Page 74: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 74

寫成Makefile執行

Page 75: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 75

build:r.js -o build/app.build.js

compass:compass watch .

coffee:coffee -b -w -c -o js/ coffeescript/

livereload:guard start

all: compass coffee livereload

Page 76: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 76

好像有好一點?

Page 77: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 77

但是對Windows開發環境?

Page 78: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 78

Page 79: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 79

What is Fucking Makefile?WTF

Page 80: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 80

為了解決環境相容問題

Page 81: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 81

JavaScript Task RunnerGrunt.js

Page 82: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 82

Why Use Grunt.js● Define Task Runner

– Initial Project– Deploy Project– Unit Test Project

● Designer Don't learning command line● Many Available Grunt plugins

– CoffeeScript, Compass, Jade, Require.js– Twitter Bower, JSHint, CSSMin, Livereload

Page 83: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 83

Grunt 0.4.x requires Node.js version >= 0.8.0.

npm uninstall -g gruntnpm install -g grunt-cli

Grunt 0.4.x requires Node.js version >= 0.8.0.

Page 84: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 84

How the Grunt CLI works?

Page 85: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 85

package.json && Gruntfile.js

Page 86: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 86

package.jsonnpm init建立此檔案

npm install grunt-cli --save-devnpm init建立此檔案

Page 87: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 87

Gruntfile.js or Gruntfile.coffeegrunt.js for 0.3.x versions of Grunt.

Page 88: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 88

Gruntfile.js包含底下項目

● The "wrapper" function● Project and task configuration

– compass, require.js, bower, shell …. etc.● Loading grunt plugins and tasks● Custom tasks

– Deploy, Clean, Build project … etc.

Page 89: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 89

開始撰寫Gruntfile.js

Page 90: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 90

The "wrapper" function

Page 91: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 91

module.exports = function(grunt) { // Do grunt-related things in here};

Page 92: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 92

Project and task configuration

Page 93: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 93

grunt.initConfig({ pkg: project_config, shell: { bower: { command: 'node node_modules/.bin/bower install', options: { stdout: true, stderr: true, callback: function(err, stdout, stderr, cb) { console.log('Install bower package compeletely.'); return cb(); } } } }});

Page 94: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 94

Loading grunt plugins and tasks

Page 95: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 95

grunt.loadNpmTasks('grunt-shell');

Page 96: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 96

Custom tasks

Page 97: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 97

// Default task(s).grunt.registerTask('default', ['connect', 'watch']);

// Deploy task(s).grunt.registerTask('release', ['htmlmin', 'cssmin']);

Page 98: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 98

Working with an existing grunt project

Page 99: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 99

An existing grunt project

● Change to the project's root directory.● Install project dependencies with npm

install.● Run Grunt with grunt.

Page 100: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 100

用Grunt整合今日介紹工具

Page 101: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 101

package manager: BowerBowerhttp://bower.io/

https://github.com/yatskevich/grunt-bower-task

Page 102: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 102

Bower只會在專案初始化執行

Page 103: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 103

bower: { install: { options: { cleanup: false, install: true, verbose: true, copy: false } }}

Page 104: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 104

bower: { cleanup: { options: { cleanup: true, install: false, verbose: true, copy: false } }}

Page 105: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 105

$ grunt bower:install$ grunt bower:cleanup

Page 106: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 106

JavaScript產生器 :CoffeeScript

https://github.com/gruntjs/grunt-contrib-coffee

Page 107: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 107

coffee: { dev: { expand: true, cwd: 'app/assets/coffee/', src: ['**/*.coffee'], dest: 'app/assets/js/', ext: '.js', options: { bare: true } }}

Page 108: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 108

$ grunt coffee:dev

Page 109: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 109

CSS產生器 :Compass

Page 110: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 110

不需要 config.rb設定檔

Page 111: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 111

compass: { dev: { options: { sassDir: 'app/assets/sass', cssDir: 'app/assets/css', imagesDir: 'app/assets/images', javascriptsDir: 'app/assets/js', outputStyle: 'nested', relativeAssets: true, noLineComments: true, environment: 'development' } }}

Page 112: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 112

$grunt compass:dev

Page 113: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 113

前端必備工具 : Livereloadhttp://livereload.com/

https://github.com/gruntjs/grunt-contrib-livereload

Page 114: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 114

Setup Serverhttps://github.com/gruntjs/grunt-contrib-connect

Page 115: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 115

connect: { livereload: { options: { hostname: '0.0.0.0', port: 3000, base: '.' } }}

Page 116: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 116

Run tasks whenever watched files change.

https://github.com/gruntjs/grunt-contrib-livereload

Page 117: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 117

regarde: { html: { files: ['app/**/*.{html,htm}'], tasks: ['livereload'] }, scss: { files: ['app/**/*.scss'], tasks: ['compass:dev'] }, css: { files: ['app/**/*.css'], tasks: ['livereload'] }, js: { files: 'app/**/*.js', tasks: ['livereload'] }, coffee: { files: '**/*.coffee', tasks: ['coffee'] }}

Page 118: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 118

grunt.registerTask('init', ['livereload-start', 'connect', 'regarde'])

$ grunt init

Page 119: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 119

Page 120: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 120

專案開發環境講完

Page 121: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 121

Deploy Your Project

Page 122: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 122

這不是後端工程師該做的嘛?

Page 123: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 123

很抱歉台灣老闆不是這樣想

Page 124: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 124

Ref: http://goo.gl/PKIr4

Page 125: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 125

Before Deploying Project

● JavaScript Minify and Combine (requirejs)● CSS Minify (cssmin)● Html Minify (htmlmin)● Remove unnecessary files (clean)● Copy files (copy)

Page 126: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 126

JavaScript Minify and Combinehttps://github.com/asciidisco/grunt-requirejs

Page 127: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 127

requirejs: {  release: {    options: {      appDir: "app/",      baseUrl: "assets/js/",      dir: "public",      name: "main",      mainConfigFile: 'app/assets/js/main.js',      preserveLicenseComments: false,      fileExclusionRegExp: /^(\.|node_modules)/,      paths: {        jquery: '../vendor/jquery/jquery'      }    }  }}

Page 128: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 128

$ grunt requirejs:release

Page 129: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 129

CSS Minifyhttps://github.com/gruntjs/grunt-contrib-cssmin

Page 130: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 130

cssmin: { release: { report: 'gzip', expand: true, cwd: 'app/assets/css', src: ['*.css'], dest: 'app/assets/css' }}

Page 131: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 131

$ grunt cssmin:release

Page 132: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 132

Html Minifyhttps://github.com/gruntjs/grunt-contrib-htmlmin

Page 133: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 133

htmlmin: { options: { removeComments: true, collapseWhitespace: true }, release: { files: { 'public/index.html': 'app/index.html' } }}

Page 134: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 134

$ grunt htmlmin:release

Page 135: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 135

Remove unnecessary fileshttps://github.com/gruntjs/grunt-contrib-clean

Page 136: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 136

clean: { options: { force: true }, release: ['app/assets/coffee', 'app/.sass-cache']}

Page 137: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 137

$ grunt clean:release

Page 138: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 138

Copy fileshttps://github.com/gruntjs/grunt-contrib-copy

Page 139: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 139

copy: { release: { files: [ { src: 'app/js/main-built.js', dest: 'public/js/20130519.js' } ] }}

Page 140: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 140

$ grunt copy:release

Page 141: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 141

今日所有整合都在Githubhttps://github.com/appleboy/html5-template-engine

Page 142: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 142

Html5 Template Enginehttps://github.com/appleboy/html5-template-engine

Page 143: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 143

Features

● The latest html5boilerplate.com source code● Includes Normalize.scss v2.1.x and v1.1.x.● The latest jQuery and Modernizr.● Support CoffeeScript, RequireJS, Compass● A lightweight web server listen to 3000 port● Support JavaScript Task Runner GruntJS● Support JavaScript test framework Mocha

Page 144: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 144

歡迎Fork打造自己的開發環境https://github.com/appleboy/html5-template-engine

Page 145: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 145

另外有Backbone開發環境https://github.com/appleboy/backbone-template-engine

Page 146: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 146

兩個Repository差異在哪?https://github.com/appleboy/backbone-template-engine

Page 147: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 147

Backbone.js+Handlebar.jshttps://github.com/appleboy/backbone-template-engine

Page 148: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 148

或者你只需要minify html css javascript

Page 149: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 149

可以參考Shell Script: Minify toolhttps://github.com/appleboy/minify-tool

Page 150: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 150

Features

● JavaScript compressor: UglifyJS● CSS compressor: Sqwish● Html compressor: htmlcompressor● Optimize images: image_optim

Page 151: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 151

$ minify (folder|file)_path

Page 152: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 152

Live Demo

Page 153: Introduction to Grunt.js on Taiwan JavaScript Conference

2013 JSDC 153

謝謝大家及工作團隊