Skip to content

Instantly share code, notes, and snippets.

@shrpne
Created January 16, 2018 23:17
Show Gist options
  • Save shrpne/961c06e4f42068eb4cd4756976679d6c to your computer and use it in GitHub Desktop.
Save shrpne/961c06e4f42068eb4cd4756976679d6c to your computer and use it in GitHub Desktop.
// common
const gulp = require('gulp');
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const gutil = require('gulp-util');
// js
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const concat = require('gulp-concat');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
// css
const less = require('gulp-less');
const postcss = require('gulp-postcss');
const postcssNormalize = require('postcss-normalize');
const autoprefixer = require('autoprefixer');
const cleanCss = require('gulp-clean-css');
// images
const cache = require('gulp-cache');
const imagemin = require('gulp-imagemin');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
let paths = {
src: {
less: 'resources/assets/less/*.less',
js: 'resources/assets/js/*.{js,vue}',
img: ['resources/assets/img/**/*.{png,jpg,gif,svg}', '!resources/assets/img/high-compress/**/*'],
imgHighCompress: 'resources/assets/img/high-compress/**/*.{png,jpg,gif,svg}',
},
dest: {
css: 'public/css/',
js: 'public/js',
img: 'public/img/',
},
watch: {
less: 'resources/assets/less/**/*.less',
js: 'resources/assets/js/**/*.{js,vue}',
}
};
// LESS
gulp.task('styles', function () {
return gulp.src(paths.src.less)
.pipe(plumber({errorHandler: onError}))
.pipe(less())
.pipe(postcss([
autoprefixer(),
postcssNormalize(),
]))
.pipe(cleanCss())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(paths.dest.css));
});
// JS
gulp.task("scripts", function() {
return gulp.src(paths.src.js)
.pipe(plumber({errorHandler: onError}))
.pipe(webpackStream(require('./webpack.config.js'), webpack))
.pipe(babel())
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(paths.dest.js));
});
gulp.task("scripts-fast", function() {
return gulp.src(paths.src.js)
.pipe(plumber({errorHandler: onError}))
.pipe(webpackStream(require('./webpack.config.js'), webpack))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(paths.dest.js));
});
// IMG
gulp.task('imagemin', function () {
return gulp.src(paths.src.img)
.pipe(plumber({errorHandler: onError}))
.pipe(cache(
imagemin([
imagemin.gifsicle({interlaced: true}),
mozjpeg({quality: 90}),
//pngquant(),
//imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo()
], {
verbose: true
}), {
fileCache: new cache.Cache({tmpDir: 'storage/', cacheDirName: 'gulp-cache'}),
name: 'default',
}))
.pipe(gulp.dest(paths.dest.img));
});
gulp.task('imagemin-high-compress', function () {
return gulp.src(paths.src.imgHighCompress)
.pipe(plumber({errorHandler: onError}))
.pipe(cache(
imagemin([
imagemin.gifsicle({interlaced: true}),
mozjpeg({quality: 80}),
pngquant(),
//imagemin.jpegtran({progressive: true}),
//imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo()
], {
verbose: true
}), {
fileCache: new cache.Cache({tmpDir: 'storage/', cacheDirName: 'gulp-cache'}),
name: 'high-compress',
}))
.pipe(gulp.dest(paths.dest.img));
});
// Полная сборка с вотчем
gulp.task('default', ['styles', 'scripts', 'imagemin', 'imagemin-high-compress'], function() {
gulp.watch(paths.watch.js, ['scripts']);
gulp.watch(paths.watch.less, ['styles']);
gulp.watch(paths.src.img, ['imagemin']);
gulp.watch(paths.src.imgHighCompress, ['imagemin-high-compress']);
});
// Полная сборка без вотча
gulp.task('once', ['styles', 'scripts', 'imagemin', 'imagemin-high-compress']);
// Сборка без картинок с вотчем
gulp.task('default-no-image', ['styles', 'scripts'], function() {
gulp.watch(paths.watch.js, ['scripts']);
gulp.watch(paths.watch.less, ['styles']);
});
// Быстрая сборка без картинок, бабеля, минификации с вотчем
gulp.task('fast', ['styles', 'scripts-fast'], function() {
gulp.watch(paths.watch.js, ['scripts-fast']);
gulp.watch(paths.watch.less, ['styles']);
});
// Ошибки
let onError = function(error) {
gutil.log([
(error.name + ' in ' + error.plugin).bold.red,
'',
error.message,
''
].join('\n'));
gutil.beep();
this.emit('end');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment