初识yeoman

最近开始新项目,在项目构建上面寻找合适的东西,grunt,bower到发现yeoman;学习了下,把一些东西记录下来然留着以后用.

1.什么是Yeoman

Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML、CSS、Javascript和图片等前端资源的包管理器)的包装为开发者创建一个易用的工作流。

Yeoman的目的不仅是要为新项目建立工作流,同时还是为了解决前端开发所面临的诸多严重问题,例如零散的依赖关系。

  • yo - the scaffolding tool from Yeoman
  • bower - the package management tool
  • grunt - the build tool

Yeoman就是快速创建webapp,初始化了各个版本的文件目录有关bower和grunt的配置.初始化过程中以询问模式处理.

2.安装Yeoman

依赖:Nodejs 如果需要使用sass 要安装Ruby

在安装玩完 nodejs的情况下

终端运行:

npm install -g yo
npm install -g grunt-cli bower

3.install 基本的项目模板(generator);

yeoman提供了一些基本的模板 如:

generator-webapp

generator-angular

使用命令添加

npm install generator-angular -g 

more

4.可以创建自己的 generator

creat

5. generator-angular 示例

初始化项目

i 初始化一个以angualr为基础的项目

yo angular

终端显示如下:

根据需求选择你需要的依赖

一段时间后项目生成如下结构

其中很多配置文件yeoman都给我们自动创建好了 比如package.json  Gruntfile.js .gitignore 等配置文件.

到这里项目创建成功了!

-----------------

6.分析下 Gruntfile.js

这是创建的示例Gruntfile.js

// Generated on 2014-07-02 using generator-angular 0.9.1
‘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);

  // Configurable paths for the application
  var appConfig = {
    app: require(‘./bower.json‘).appPath || ‘app‘,
    dist: ‘build‘
  };

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: appConfig,

    // Watches files for changes and runs tasks based on the changed files
    watch: {
      bower: {
        files: [‘bower.json‘],
        tasks: [‘wiredep‘]
      },
      js: {
        files: [‘<%= yeoman.app %>/scripts/{,*/}*.js‘],
        tasks: [‘newer:jshint:all‘],
        options: {
          livereload: ‘<%= connect.options.livereload %>‘
        }
      },
      jsTest: {
        files: [‘test/spec/{,*/}*.js‘],
        tasks: [‘newer:jshint:test‘, ‘karma‘]
      },
      styles: {
        files: [‘<%= yeoman.app %>/styles/{,*/}*.css‘],
        tasks: [‘newer:copy:styles‘, ‘autoprefixer‘]
      },
      gruntfile: {
        files: [‘Gruntfile.js‘]
      },
      livereload: {
        options: {
          livereload: ‘<%= connect.options.livereload %>‘
        },
        files: [
          ‘<%= yeoman.app %>/{,*/}*.html‘,
          ‘.tmp/styles/{,*/}*.css‘,
          ‘<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}‘
        ]
      }
    },

    // The actual grunt server settings
    connect: {
      options: {
        port: 9000,
        // Change this to ‘0.0.0.0‘ to access the server from outside.
        hostname: ‘localhost‘,
        livereload: 35729
      },
      livereload: {
        options: {
          open: true,
          middleware: function (connect) {
            return [
              connect.static(‘.tmp‘),
              connect().use(
                ‘/bower_components‘,
                connect.static(‘./bower_components‘)
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      test: {
        options: {
          port: 9001,
          middleware: function (connect) {
            return [
              connect.static(‘.tmp‘),
              connect.static(‘test‘),
              connect().use(
                ‘/bower_components‘,
                connect.static(‘./bower_components‘)
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      dist: {
        options: {
          open: true,
          base: ‘<%= yeoman.dist %>‘
        }
      }
    },

    // Make sure code styles are up to par and there are no obvious mistakes
    jshint: {
      options: {
        jshintrc: ‘.jshintrc‘,
        reporter: require(‘jshint-stylish‘)
      },
      all: {
        src: [
          ‘Gruntfile.js‘,
          ‘<%= yeoman.app %>/scripts/{,*/}*.js‘
        ]
      },
      test: {
        options: {
          jshintrc: ‘test/.jshintrc‘
        },
        src: [‘test/spec/{,*/}*.js‘]
      }
    },

    // Empties folders to start fresh
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            ‘.tmp‘,
            ‘<%= yeoman.dist %>/{,*/}*‘,
            ‘!<%= yeoman.dist %>/.git*‘
          ]
        }]
      },
      server: ‘.tmp‘
    },

    // 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 app
    wiredep: {
      options: {
        cwd: ‘<%= yeoman.app %>‘
      },
      app: {
        src: [‘<%= yeoman.app %>/index.html‘],
        ignorePath:  /..\//
      }
    },

    // Renames files for browser caching purposes
    filerev: {
      dist: {
        src: [
          ‘<%= yeoman.dist %>/scripts/{,*/}*.js‘,
          ‘<%= yeoman.dist %>/styles/{,*/}*.css‘,
          ‘<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}‘,
          ‘<%= yeoman.dist %>/styles/fonts/*‘
        ]
      }
    },

    // 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: {
      html: ‘<%= yeoman.app %>/index.html‘,
      options: {
        dest: ‘<%= yeoman.dist %>‘,
        flow: {
          html: {
            steps: {
              js: [‘concat‘, ‘uglifyjs‘],
              css: [‘cssmin‘]
            },
            post: {}
          }
        }
      }
    },

    // Performs rewrites based on filerev and the useminPrepare configuration
    usemin: {
      html: [‘<%= yeoman.dist %>/{,*/}*.html‘],
      css: [‘<%= yeoman.dist %>/styles/{,*/}*.css‘],
      options: {
        assetsDirs: [‘<%= yeoman.dist %>‘,‘<%= yeoman.dist %>/images‘]
      }
    },

    /*The following *-min tasks will produce minified files in the dist folder
    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: {
           ‘<%= yeoman.dist %>/styles/main.css‘: [
             ‘.tmp/styles/{,*/}*.css‘
           ]
         }
       }
    },
    uglify: {
       dist: {
         files: {
           ‘<%= yeoman.dist %>/scripts/scripts.js‘: [
             ‘<%= yeoman.dist %>/scripts/scripts.js‘
           ]
         }
       }
    },
    concat: {
       dist: {}
    },

    imagemin: {
      dist: {
        files: [{
          expand: true,
          cwd: ‘<%= yeoman.app %>/images‘,
          src: ‘{,*/}*.{png,jpg,jpeg,gif}‘,
          dest: ‘<%= yeoman.dist %>/images‘
        }]
      }
    },

    svgmin: {
      dist: {
        files: [{
          expand: true,
          cwd: ‘<%= yeoman.app %>/images‘,
          src: ‘{,*/}*.svg‘,
          dest: ‘<%= yeoman.dist %>/images‘
        }]
      }
    },

    htmlmin: {
      dist: {
        options: {
          collapseWhitespace: true,
          conservativeCollapse: true,
          collapseBooleanAttributes: true,
          removeCommentsFromCDATA: true,
          removeOptionalTags: true
        },
        files: [{
          expand: true,
          cwd: ‘<%= yeoman.dist %>‘,
          src: [‘*.html‘, ‘views/{,*/}*.html‘],
          dest: ‘<%= yeoman.dist %>‘
        }]
      }
    },

    // ngmin tries to make the code safe for minification automatically by
    // using the Angular long form for dependency injection. It doesn‘t work on
    // things like resolve or inject so those have to be done manually.
    ngmin: {
      dist: {
        files: [{
          expand: true,
          cwd: ‘.tmp/concat/scripts‘,
          src: ‘*.js‘,
          dest: ‘.tmp/concat/scripts‘
        }]
      }
    },

    // Replace Google CDN references
    cdnify: {
      dist: {
        html: [‘<%= yeoman.dist %>/*.html‘]
      }
    },

    // Copies remaining files to places other tasks can use
    copy: {
      dist: {
        files: [{
          expand: true,
          dot: true,
          cwd: ‘<%= yeoman.app %>‘,
          dest: ‘<%= yeoman.dist %>‘,
          src: [
            ‘*.{ico,png,txt}‘,
            ‘.htaccess‘,
            ‘*.html‘,
            ‘views/{,*/}*.html‘,
            ‘images/{,*/}*.{webp}‘,
            ‘fonts/*‘
          ]
        }, {
          expand: true,
          cwd: ‘.tmp/images‘,
          dest: ‘<%= yeoman.dist %>/images‘,
          src: [‘generated/*‘]
        }]
      },
      styles: {
        expand: true,
        cwd: ‘<%= yeoman.app %>/styles‘,
        dest: ‘.tmp/styles/‘,
        src: ‘{,*/}*.css‘
      }
    },

    // Run some tasks in parallel to speed up the build process
    concurrent: {
      server: [
        ‘copy:styles‘
      ],
      test: [
        ‘copy:styles‘
      ],
      dist: [
        ‘copy:styles‘,
        ‘imagemin‘,
        ‘svgmin‘
      ]
    },

    // Test settings
    karma: {
      unit: {
        configFile: ‘test/karma.conf.js‘,
        singleRun: true
      }
    }
  });

  grunt.registerTask(‘serve‘, ‘Compile then start a connect web server‘, function (target) {
    if (target === ‘dist‘) {
      return grunt.task.run([‘build‘, ‘connect:dist:keepalive‘]);
    }

    grunt.task.run([
      ‘clean:server‘,
      ‘wiredep‘,
      ‘concurrent:server‘,
      ‘autoprefixer‘,
      ‘connect:livereload‘,
      ‘watch‘
    ]);
  });

  grunt.registerTask(‘server‘, ‘DEPRECATED TASK. Use the "serve" task instead‘, function (target) {
    grunt.log.warn(‘The `server` task has been deprecated. Use `grunt serve` to start a server.‘);
    grunt.task.run([‘serve:‘ + target]);
  });

  grunt.registerTask(‘test‘, [
    ‘clean:server‘,
    ‘concurrent:test‘,
    ‘autoprefixer‘,
    ‘connect:test‘,
    ‘karma‘
  ]);

  grunt.registerTask(‘build‘, [
    ‘clean:dist‘,
    ‘wiredep‘,
    ‘useminPrepare‘,
    ‘concurrent:dist‘,
    ‘autoprefixer‘,
    ‘concat‘,
    ‘ngmin‘,
    ‘copy:dist‘,
    ‘cdnify‘,
    ‘cssmin‘,
    ‘uglify‘,
    ‘filerev‘,
    ‘usemin‘,
    ‘htmlmin‘
  ]);

  grunt.registerTask(‘default‘, [
    ‘newer:jshint‘,
    ‘test‘,
    ‘build‘
  ]);
};

Gruntfile.js

------------

参考:

http://yeoman.io/

http://www.cnblogs.com/cocowool/archive/2013/03/09/2952003.html

http://www.36ria.com/6144

初识yeoman

时间: 2024-07-30 04:54:55

初识yeoman的相关文章

前端自动化构建工具-yoman浅谈

如今随着前端技术的飞速发展,前端项目也变得越来越复杂.快速的搭建一个集成多种工具和框架的复杂前端项目也越来越成为一种需求.当然如果你要自己从0开始完全自己diy,绝对可以,只不过需要耗费一些不少的时间.既然如此要是有自动化的项目构建工具,帮你生成各种必须的配置项,你只需愉快的写代码该多方便呀.嗯,是的这样的工具或者说脚手架确实是有的,就是下面要提到的eoman.来吧,一起看一下如何使用这个工具让你的项目秒建吧. 初识yeoman yeoman是什么 yeoman是Google领头开发的一个前端构

初识Python,望君多多关照

在学习Python之前,我们接触过数据结构和网页制作.前者让我们学习如何把C语言运用的更加整齐规范,而后者让我们亲身学习如何运用所学,制作一个静态网页.通过这些课程的学习,让我对C语言产生了比较大的压力,以至于对编程.对这学期的Python课程都有一种如临大敌的感觉. 但是真的学习了这门课程,体会了编码过程中的一些固定运用方法和套路之后,也许过程中对这门课程隐隐约约产生了一点点朦胧的感觉,仿佛他也并没有想象中的那么困难,起码现在的学习让我认为,他可能没有C语言那么繁琐和麻烦.当然,以一个初学者的

初识数组排序!!!!

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>初识数组排序</title> <!--调试成功--> <style type="text/css"> *{ padding:0; margin: 0; } li,ul{ list-style: none; } #p

初识操作系统和linux

初识操作系统和linux 1.计算机系统由硬件系统和软件系统两大部分组成:是一种能接收和存储信息,并按照存储在其内部的程序对海量数据进行自动.高速地处理,然后把处理结果输出的现代化智能电子设备. 2.世界上第一台计算机是1946年诞生在美国宾州大学. 3.冯·诺依曼体系结构:1946年数学家冯·诺依曼于提出计算机硬件系统由运算器.控制器.存储器.输入设备.输出设备.摩根定律:当价格不变时,集成电路上可容纳的元器件的数目,约每隔18-24个月便会增加一倍,性能也将提升一倍.现在计算机技术进本很难遵

JAVA 初识类加载机制 第13节

JAVA 初识类加载机制 第13节 从这章开始,我们就进入虚拟机类加载机制的学习了.那么什么是类加载呢?当我们写完一个Java类的时候,并不是直接就可以运行的,它还要编译成.class文件,再由虚拟机解释给当前的操作系统去执行.这些过程都是我们看不见的,我们能看见的也就是一个.class文件.既然虚拟机要解释这些.class文件给当前的操作系统听,那么他怎么获得这些.class文件呢?虚拟机获得这些.class文件的过程就是类加载了. 所以,总结来说就是:虚拟机将.class文件从磁盘或者其他地

初识React

原文地址:北云软件-初识React 专注于UI 在MVC分层设计模式中,react常被拿来实现视图层(V).React不依赖于技术栈的其他部分,因此可以方便的在现有项目中尝试用它来实现一个小特性. 虚拟DOM React从DOM中抽象出来,给出一种更简洁的编程模型,且性能表现更好.能够通过NodeJS实现服务端渲染,通过React Native开发原生app. 数据流React实现单向.响应式数据流,减少boilerplate且比传统数据绑定更容易理解. 简洁的组件React的组件都实现了一个r

泛型的几种类型以及初识webform

今天学习的可以分为两类吧,但是学习的都是比较抽象的,不太容易掌握吧.首先我们大部分时间学习了泛型,泛型的委托,泛型接口以及枚举器,迭代器,扩展方法:最后简单的认识了webform,实现了一个简单的功能. 一.泛型 定义:泛型(generic)可以软糖多个类型共享一组代码,泛型允许我们声明类型参数化.可以用不同的类型进行实例化,说白了,就是可以用类型占位符,创建具体类型致命的真实概念.C#中提供了五种泛型,类,结构,接口,委托和方法.下面举例说明可能更容易理解, class MyStack<T>

使用Yeoman你必须要知道的

不得不说,这年头写JS程序变得越来越复杂了.虽然大家都为了让写JS变得更简单而创建框架.创建工具,但是感觉已经开始偏离了最初的目的. 今天来说说Yeoman,一帮人为了让写AngularJS程序更高端智能而创建的一个工具.可以利用简单的命令行操作帮你初始化一个Angular App,CSS.图片.View和JS应该放哪都帮你决定好了,连复杂的Grunt都帮你配好了.前端同志只要用"grunt serve"就可以预览你的Web App而不用配置Web服务器,用"grunt bu

最新计算机技术与管理科学应用专家——初识ERB

ERB管理系统:英文全称Enterprise Resource and Behavior,英文简称:ERB,中文名全称:企业资源与行为管理系统.ERB是由理文企业管理顾问有限公司首席管理师,现任商翼ERB企业管理系统项目总监吴志华先生,于2010年9月首先提出的.ERB不再单以供应链管理作为系统应用的基础,而是以企业行为与企业资源规划的最佳结合作为系统应用设计的核心基础,强调企业行为的规划.执行.监督与追溯,强调企业管理水平与员工素养的持续提升:提供企业行为与企业资源管理最佳结合的整体应用解决方