这又是一个一次整合终身受益;不止是终身;换个项目同样可以很方便复用;不信你看另一个项目: thinkphp整合系列之gulp实现前端自动化
{
"name": "baijunyao/thinkphp-bjyadmin",
"version": "1.0.0",
"description": "博客",
"main": "index.js",
"scripts": {
"test": "echo \\"Error: no test specified\\" && exit 1"
},
"keywords": [
"白666",
"博客"
],
"author": "baijunyao",
"license": "Apache2",
"devDependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"browser-sync": "^2.14.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-cleanhtml": "^1.0.1",
"gulp-concat": "^2.6.0",
"gulp-contrib-copy": "^0.1.2",
"gulp-decomment": "^0.1.3",
"gulp-imagemin": "^3.0.2",
"gulp-less": "^3.1.0",
"gulp-load-plugins": "^1.2.4",
"gulp-minify-css": "^1.2.4",
"gulp-plumber": "^1.1.0",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.3.2",
"gulp-uglify": "^2.0.0"
}
}
var gulp = require('gulp'),
sass = require('gulp-sass'),
minifyCss = require('gulp-minify-css'),
plumber = require('gulp-plumber'),
babel = require('gulp-babel'),
uglify = require('gulp-uglify'),
clearnHtml = require("gulp-cleanhtml"),
imagemin = require('gulp-imagemin'),
copy = require('gulp-contrib-copy'),
browserSync = require('browser-sync').create(),
reload = browserSync.reload;
// 定义源代码的目录和编译压缩后的目录
var src='tpl_src',
dist='tpl';
// 编译全部scss 并压缩
gulp.task('css', function(){
gulp.src(src+'/**/*.scss')
.pipe(sass())
.pipe(minifyCss())
.pipe(gulp.dest(dist))
})
// 编译全部js 并压缩
gulp.task('js', function() {
gulp.src(src+'/**/*.js')
.pipe(plumber())
.pipe(babel({
presets: ['es2015']
}))
.pipe(uglify())
.pipe(gulp.dest(dist));
});
// 压缩全部html
gulp.task('html', function () {
gulp.src(src+'/**/*.+(html|tpl)')
.pipe(clearnHtml())
.pipe(gulp.dest(dist));
});
// 压缩全部image
gulp.task('image', function () {
gulp.src([src+'/**/*.+(jpg|jpeg|png|gif|bmp)'])
.pipe(imagemin())
.pipe(gulp.dest(dist));
});
// 其他不编译的文件直接copy
gulp.task('copy', function () {
gulp.src(src+'/**/*.!(jpg|jpeg|png|gif|bmp|scss|js|html|tpl)')
.pipe(copy())
.pipe(gulp.dest(dist));
});
// 自动刷新
gulp.task('server', function() {
browserSync.init({
proxy: "tbjyadmin.com", // 指定代理url
notify: false, // 刷新不弹出提示
});
// 监听scss文件编译
gulp.watch(src+'/**/*.scss', ['css']);
// 监听其他不编译的文件 有变化直接copy
gulp.watch(src+'/**/*.!(jpg|jpeg|png|gif|bmp|scss|js|html)', ['copy']);
// 监听html文件变化后刷新页面
gulp.watch(src+"/**/*.js", ['js']).on("change", reload);
// 监听html文件变化后刷新页面
gulp.watch(src+"/**/*.+(html|tpl)", ['html']).on("change", reload);
// 监听css文件变化后刷新页面
gulp.watch(dist+"/**/*.css").on("change", reload);
});
// 监听事件
gulp.task('default', ['css', 'js', 'image', 'html', 'copy', 'server'])
# 安装各种包
cnpm install
# 运行gulp
gulp