Transcript
Page 1: GruntJs Installation and popular plugins. MoscowJS

19:55

GRUNT.JSДОЛОЙ РУТИННЫЕ ЗАДАЧИ

Page 2: GruntJs Installation and popular plugins. MoscowJS

ДВА СЛОВА О СЕБЕДмитрий Кунинведущий разработчик AT-Consultingэнтузиаст javascriptжутко ленивый парень

Page 3: GruntJs Installation and popular plugins. MoscowJS

ЧТО ТАКОЕ GRUNT.JS?Система автоматизации, которая может практически все.

Page 4: GruntJs Installation and popular plugins. MoscowJS

КАК ЭТА ШТУКАРАБОТАЕТ?

node.jsnpmpackage.jsonGruntfile.js

Page 5: GruntJs Installation and popular plugins. MoscowJS

КТО ИСПОЛЬЗУЕТ

Page 6: GruntJs Installation and popular plugins. MoscowJS

ЧТО ВЫ НЕНАВИДИТЕДЕЛАТЬ?

ЧТО ВЫ ЗАБЫВАЕТЕДЕЛАТЬ?

Page 7: GruntJs Installation and popular plugins. MoscowJS

УСТАНОВКА РАЗ И НАВСЕГДАnpm install -g grunt-cli

УСТАНОВКА В ПРОЕКТnpm install grunt --save-dev

Page 8: GruntJs Installation and popular plugins. MoscowJS

УСТАНОВКА И НАСТРОЙКАШАБЛОНИЗАТОРА

npm install -g grunt-init git clone [email protected]:gruntjs/grunt-init-TEMPLATE.git ~/.grunt-init/TEMPLATE

TEMPLATES:

grunt-init-commonjs grunt-init-gruntfile grunt-init-gruntplugin grunt-init-jquery grunt-init-node

Page 9: GruntJs Installation and popular plugins. MoscowJS

ЗАГРУЗКА/УСТАНОВКА GRUNTFILEgrunt-init grunt-init-gruntfile || grunt-init gruntfile

Page 10: GruntJs Installation and popular plugins. MoscowJS

СТРУКТУРА GRUNTFILE

module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-watch'); grunt.initConfig({ watch: { files: ['<%= jshint.files %>'], tasks: ['jshint'] } }); grunt.registerTask('default', ['jshint', 'concat', 'uglify']); };

Page 11: GruntJs Installation and popular plugins. MoscowJS

КОММАНДЫ GRUNT

grunt.initConfig grunt.file.readJSON('package.json') grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default',["..."]) grunt.log.writeln("..."); grunt.log.error("..."); grunt.task.requires('foo'); grunt.event.on('watch') grunt.template.today("yyyy-mm-dd"); grunt.template.date(847602000000, 'yyyy-mm-dd')

Page 12: GruntJs Installation and popular plugins. MoscowJS

УСТАНОВКА ПЛАГИНА

npm install grunt-contrib-concat npm install grunt-contrib-concat --save npm install grunt-contrib-concat --save-dev

grunt.loadNpmTasks('grunt-contrib-watch'); watch: { files: ['<%= jshint.files %>'], tasks: ['jshint'] }

Page 13: GruntJs Installation and popular plugins. MoscowJS

PACKAGE.JSON

"author": "authorName", "name": "projectName", "version": "0.0.1", "devDependencies" : { "grunt": ">= 0.4", "grunt-contrib-watch": "~0.3.1", "grunt-contrib-cssmin": ">=0.5.0", "grunt-contrib-uglify": ">=0.2.0", "grunt-contrib-concat": ">=0.1.3" }

Page 14: GruntJs Installation and popular plugins. MoscowJS

===

АЛЬТЕРНАТИВНАЯ ЗАГРУЗКАПЛАГИНОВ

grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-jade'); grunt.loadNpmTasks('grunt-contrib-stylus'); grunt.loadNpmTasks('grunt-contrib-testing'); grunt.loadNpmTasks('grunt-contrib-pixel-perfect'); grunt.loadNpmTasks('grunt-shedevr');

npm install matchdep require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks)

Page 15: GruntJs Installation and popular plugins. MoscowJS

ЗАПУСК ЗАДАЧ

grunt build grunt build:test grunt connect:smth

grunt.registerTask('update', function(message){ globalConfig.message = message; grunt.task.run('bgShell:gitCi'); }); bgShell: { _defaults: { bg: false }, gitCi: { cmd: 'git commit -am "<%= globalConfig.message %>"', } },

Page 16: GruntJs Installation and popular plugins. MoscowJS

ОБЪЕДИНЕНИЕ НЕСКОЛЬКИХ ЗАДАЧ

grunt.registerTask('default', [ 'jshint', 'concat', 'uglify' ]);

Page 17: GruntJs Installation and popular plugins. MoscowJS

ПОПУЛЯРНЫЕЗАДАЧИ ИПЛАГИНЫ

Page 18: GruntJs Installation and popular plugins. MoscowJS

ПРЕПРОЦЕССИНГ HTMLgrunt-contrib-jadegrunt-contrib-hamlgrunt-slim

npm install grunt-contrib-jade --save-dev npm install grunt-contrib-haml --save-dev npm install grunt-slim --save-dev

Page 19: GruntJs Installation and popular plugins. MoscowJS

GRUNT-CONTRIB-JADE

jade: { dist: [ { src: ['views/*.jade'], dest: '/' } ], test: [ { src: ['test/*.jade'], dest: '/test' } ] }

Page 20: GruntJs Installation and popular plugins. MoscowJS

ПРЕПРОЦЕССИНГ СSSstyluslesssass

npm install grunt-contrib-stylus --save-dev npm install grunt-contrib-less --save-dev npm install grunt-contrib-sass --save-dev

Page 21: GruntJs Installation and popular plugins. MoscowJS

ПРИМЕРНАЯ НАСТРОЙКА CSSПРЕПРОЦЕССОРА

sass: { dist: { 'main.css': 'main.scss' } }

Page 22: GruntJs Installation and popular plugins. MoscowJS

ПРЕПРОЦЕССИНГ/LINTING JSCoffescriptJSHint

npm install grunt-contrib-jshint --save-dev npm install grunt-contrib-coffee --save-dev

Page 23: GruntJs Installation and popular plugins. MoscowJS

JSHINT

jshint: { options: { jshintrc: '.jshintrc' }, all: [ 'Gruntfile.js', 'app/scripts/{,*/}*.js', '!app/scripts/vendor/*', 'test/spec/{,*/}*.js' ] }

{ "node": true, "bitwise": true, "camelcase": true, "curly": true, "eqeqeq": true, "immed": true, "indent": 4, "latedef": true, "newcap": true, "jquery": true }

Page 24: GruntJs Installation and popular plugins. MoscowJS

UNIT TESTINGjasmine

npm install grunt-contrib-jasmine --save-dev

Page 25: GruntJs Installation and popular plugins. MoscowJS

CONCATINATION/UGLIFYuseminuglifyconcat

npm install grunt-contrib-usemin --save-dev npm install grunt-contrib-concat --save-dev npm install grunt-contrib-uglify --save-dev

Page 26: GruntJs Installation and popular plugins. MoscowJS

USEMINprepeareUsemin -> concat/uglify-> usemin

<!-- build:css css/main.css --> <link rel="stylesheet" href="css/reveal.css"> <link rel="stylesheet" href="css/moon.css"> <link rel="stylesheet" href="css/zenburn.css"> <!-- endbuild -- > <!-- build:js js/app.js --> <script src="js/reveal.js"></script> <script src="js/init.js"></script"> <!-- endbuild -- >

Page 27: GruntJs Installation and popular plugins. MoscowJS

ОЧИСТКА КОДАdebug/console cleanup

npm install grunt-groundskeeper --save-dev npm install grunt-remove-logging --save-dev

Page 28: GruntJs Installation and popular plugins. MoscowJS

РАЗНОЕОткрытие страницыУдаление временных папокАрхивацияВыгрузка на фтпБампинг package.json файлаCDN замена скриптов

npm install grunt-open --save-dev npm install grunt-contrib-clean --save-dev npm install grunt-contrib-compress --save-dev npm install grunt-ftp-deploy --save-dev npm install grunt-bumpup --save-dev npm install grunt-google-cdn --save-dev

Page 29: GruntJs Installation and popular plugins. MoscowJS

ОБЪЕДИНЕНИЕВСЕГО УКАЗАННОГО В СВЯЗКУ

watch

npm install grunt-contrib-watch --save-dev

Page 30: GruntJs Installation and popular plugins. MoscowJS

НАСТРОЙКА WATCH

watch: { scripts: { files: [ "public/**/*.js", "app/assets/stylesheets/stylus/*.styl" ], tasks:["stylus"], options: { livereload: true }, } }

Page 31: GruntJs Installation and popular plugins. MoscowJS

ГЛАВНЫЙ РЕСУРСHTTP://GRUNTJS.COM/

Page 32: GruntJs Installation and popular plugins. MoscowJS

ФИНАЛЬНЫЙ ХИНТ

~/.bash_profile # Установка grunt плагина с сохранением в dev зависимости function gi() { npm install --save-dev grunt-"$@" } # Установка grunt-contrib плагина с сохранением в dev зависимости function gci() { npm install --save-dev grunt-contrib-"$@" }

Page 33: GruntJs Installation and popular plugins. MoscowJS

ВОПРОСЫ?СПАСИБО ЗА ВНИМАНИЕ!

Дмитрий Кунинbrainstorage.me/[email protected]


Top Related