Skip to content

Instantly share code, notes, and snippets.

@gnosis23
Last active February 26, 2017 06:51
Show Gist options
  • Save gnosis23/e2a5b5c936b50c170a891858ef22dcbb to your computer and use it in GitHub Desktop.
Save gnosis23/e2a5b5c936b50c170a891858ef22dcbb to your computer and use it in GitHub Desktop.
gulp.js sample
/* File: gulpfile.js */
var gulp = require('gulp'),
less = require('gulp-less'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename');
var minifycss = require('gulp-minify-css');
var watchPath = require('gulp-watch-path');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var combiner = require('stream-combiner2');
var rev = require('gulp-rev');
var uglify = require('gulp-uglify');
gulp.task('watch', function () {
gulp.watch('./src/less/**/*.less', ['less']);
});
var handleError = function (err) {
var colors = gutil.colors;
console.log('\n');
gutil.log(colors.red('Error!'));
gutil.log('fileName: ' + colors.red(err.fileName));
gutil.log('lineNumber: ' + colors.red(err.lineNumber));
gutil.log('message: ' + err.message);
gutil.log('plugin: ' + colors.yellow(err.plugin))
};
gulp.task('watchLess', function () {
gulp.watch('src/less/**/*.less', function (event) {
var paths = watchPath(event, 'src/less/', 'dist/styles/');
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath);
gutil.log('Dist ' + paths.distPath);
var combined = combiner.obj([
gulp.src('src/less/**/*.less'),
sourcemaps.init(),
less(),
minifycss(),
sourcemaps.write('./'),
gulp.dest('dist/styles/')
// rev(),
// gulp.dest('rev/styles/'),
// rev.manifest(),
// gulp.dest('rev/styles/')
]);
combined.on('error', handleError)
})
});
gulp.task('watchJs', function () {
gulp.watch('src/js/**/*.js', function (event) {
var paths = watchPath(event, 'src/js', 'dist/js');
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath);
gutil.log('Dist ' + paths.distPath);
var combined = combiner.obj([
gulp.src('src/js/**/*.js'),
sourcemaps.init(),
uglify(),
sourcemaps.write('./'),
gulp.dest('dist/js/')
])
});
});
gulp.task('watchHtml', function () {
gulp.watch('src/template/**/*.html', function (event) {
var paths = watchPath(event, 'src/template', 'dist');
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath);
gutil.log('Dist ' + paths.distPath);
var combined = combiner.obj([
gulp.src('src/template/**/*.html'),
gulp.dest('dist/')
])
});
});
var revCollector = require('gulp-rev-collector');
var minifyHTML = require('gulp-minify-html');
gulp.task('rev', function () {
return gulp.src(['rev/**/*.json', 'src/template/**/*.html'])
.pipe( gulp.dest('dist') )
.pipe( revCollector({
replaceReved: true,
dirReplacements: {
'/dist/styles': '/rev/styles'
}
}) )
.pipe( gulp.dest('rev') );
});
gulp.task('copy', function () {
gulp.src('src/vendor/**/*.js').pipe(gulp.dest('dist/js/vendor/'));
});
gulp.task('default', ['copy', 'watchLess', 'watchJs', 'watchHtml']);
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'));
});
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(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: 1234
});
open('http://localhost:1234');
gulp.watch(app.srcPath + 'script/**/*.js', ['js']);
gulp.watch(app.srcPath + '**/*.html', ['html']);
gulp.watch(app.srcPath + 'data/**/*.json', ['json']);
gulp.watch(app.srcPath + 'style/**/*.less', ['less']);
gulp.watch(app.srcPath + 'image/**/*', ['image']);
});
gulp.task('default', ['serve']);
{
"name": "es6",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.1",
"gulp-connect": "^5.0.0",
"gulp-cssmin": "^0.1.7",
"gulp-imagemin": "^3.1.1",
"gulp-less": "^3.3.0",
"gulp-load-plugins": "^1.5.0",
"gulp-plumber": "^1.1.0",
"gulp-uglify": "^2.0.1",
"open": "0.0.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment