Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
利用Grunt打造前端工作流
Search
hui.liu
June 29, 2013
Technology
1.2k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
利用Grunt打造前端工作流
hui.liu
June 29, 2013
More Decks by hui.liu
See All by hui.liu
Redux and React Server Rendering
hulufei
1
120
How to build an online IDE with React
hulufei
0
540
Other Decks in Technology
See All in Technology
MCP Appsを作ってみよう
iwamot
PRO
3
110
2026.06.13_AI時代に事業会社が「SIer出身エンジニア」を求める理由 / Why Businesses Seek Engineers with a System Integrator Background in the AI Era
jumtech
0
900
Rancherの紹介&Update情報(RancherJP Online Meetup #09)
yoshiyuki_kono
0
130
Ruby::Boxでできること、Refinementsでできること
joker1007
3
400
関西に縁あるMicrosoft MVPsが語るCopilotの未来
kasada
0
1.2k
新アーキテクチャ「TiDB X」解説とDedicated比較 TiDB Cloud Premiumのゲーム運用活用を検証
staffrecruiter
0
120
AgentGatewayを試してみたかった
tkikuchi
0
120
作って終わりにしない タイミーのセマンティックレイヤー育成の現在地
chanyou0311
1
590
noUncheckedIndexedAccess、3時間、1万円。 / noUncheckedIndexedAccess, 3 Hours, 10,000 JPY.
kaonavi
1
340
protovalidate-es を導入してみた
bengo4com
0
160
価格.comをAI駆動で全面刷新する ー 30年分の技術的負債を返し、次の30年の土台をつくる ー / AI Engineering Summit Tokyo 2026
tkyowa
50
56k
非定型業務をAI slackbotで自動化する ~ 社内要望を自動壁打ちするbotを作った ~/automating-ad-hoc-work-with-ai-slackbot
shibayu36
0
310
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
160
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
140
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Faster Mobile Websites
deanohume
310
31k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
380
The Curious Case for Waylosing
cassininazir
1
380
The SEO identity crisis: Don't let AI make you average
varn
0
480
For a Future-Friendly Web
brad_frost
183
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Transcript
利用Grunt打造前端工作流 刘辉 / @hulufei www.hulufei.com VIPSHOP ・ Front-End
前端的日常
F2E 开发 份内苦逼的事 F5 F5 Ctrl + F5 Ctrl +
F5 ....... 代码检查 测试 文件合并 文件压缩 发布 + csslint + jshint + test + concat css + concat js + min js + min css + min html + min image + rever(版本号)
Be A Ninja
None
None
= Workflow
专注偷懒二十年
None
None
Grunt Plugins contrib-coffee contrib-concat contrib-csslint contrib-jshint contrib-cssmin contrib-htmlmin contrib-imagemin contrib-qunit
contrib-uglify contrib-less and more http://gruntjs.com/plugins
None
Install & Start Using Grunt npm install -g grunt grunt-cli
+ Two Files Gruntfile.js packgage.json
Minify With Grunt
Install Plugin npm install grunt-contrib-uglify --save-dev
package.json { "name": "my-project-name", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.1",
"grunt-contrib-uglify": "~0.2.2" } } "grunt-contrib-uglify": "~0.2.2"
Gruntfile.js Gruntfile.coffee module.exports = -> @initConfig uglify: dist: src: 'src/myfile.js'
dest: 'dist/myfile.min.js' @loadNpmTasks 'grunt-contrib-uglify' @registerTask 'default', ['uglify'] module.exports = function(grunt) { // Project configuration. grunt.initConfig({ uglify: { build: { src: 'src/myfile.js' , dest: 'build/myfile.min.js' } } }); // Load the plugin grunt.loadNpmTasks( 'grunt-contrib-uglify' ); // Default task(s). grunt.registerTask( 'default', ['uglify']); }; 'default'
Run Task grunt uglify or grunt
None
Grunt Tasks • Basic Task • Multi Task
Basic Task grunt.registerTask('foo', 'optional description', function(arg1, arg2) { console.log(arg1); console.log(arg2);
}); No Configuration grunt foo:hello:world // => hello // => world registerTask
Multi Task // Configuration grunt.initConfig({ foo: { target1: { //
target1 options and files go here. }, target2: { // target2 options and files go here. } } }); grunt.registerMultiTask('foo', function() { grunt.log.writeln(this.target); grunt.log.writeln(this.data); }); grunt foo:target1 // => target1 // => {} registerMultiTask target1 target2
Files
Files - 通配符 • * 匹配任何字符,除了 / • ? 匹配单个字符,除了
/ • ** 匹配任何字符,包括 /,所以用在目录路径里面 • {} 逗号分割的“或”操作(逗号后面不要有空格) • ! 排除某个匹配 // foo目录下所有 th 开头的js文件 { src: 'foo/th*.js' , dest: ... } // foo目录下所有 a 或者 b 开头的js文件 { src: 'foo/{a,b}*.js' , dest: ... } // 也可以分开写成 这样 { src: ['foo/a*.js' , 'foo/b*.js' ], dest: ... } // foo目录下所有js文件 { src: ['foo/*.js'], dest: ... } // foo目录下所有js文件,除了bar.js { src: ['foo/*.js', '!foo/bar.js' ], dest: ... }
Files - 三种格式(1) Compact Format 每个 target 只能指定一对src-dest grunt.initConfig({ jshint:
{ foo: { src: ['src/aa.js', 'src/aaa.js'] } }, concat: { bar: { src: ['src/bb.js', 'src/bbb.js'], dest: 'dest/b.js', } } }); src dest
Files - 三种格式(2) Files Object Format 每个 target 可以指定多对src-dest 放在
files 对象里面 grunt.initConfig({ concat: { foo: { files: { 'dest/a.js': ['src/aa.js', 'src/aaa.js'], 'dest/a1.js': ['src/aa1.js', 'src/aaa1.js'] } }, bar: { files: { 'dest/b.js': ['src/bb.js', 'src/bbb.js'], 'dest/b1.js': ['src/bb1.js', 'src/bbb1.js'] } } } });
Files - 三种格式(3) Files Array Format 每个 target 可以指定多对src-dest 放在
files 数组里面 另:可以为每个 src-dest 对象指定额外的 属性,比如filter 等 grunt.initConfig({ concat: { foo: { files: [ { src: [ 'src/aa.js', 'src/aaa.js'], dest: 'dest/a.js' }, { src: [ 'src/aa1.js', 'src/aaa1.js'], dest: 'dest/a1.js' } ] }, bar: { files: [ { src: [ 'src/bb.js', 'src/bbb.js'], dest: 'dest/b/', nonull: true }, { src: [ 'src/bb1.js', 'src/bbb1.js'], dest: 'dest/b1/', filter: 'isFile' } ] } } }); filter nonull
You Got It
None
Team Workflow 最小化配置・可复用・可维护
Gruntfile.js packgage.json lib/ ... config.json + npm install Usage: Maintained:
module.exports = function(grunt) { grunt.initConfig({ project: grunt.file.readJSON('config.json') }); }; Load
Config
Template module.exports = function(grunt) { grunt.initConfig({ cssmin: { vips: {
src: ['**/*.css'], dest: '<%= dir %>/css/public/' } } }); }; <%= dir %>
Change Config grunt.config('dir', 'value')
None
None
Commands List grunt taste 构建更改的文件,但是不提交 grunt build:all 构建整个开发分支, 不提交 grunt
build:noimage 构建整个开发分支,除了图片, 不提交 grunt build:changelog 构建CHANGELOG内的文件, 不提交 grunt push -m 'comment' 构建分支并提交,更改文件写入 CHANGELOG grunt sync -m 'comment' push,最后同步更改的文件到开 发服务器 grunt sync:tpl 同步所有模板文件到开 发服务器 grunt sync:s2 同步所有静态文件到开发服务器 grunt sync:changelog 同步CHANGELOG记录的对应分支的文件到开发服务器 grunt vipserver 映射s2静态资源到本地, 监控模板文件更改自动同步到开发服务器 grunt switch:hosts 切换指定hosts grunt co svn checkout配置的所有静态资源分支 grunt lint lint更改的css/js文件 grunt monitor 监听文件更改,自动lint对应的文件 grunt tcl 生成一个CHANGELOG文件的模板 grunt deploy Build for deploy Build Sync Local Helper workflow for vipshop
None
None
Thank You Q&A