Top Banner
打造團隊共同開發環境 2013.05.25 KSDG 高雄軟體開發者社群
104

How to integrate front end tool via gruntjs

Aug 20, 2015

Download

Technology

Bo-Yi Wu
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: How to integrate front end tool via gruntjs

打造團隊共同開發環境2013.05.25 KSDG

高雄軟體開發者社群

Page 2: How to integrate front end tool via gruntjs

2013 KSDG 2

Who am I

Bo-Yi Wu @appleboyhttp://blog.wu-boy.comhttps://github.com/appleboy

任職於瑞昱半導體 RealTek(IC Design House)

Page 3: How to integrate front end tool via gruntjs

2013 KSDG 3

開發團隊

● 前端工程師

– UI, JavaScript, CSS Designer● 後端工程師

– PHP, Ruby, Python Enginner● 測試工程師

– Python, JavaScript Enginner

Page 4: How to integrate front end tool via gruntjs

2013 KSDG 4

工程師都有自己的Coding Style

Page 5: How to integrate front end tool via gruntjs

2013 KSDG 5

每次要改別人的程式碼都特別痛苦

Page 6: How to integrate front end tool via gruntjs

2013 KSDG 6

好的專案看起來就像是同一個人寫的

Page 7: How to integrate front end tool via gruntjs

2013 KSDG 7

統一團隊開發環境Windows, Linux, Mac

Page 8: How to integrate front end tool via gruntjs

2013 KSDG 8

減少建置環境時間

Page 9: How to integrate front end tool via gruntjs

2013 KSDG 9

前端工具Html,CSS,JavaScript

Page 10: How to integrate front end tool via gruntjs

2013 KSDG 10

前端工具CSS

Page 11: How to integrate front end tool via gruntjs

2013 KSDG 11

CSS 產生器Sass/Scss

Page 12: How to integrate front end tool via gruntjs

2013 KSDG 12

gem install compass

Page 13: How to integrate front end tool via gruntjs

2013 KSDG 13

前端工具JavaScript

Page 14: How to integrate front end tool via gruntjs

2013 KSDG 14

JavaScript 產生器

Page 15: How to integrate front end tool via gruntjs

2013 KSDG 15

npm install -g coffeescript

Page 16: How to integrate front end tool via gruntjs

2013 KSDG 16

網頁總是會用到很多套件jQuery,Backbone ...

Page 17: How to integrate front end tool via gruntjs

2013 KSDG 17

外部套件版本控管

Page 18: How to integrate front end tool via gruntjs

2013 KSDG 18

Package ManagerBowerBower

http://bower.io/

Page 19: How to integrate front end tool via gruntjs

2013 KSDG 19

npm install -g bower

Page 20: How to integrate front end tool via gruntjs

2013 KSDG 20

前端工具介紹到此

Page 21: How to integrate front end tool via gruntjs

2013 KSDG 21

後端工程師PHP,Ruby,Python

Page 22: How to integrate front end tool via gruntjs

2013 KSDG 22

PHP Coding StylePHP-FIGPSR-0,1,2

http://www.php-fig.org/

Page 23: How to integrate front end tool via gruntjs

2013 KSDG 23

自動修正Coding StylePHP-CS-Fixer

https://github.com/fabpot/PHP-CS-Fixer

Page 24: How to integrate front end tool via gruntjs

2013 KSDG 24

寫 Server 端不再是 PHP,Python

程式語言

Page 25: How to integrate front end tool via gruntjs

2013 KSDG 25

JavScript LanguageNode.jsNode.js

Page 26: How to integrate front end tool via gruntjs

2013 KSDG 26

Node.js 發行速度快

Page 27: How to integrate front end tool via gruntjs

2013 KSDG 27

管理 Node.js 版本

Page 28: How to integrate front end tool via gruntjs

2013 KSDG 28

Node Version Manager nvmnvm

https://github.com/creationix/nvm

Page 29: How to integrate front end tool via gruntjs

2013 KSDG 29

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 30: How to integrate front end tool via gruntjs

2013 KSDG 30

減少每天按 Ctrl+F5 次數

LiveReload

Page 31: How to integrate front end tool via gruntjs

2013 KSDG 31

整理上述工具 Command

Page 32: How to integrate front end tool via gruntjs

2013 KSDG 32

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

Page 33: How to integrate front end tool via gruntjs

2013 KSDG 33

要記住這麼多 Command該如何整合成單一指令呢?

Page 34: How to integrate front end tool via gruntjs

2013 KSDG 34

Makefile ?

Page 35: How to integrate front end tool via gruntjs

2013 KSDG 35

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 36: How to integrate front end tool via gruntjs

2013 KSDG 36

Windows 開發環境可以跑嘛 ?WTF

Page 37: How to integrate front end tool via gruntjs

2013 KSDG 37

有沒有更簡單的方法同時相容多種作業系統Windows,Linux,Mac

Page 38: How to integrate front end tool via gruntjs

2013 KSDG 38

JavaScript Task Runner

GruntJS

Page 39: How to integrate front end tool via gruntjs

2013 KSDG 39

Why Use GruntJS● 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 40: How to integrate front end tool via gruntjs

2013 KSDG 40

How the Grunt CLI works?

Page 41: How to integrate front end tool via gruntjs

2013 KSDG 41

只需要兩個設定檔

Page 42: How to integrate front end tool via gruntjs

2013 KSDG 42

package.json && Gruntfile.js

Page 43: How to integrate front end tool via gruntjs

2013 KSDG 43

package.json

npm install grunt-cli --save-dev

npm init 建立此檔案

Page 44: How to integrate front end tool via gruntjs

2013 KSDG 44

Gruntfile.js or Gruntfile.coffee

grunt.js for 0.3.x versions of Grunt.

Page 45: How to integrate front end tool via gruntjs

2013 KSDG 45

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 46: How to integrate front end tool via gruntjs

2013 KSDG 46

開始撰寫Gruntfile.js

Page 47: How to integrate front end tool via gruntjs

2013 KSDG 47

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 48: How to integrate front end tool via gruntjs

2013 KSDG 48

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

Page 49: How to integrate front end tool via gruntjs

2013 KSDG 49

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 50: How to integrate front end tool via gruntjs

2013 KSDG 50

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

Page 51: How to integrate front end tool via gruntjs

2013 KSDG 51

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 52: How to integrate front end tool via gruntjs

2013 KSDG 52

grunt.loadNpmTasks('grunt-shell');

Page 53: How to integrate front end tool via gruntjs

2013 KSDG 53

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 54: How to integrate front end tool via gruntjs

2013 KSDG 54

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

Page 55: How to integrate front end tool via gruntjs

2013 KSDG 55

用 Grunt 整合開發工具

Page 56: How to integrate front end tool via gruntjs

2013 KSDG 56

開發專案三步驟

● Initialize Project● Test Project● Deploy Project

Page 57: How to integrate front end tool via gruntjs

2013 KSDG 57

開發專案三步驟

● Initialize Project● Test Project● Deploy Project

Page 58: How to integrate front end tool via gruntjs

2013 KSDG 58

Initialize Project

● Bower install● Create server via express.● Livereload● Coffeescript and compass

Page 59: How to integrate front end tool via gruntjs

2013 KSDG 59

Initialize Project

● Bower install● Create server via express.● Livereload● Coffeescript and compass

Page 60: How to integrate front end tool via gruntjs

2013 KSDG 60

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

Page 61: How to integrate front end tool via gruntjs

2013 KSDG 61

$ grunt bower:install

Page 62: How to integrate front end tool via gruntjs

2013 KSDG 62

Initialize Project

● Bower install● Create server via express● Livereload● Coffeescript and compass

Page 63: How to integrate front end tool via gruntjs

2013 KSDG 63

express: { dev: { options: { script: 'build/server.js' } }}

Page 64: How to integrate front end tool via gruntjs

2013 KSDG 64

$ grunt express:dev

Page 65: How to integrate front end tool via gruntjs

2013 KSDG 65

不想自己寫server code

Page 66: How to integrate front end tool via gruntjs

2013 KSDG 66

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

Page 67: How to integrate front end tool via gruntjs

2013 KSDG 67

$ grunt connect:livereload

Page 68: How to integrate front end tool via gruntjs

2013 KSDG 68

Initialize Project

● Bower install● Create server via express.● Livereload● Coffeescript and compass

Page 69: How to integrate front end tool via gruntjs

2013 KSDG 69

livereload: { port: 35729}

Page 70: How to integrate front end tool via gruntjs

2013 KSDG 70

偵測檔案變化

Page 71: How to integrate front end tool via gruntjs

2013 KSDG 71

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

Page 72: How to integrate front end tool via gruntjs

2013 KSDG 72

Initialize Project

● Bower install● Create server via express.● Livereload● Coffeescript and compass

Page 73: How to integrate front end tool via gruntjs

2013 KSDG 73

regarde: { scss: { files: ['app/**/*.scss'], tasks: ['compass:dev'] }, coffee: { files: '**/*.coffee', tasks: ['coffee'] }}

Page 74: How to integrate front end tool via gruntjs

2013 KSDG 74

Compass Task

Page 75: How to integrate front end tool via gruntjs

2013 KSDG 75

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 76: How to integrate front end tool via gruntjs

2013 KSDG 76

Coffee Task

Page 77: How to integrate front end tool via gruntjs

2013 KSDG 77

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

Page 78: How to integrate front end tool via gruntjs

2013 KSDG 78

開發專案三步驟

● Initialize Project● Test Project● Deploy Project

Page 79: How to integrate front end tool via gruntjs

2013 KSDG 79

定義 Test 工作

Page 80: How to integrate front end tool via gruntjs

2013 KSDG 80

grunt.registerTask('test', ['release', 'shell:test', 'mocha_phantomjs']);

Page 81: How to integrate front end tool via gruntjs

2013 KSDG 81

$ grunt test

Page 82: How to integrate front end tool via gruntjs

2013 KSDG 82

開發專案三步驟

● Initialize Project● Test Project● Deploy Project

Page 83: How to integrate front end tool via gruntjs

2013 KSDG 83

上線前該做的事

Page 84: How to integrate front end tool via gruntjs

2013 KSDG 84

Before Deploying Project● JavaScript Minify and Combine

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

Page 85: How to integrate front end tool via gruntjs

2013 KSDG 85

JavaScript Minify and Combine

https://github.com/asciidisco/grunt-requirejs

Page 86: How to integrate front end tool via gruntjs

2013 KSDG 86

requirejs: { release: { options: { appDir: "app/", baseUrl: "assets/js/", dir: "public", name: "main", mainConfigFile: 'app/assets/js/main.js', paths: { jquery: '../vendor/jquery/jquery' } } }}

Page 87: How to integrate front end tool via gruntjs

2013 KSDG 87

$ grunt requirejs:release

Page 88: How to integrate front end tool via gruntjs

2013 KSDG 88

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

Page 89: How to integrate front end tool via gruntjs

2013 KSDG 89

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

Page 90: How to integrate front end tool via gruntjs

2013 KSDG 90

$ grunt cssmin:release

Page 91: How to integrate front end tool via gruntjs

2013 KSDG 91

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

Page 92: How to integrate front end tool via gruntjs

2013 KSDG 92

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

Page 93: How to integrate front end tool via gruntjs

2013 KSDG 93

$ grunt htmlmin:release

Page 94: How to integrate front end tool via gruntjs

2013 KSDG 94

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

Page 95: How to integrate front end tool via gruntjs

2013 KSDG 95

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

Page 96: How to integrate front end tool via gruntjs

2013 KSDG 96

$ grunt clean:release

Page 97: How to integrate front end tool via gruntjs

2013 KSDG 97

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

Page 98: How to integrate front end tool via gruntjs

2013 KSDG 98

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

Page 99: How to integrate front end tool via gruntjs

2013 KSDG 99

$ grunt copy:release

Page 100: How to integrate front end tool via gruntjs

2013 KSDG 100

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

Page 101: How to integrate front end tool via gruntjs

2013 KSDG 101

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

Page 102: How to integrate front end tool via gruntjs

2013 KSDG 102

● 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 4000

port● Support JavaScript Task Runner GruntJS● Support JavaScript test framework Mocha

Page 103: How to integrate front end tool via gruntjs

2013 KSDG 103

歡迎 Fork打造團隊開發環境

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

Page 104: How to integrate front end tool via gruntjs

2013 KSDG 104

Live Demo