声明,以下相关全是前端的东西。
Yeoman 一个自动化工作流,说白了就是把一些你需要手动操作的东西,自动给你做好。
我的理解就是给你创建一个项目,然后里面该有的东西都有了。
官网:http://www.yeoman.io/
Grunt 什么?大G?,其实不是的。
一个类似maven的东西(不知道maven?,那总知道ant吧),能够对前端的文件进行压缩,打包,还有部署,(应该不叫部署,叫整理,更合适一些)。
官网:http://gruntjs.com/
grunt的强大,在于可以有很多第三方的插件可以下载,例如less,coffeescripe等等,grunt可以帮助你自动编译这些文件。
Bower 一只肥鸟,官方说法叫做A package manager for the web,也就是包管理器。
自我感觉不是很好用,比如你需要用jquery,它会把jquery相关的文件全部下载,例如jquery.cokkie.js等,不仅浪费时间,还占用空间。
下面来讲解,这三样东西的具体配置:
安装Yeoman
//首先,你可以执行 sudo npm install -g yo grunt-cli bower 这样可以一下安装3个文件,当然你也可以选择不装。。
先得安装Node.js,Git和Ruby,不要问为什么,官网说的。。。
npm install -g yo
创建一个project目录,定位在该目录下
npm install -g generator-webapp
-g 的意思是全局安装,这样的话你就可以在任何地方使用yo命令了。
默认情况下会让你选择webapp所用的框架,例如
可以多选,也可以单选,选择后生成的相关配置文件也会根据你的选择来改变,所以这一配置要慎重。
这样,yeoman就装好了。
让你想执行bower命令时,发现报错,因为你还没装bower。。
安装Bower
npm install -g bower
这个很简单,先装上再说,具体用法,下面会讲。
安装Grunt
npm install -g grunt-cli
cli并不是grunt,只是一个能够让你执行grunt命令的程序
接下来安装grunt
npm install -g grunt
回到你建的project目录,装完之后可以看到 目录下会有一个Gruntfile.js文件,
这个是grunt的核心文件,也是这三样东西交互的文件。
// Generated on 2014-04-08 using generator-webapp 0.4.8 ‘use strict‘; // # Globbing // for performance reasons we‘re only matching one level down: // ‘test/spec/{,*/}*.js‘ // use this if you want to recursively match all subfolders: // ‘test/spec/**/*.js‘ module.exports = function (grunt) { // Load grunt tasks automatically require(‘load-grunt-tasks‘)(grunt); // Time how long tasks take. Can help when optimizing build times require(‘time-grunt‘)(grunt); // Define the configuration for all the tasks grunt.initConfig({ // Project settings config: { // Configurable paths app: ‘app‘, dist: ‘dist‘ }, // Watches files for changes and runs tasks based on the changed files watch: { bower: { files: [‘bower.json‘], tasks: [‘bowerInstall‘] }, js: { files: [‘<%= config.app %>/scripts/{,*/}*.js‘], tasks: [‘jshint‘], options: { livereload: true } }, jstest: { files: [‘test/spec/{,*/}*.js‘], tasks: [‘test:watch‘] }, gruntfile: { files: [‘Gruntfile.js‘] }, styles: { files: [‘<%= config.app %>/styles/{,*/}*.css‘], tasks: [‘newer:copy:styles‘, ‘autoprefixer‘] }, livereload: { options: { livereload: ‘<%= connect.options.livereload %>‘ }, files: [ ‘<%= config.app %>/{,*/}*.html‘, ‘.tmp/styles/{,*/}*.css‘, ‘<%= config.app %>/images/{,*/}*‘ ] } }, // The actual grunt server settings connect: { options: { port: 9000, livereload: 35729, // Change this to ‘0.0.0.0‘ to access the server from outside hostname: ‘localhost‘ }, livereload: { options: { open: true, base: [ ‘.tmp‘, ‘<%= config.app %>‘ ] } }, test: { options: { port: 9001, base: [ ‘.tmp‘, ‘test‘, ‘<%= config.app %>‘ ] } }, dist: { options: { open: true, base: ‘<%= config.dist %>‘, livereload: false } } }, // Empties folders to start fresh clean: { dist: { files: [{ dot: true, src: [ ‘.tmp‘, ‘<%= config.dist %>/*‘, ‘!<%= config.dist %>/.git*‘ ] }] }, server: ‘.tmp‘ }, // Make sure code styles are up to par and there are no obvious mistakes jshint: { options: { jshintrc: ‘.jshintrc‘, reporter: require(‘jshint-stylish‘) }, all: [ ‘Gruntfile.js‘, ‘<%= config.app %>/scripts/{,*/}*.js‘, ‘!<%= config.app %>/scripts/vendor/*‘, ‘test/spec/{,*/}*.js‘ ] }, // Mocha testing framework configuration options mocha: { all: { options: { run: true, urls: [‘http://<%= connect.test.options.hostname %>:<%= connect.test.options.port %>/index.html‘] } } }, // Add vendor prefixed styles autoprefixer: { options: { browsers: [‘last 1 version‘] }, dist: { files: [{ expand: true, cwd: ‘.tmp/styles/‘, src: ‘{,*/}*.css‘, dest: ‘.tmp/styles/‘ }] } }, // Automatically inject Bower components into the HTML file bowerInstall: { app: { src: [‘<%= config.app %>/index.html‘], ignorePath: ‘<%= config.app %>/‘ } }, // Renames files for browser caching purposes rev: { dist: { files: { src: [ ‘<%= config.dist %>/scripts/{,*/}*.js‘, ‘<%= config.dist %>/styles/{,*/}*.css‘, ‘<%= config.dist %>/images/{,*/}*.*‘, ‘<%= config.dist %>/styles/fonts/{,*/}*.*‘, ‘<%= config.dist %>/*.{ico,png}‘ ] } } }, // Reads HTML for usemin blocks to enable smart builds that automatically // concat, minify and revision files. Creates configurations in memory so // additional tasks can operate on them useminPrepare: { options: { dest: ‘<%= config.dist %>‘ }, html: ‘<%= config.app %>/index.html‘ }, // Performs rewrites based on rev and the useminPrepare configuration usemin: { options: { assetsDirs: [‘<%= config.dist %>‘, ‘<%= config.dist %>/images‘] }, html: [‘<%= config.dist %>/{,*/}*.html‘], css: [‘<%= config.dist %>/styles/{,*/}*.css‘] }, // The following *-min tasks produce minified files in the dist folder imagemin: { dist: { files: [{ expand: true, cwd: ‘<%= config.app %>/images‘, src: ‘{,*/}*.{gif,jpeg,jpg,png}‘, dest: ‘<%= config.dist %>/images‘ }] } }, svgmin: { dist: { files: [{ expand: true, cwd: ‘<%= config.app %>/images‘, src: ‘{,*/}*.svg‘, dest: ‘<%= config.dist %>/images‘ }] } }, htmlmin: { dist: { options: { collapseBooleanAttributes: true, collapseWhitespace: true, removeAttributeQuotes: true, removeCommentsFromCDATA: true, removeEmptyAttributes: true, removeOptionalTags: true, removeRedundantAttributes: true, useShortDoctype: true }, files: [{ expand: true, cwd: ‘<%= config.dist %>‘, src: ‘{,*/}*.html‘, dest: ‘<%= config.dist %>‘ }] } }, // By default, your `index.html`‘s <!-- Usemin block --> will take care of // minification. These next options are pre-configured if you do not wish // to use the Usemin blocks. // cssmin: { // dist: { // files: { // ‘<%= config.dist %>/styles/main.css‘: [ // ‘.tmp/styles/{,*/}*.css‘, // ‘<%= config.app %>/styles/{,*/}*.css‘ // ] // } // } // }, // uglify: { // dist: { // files: { // ‘<%= config.dist %>/scripts/scripts.js‘: [ // ‘<%= config.dist %>/scripts/scripts.js‘ // ] // } // } // }, // concat: { // dist: {} // }, // Copies remaining files to places other tasks can use copy: { dist: { files: [{ expand: true, dot: true, cwd: ‘<%= config.app %>‘, dest: ‘<%= config.dist %>‘, src: [ ‘*.{ico,png,txt}‘, ‘.htaccess‘, ‘images/{,*/}*.webp‘, ‘{,*/}*.html‘, ‘styles/fonts/{,*/}*.*‘, ‘bower_components/bootstrap/dist/fonts/*.*‘ ] }] }, styles: { expand: true, dot: true, cwd: ‘<%= config.app %>/styles‘, dest: ‘.tmp/styles/‘, src: ‘{,*/}*.css‘ } }, // Run some tasks in parallel to speed up build process concurrent: { server: [ ‘copy:styles‘ ], test: [ ‘copy:styles‘ ], dist: [ ‘copy:styles‘, ‘imagemin‘, ‘svgmin‘ ] } }); grunt.registerTask(‘serve‘, function (target) { if (target === ‘dist‘) { return grunt.task.run([‘build‘, ‘connect:dist:keepalive‘]); } grunt.task.run([ ‘clean:server‘, ‘concurrent:server‘, ‘autoprefixer‘, ‘connect:livereload‘, ‘watch‘ ]); }); grunt.registerTask(‘server‘, function (target) { grunt.log.warn(‘The `server` task has been deprecated. Use `grunt serve` to start a server.‘); grunt.task.run([target ? (‘serve:‘ + target) : ‘serve‘]); }); grunt.registerTask(‘test‘, function (target) { if (target !== ‘watch‘) { grunt.task.run([ ‘clean:server‘, ‘concurrent:test‘, ‘autoprefixer‘ ]); } grunt.task.run([ ‘connect:test‘, ‘mocha‘ ]); }); grunt.registerTask(‘build‘, [ ‘clean:dist‘, ‘useminPrepare‘, ‘concurrent:dist‘, ‘autoprefixer‘, ‘concat‘, ‘cssmin‘, ‘uglify‘, ‘copy:dist‘, ‘rev‘, ‘usemin‘, ‘htmlmin‘ ]); grunt.registerTask(‘default‘, [ ‘newer:jshint‘, ‘test‘, ‘build‘ ]); };
可以看到,文件中的每个配置项都是一个task,并且没一个task都是需要下载grunt插件的。
require(‘load-grunt-tasks‘)(grunt);
这段代码很重要,他会找到项目下的node_modules文件夹,把其中的grunt插件全部引入。
接下来便可以执行grunt serve命令了
grunt.registerTask(‘serve‘, function (target) { if (target === ‘dist‘) { return grunt.task.run([‘build‘, ‘connect:dist:keepalive‘]); } grunt.task.run([ ‘clean:server‘, ‘concurrent:server‘, ‘autoprefixer‘, ‘connect:livereload‘, ‘watch‘ ]); });
可以看到这个serve命令还接受一个参数target,如果执行grunt serve dist 就会执行build task还有connect下的dist下的 keeplive task。
如果不传参数,正常执行if外面的命令,最后也个watch task很关键,会启动一个临时server来给你debug用,会监听你的每个文件是否改变来刷新你的浏览器(livereload task配置相
关),很智能吧。
未完
Yeoman Grunt Bower配置相关,布布扣,bubuko.com
原文:http://blog.csdn.net/u012844719/article/details/23164939