gulp常用

来源:互联网 发布:linux运维简历 编辑:程序博客网 时间:2024/06/06 07:49
  1. 全局安装 gulp=>$ npm install –global gulp

  2. 作为项目的开发依赖(devDependencies)安装:
    $ npm install –save-dev gulp

  3. 在项目根目录下创建一个名为 gulpfile.js 的文件:
    var gulp = require(‘gulp’);

  4. 使用插件要安装插件,如npm install –save-dev gulp-clear;

  5. 要启动服务器的时候输入gulp就可以了;
    以下是代码片段
var gulp = require('gulp');var $ = require('gulp-load-plugins')();var open = require('open');var app = {  srcPath: 'src/',  devPath: 'build/',  prdPath: 'dist/'};gulp.task('lib', function() {  gulp.src('bower_components/**/*.js')  .pipe(gulp.dest(app.devPath + 'vendor'))  .pipe(gulp.dest(app.prdPath + 'vendor'))  .pipe($.connect.reload());});gulp.task('html', function() {  gulp.src(app.srcPath + '**/*.html')  .pipe(gulp.dest(app.devPath))  .pipe(gulp.dest(app.prdPath))  .pipe($.connect.reload());})gulp.task('json', function() {  gulp.src(app.srcPath + 'data/**/*.json')  .pipe(gulp.dest(app.devPath + 'data'))  .pipe(gulp.dest(app.prdPath + 'data'))  .pipe($.connect.reload());});gulp.task('less', function() {  gulp.src(app.srcPath + 'style/index.less')  .pipe($.plumber())  .pipe($.less())  .pipe(gulp.dest(app.devPath + 'css'))  .pipe($.cssmin())  .pipe(gulp.dest(app.prdPath + 'css'))  .pipe($.connect.reload());});gulp.task('js', function() {  gulp.src(app.srcPath + 'script/**/*.js')  .pipe($.plumber())  .pipe($.concat('index.js'))  .pipe(gulp.dest(app.devPath + 'js'))  .pipe($.uglify())  .pipe(gulp.dest(app.prdPath + 'js'))  .pipe($.connect.reload());});gulp.task('image', function() {  gulp.src(app.srcPath + 'image/**/*')  .pipe($.plumber())  .pipe(gulp.dest(app.devPath + 'image'))  .pipe($.imagemin())  .pipe(gulp.dest(app.prdPath + 'image'))  .pipe($.connect.reload());});gulp.task('build', ['image', 'js', 'less', 'lib', 'html', 'json']);gulp.task('clean', function() {  gulp.src([app.devPath, app.prdPath])  .pipe($.clean());});gulp.task('serve', ['build'], function() {  $.connect.server({    root: [app.devPath],    livereload: true,    port: 3000  });  open('http://localhost:3000');  gulp.watch('bower_components/**/*', ['lib']);  gulp.watch(app.srcPath + '**/*.html', ['html']);  gulp.watch(app.srcPath + 'data/**/*.json', ['json']);  gulp.watch(app.srcPath + 'style/**/*.less', ['less']);  gulp.watch(app.srcPath + 'script/**/*.js', ['js']);  gulp.watch(app.srcPath + 'image/**/*', ['image']);});gulp.task('default', ['serve']);

以上代码安装的插件参考:这些都是用过npm 安装之后自动保存在package.json的devDependencies当中的;

    "gulp": "^3.9.1",    "gulp-clean": "^0.3.2",    "gulp-concat": "^2.6.0",    "gulp-connect": "^5.0.0",    "gulp-cssmin": "^0.1.7",    "gulp-imagemin": "^3.0.3",    "gulp-less": "^3.1.0",    "gulp-load-plugins": "^1.2.4",    "gulp-plumber": "^1.1.0",    "gulp-uglify": "^2.0.0",    "open": "0.0.5"
原创粉丝点击