gulp+es6自动化环境搭建

1、首先npm init建立package.json文件

2、项目中安装gulp , npm install gulp --save-dev

3、安装以下所有插件,例如:cnpm install gulp-rename --save-dev

"devDependencies": {
    "babel-core": "^6.26.3",
    "babel-preset-es2015": "^6.24.1",
    "browserify": "^16.2.3",
    "glob-parent": "^3.1.0",
    "gulp": "^3.9.1",
    "gulp-babel": "^7.0.1",
    "gulp-connect": "^5.6.1",
    "gulp-less": "^4.0.1",
    "gulp-load-plugins": "^1.5.0",
    "gulp-minify-css": "^1.2.4",
    "gulp-rename": "^1.4.0",
    "gulp-uglify": "^3.0.1",
    "indx": "^0.2.3",
    "is-posix-bracket": "^0.1.1"
  }

4、新建gulpfile.js文件,配置如下:assets/javascript路径可自行修改

var gulp  = require ('gulp'),
		$ = require("gulp-load-plugins")();

/*活动模板*/
gulp.task('convertJS', function(){
  gulp.src(['assets/javascript/!(*.min).js'])
    .pipe($.babel({
      presets: ['es2015']
    }))
    .pipe($.uglify())
    .pipe($.rename({suffix: '.min'}))   //rename压缩后的文件名
    .pipe(gulp.dest('assets/javascript/'))
});

// 解析css
gulp.task("css",function () {
  gulp.src("assets/stylesheets/*.less")
  .pipe($.less())
 // .pipe(gulp.dest('./template5/assets/stylesheets/'))
  .pipe( $.minifyCss() )
  .pipe($.rename({suffix: '.min'}))   //rename压缩后的文件名
  .pipe( gulp.dest("assets/stylesheets/"))
})
gulp.task("reload",function () {
  gulp.src([
    "assets/stylesheets/*.less",
    "assets/javascript/*.js",
    "*.html"
    ])
  .pipe($.connect.reload())
})
//开启服务器
gulp.task("webserver",function () {
  $.connect.server({
    host: '0.0.0.0',
    port : "7070",
    livereload : true,
    root: "./"
  })
})


// gulp.watch([
//   "assets/stylesheets/*.less",
//   "assets/javascript/*.js",
//   "*.html",
// ],["css", "convertJS", "reload"])
gulp.watch([
  "assets/stylesheets/*.less",
],["css","reload"])//关联文件

gulp.watch([
  "assets/javascript/*.js",
  ],['convertJS',"reload"])//关联文件

gulp.watch([
"*.html",
],["reload"])//关联文件

gulp.task("default",["css","convertJS","webserver"])

5、若需要已配置好的代码,请访问 https://github.com/lilongwei4321/gulpES6 下载。

你可能感兴趣的:(gulp)