|
| 1 | +const gulp = require('gulp') |
| 2 | +const copy = require('gulp-copy') |
| 3 | +const concat = require('gulp-concat') |
| 4 | +const sass = require('gulp-sass') |
| 5 | +const minifycss = require('gulp-clean-css') |
| 6 | +const autoprefixer = require('gulp-autoprefixer') |
| 7 | +const spriter = require('gulp-css-spriter') |
| 8 | +const uglify = require('gulp-uglify') |
| 9 | +const imagemin = require('gulp-imagemin') |
| 10 | +const pngquant = require('imagemin-pngquant') |
| 11 | +const minifyhtml = require('gulp-minify-html') |
| 12 | +const rev = require('gulp-rev') |
| 13 | +const revcollector = require('gulp-rev-collector') |
| 14 | +const eslint = require('gulp-eslint') |
| 15 | +const sequence = require('gulp-sequence') |
| 16 | +const sourcemaps = require('gulp-sourcemaps') |
| 17 | +const del = require('del') |
| 18 | + |
| 19 | +let paths = { |
| 20 | + jsComSrc: './src/js/common/*.js', |
| 21 | + jsAppSrc: './src/js/*.js', |
| 22 | + jsDest: '../public/static/js', |
| 23 | + cssSrc: './src/css/**/*', |
| 24 | + cssDest: '../public/static/css', |
| 25 | + imgSrc: './src/img/**/*', |
| 26 | + imgDest: '../public/static/img', |
| 27 | + htmlSrc: './src/html/**/*', |
| 28 | + htmlDest: '../public/static/html', |
| 29 | + libSrc: './src/libs/**/*', |
| 30 | + libDest: '../public/static', |
| 31 | + dist: '../public/static/**/*', |
| 32 | + // manifestFile: './rev-manifest.json' |
| 33 | +} |
| 34 | + |
| 35 | +gulp.task('scripts', ['scripts-common', 'scripts-app']) |
| 36 | + |
| 37 | +gulp.task('scripts-common', function() { |
| 38 | + return gulp.src(paths.jsComSrc) |
| 39 | + .pipe(sourcemaps.init()) |
| 40 | + .pipe(concat('common.min.js')) |
| 41 | + .pipe(uglify()) |
| 42 | + // .pipe(rev()) // 文件名加MD5后缀 |
| 43 | + .pipe(sourcemaps.write('./')) |
| 44 | + .pipe(gulp.dest(paths.jsDest)) |
| 45 | + // .pipe(rev.manifest()) // 生成一个rev-manifest.json |
| 46 | + // .pipe(gulp.dest('./')) // 将 rev-manifest.json 保存到 ./ 目录内 |
| 47 | +}) |
| 48 | + |
| 49 | +gulp.task('scripts-app', function() { |
| 50 | + return gulp.src(paths.jsAppSrc) |
| 51 | + .pipe(sourcemaps.init()) |
| 52 | + .pipe(concat('app.min.js')) |
| 53 | + .pipe(uglify()) |
| 54 | + // .pipe(rev()) // 文件名加MD5后缀 |
| 55 | + .pipe(sourcemaps.write('./')) |
| 56 | + .pipe(gulp.dest(paths.jsDest)) |
| 57 | + // .pipe(rev.manifest()) // 生成一个rev-manifest.json |
| 58 | + // .pipe(gulp.dest('./')) // 将 rev-manifest.json 保存到 ./ 目录内 |
| 59 | +}) |
| 60 | + |
| 61 | +// gulp.task('rev', ['scripts'], function() { |
| 62 | +// gulp.src(['./rev-manifest.json', '../server/views/index.hbs']) // 读取 rev-manifest.json 文件以及需要进行文件名替换的文件 |
| 63 | +// .pipe(revcollector()) // 执行文件内文件名的替换 |
| 64 | +// .pipe(gulp.dest('../server/views')) // 替换后的文件输出的目录 |
| 65 | +// }) |
| 66 | + |
| 67 | +gulp.task('images', function() { |
| 68 | + return gulp.src(paths.imgSrc) |
| 69 | + .pipe(imagemin({ |
| 70 | + optimizationLevel: 5, |
| 71 | + progressive: true, |
| 72 | + use: [pngquant()] |
| 73 | + })) |
| 74 | + .pipe(gulp.dest(paths.imgDest)) |
| 75 | +}) |
| 76 | + |
| 77 | +gulp.task('styles', function() { |
| 78 | + return gulp.src(paths.cssSrc) |
| 79 | + .pipe(sass()) // 编译scss |
| 80 | + .pipe(concat('app.min.css')) // 合并scss |
| 81 | + .pipe(autoprefixer()) // 浏览器厂商前缀 {browsers:["> 1%","Firefox >= 10","ie >= 9","iOS >= 4","Chrome >= 10"],cascade:false} |
| 82 | + // .pipe(spriter({ |
| 83 | + // // The path and file name of where we will save the sprite sheet |
| 84 | + // 'spriteSheet': paths.imgDest + '/spritesheet.png', |
| 85 | + // // Because we don't know where you will end up saving the CSS file at this point in the pipe, |
| 86 | + // // we need a litle help identifying where it will be. |
| 87 | + // 'pathToSpriteSheetFromCSS': '../img/spritesheet.png' |
| 88 | + // })) |
| 89 | + .pipe(minifycss()) // 压缩css |
| 90 | + .pipe(gulp.dest(paths.cssDest)) |
| 91 | +}) |
| 92 | + |
| 93 | +gulp.task('htmls', function() { |
| 94 | + gulp.src(paths.htmlSrc) |
| 95 | + .pipe(minifyhtml({ comments: false })) |
| 96 | + .pipe(gulp.dest(paths.htmlDest)) |
| 97 | +}) |
| 98 | + |
| 99 | +gulp.task('lint', function () { |
| 100 | + return gulp.src(['./src/js/**/*', '!node_modules/**']) |
| 101 | + .pipe(eslint()) |
| 102 | + .pipe(eslint.format()) |
| 103 | + .pipe(eslint.failAfterError()) |
| 104 | +}) |
| 105 | + |
| 106 | +gulp.task('clean', function(){ |
| 107 | + del([paths.dist], { force: true }) |
| 108 | +}) |
| 109 | + |
| 110 | +gulp.task('libs', function() { |
| 111 | + gulp.src(paths.libSrc) |
| 112 | + .pipe(copy(paths.libDest, { prefix: 1 })) |
| 113 | + .pipe(gulp.dest(paths.libDest + '/libs')) |
| 114 | +}) |
| 115 | + |
| 116 | +gulp.task('watch', function() { |
| 117 | + gulp.watch(paths.jsComSrc, ['scripts-common']) |
| 118 | + gulp.watch(paths.jsAppSrc, ['scripts-app']) |
| 119 | + gulp.watch(paths.cssSrc, ['styles']) |
| 120 | + gulp.watch(paths.imgSrc, ['images']) |
| 121 | + gulp.watch(paths.htmlSrc, ['htmls']) |
| 122 | + gulp.watch(paths.libSrc, ['libs']) |
| 123 | +}) |
| 124 | + |
| 125 | +gulp.task('build', ['clean'], function (cb) { |
| 126 | + // gulp.start('scripts', 'styles', 'images', 'htmls', 'libs') |
| 127 | + sequence('scripts', 'styles', 'images', 'htmls', 'libs')(cb) |
| 128 | +}) |
| 129 | + |
| 130 | +gulp.task('build-no-clean', function (cb) { |
| 131 | + gulp.start('scripts', 'styles', 'images', 'htmls', 'libs') |
| 132 | +}) |
| 133 | + |
| 134 | +gulp.task('default', sequence('build', 'watch')) |
| 135 | + |
| 136 | + |
| 137 | +// function SpriterGroup(pathArr) { |
| 138 | +// for (let i = 0; i < pathArr.length; i++) { |
| 139 | +// gulp.src(pathArr[i]) |
| 140 | +// .pipe(spriter({ |
| 141 | +// 'spriteSheet' : paths.imgDest + '/spriteSheet_' + i +'.png', |
| 142 | +// 'pathToSpriteSheetFormCss' : '../img/spriteSheet_' + i + '.png' |
| 143 | +// })) |
| 144 | +// .pipe(gulp.dest(paths.cssDest)) |
| 145 | +// } |
| 146 | +// } |
0 commit comments