Skip to content

Instantly share code, notes, and snippets.

@kurtextrem
Created June 26, 2014 07:08
Show Gist options
  • Save kurtextrem/79da2dc2061d880088de to your computer and use it in GitHub Desktop.
Save kurtextrem/79da2dc2061d880088de to your computer and use it in GitHub Desktop.
Gulp minify
node_modules/
'use strict';
// Include Gulp & Tools We'll Use
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var uglify = require('gulp-uglifyjs');
var runSequence = require('run-sequence');
// minify json
gulp.task('json', function () {
return gulp.src('src/**/**/*.json')
.pipe($.jsonminify())
.pipe(gulp.dest('dist'))
.pipe($.size({
title: 'json'
}));
})
// build zip for webstore
gulp.task('zip', function () {
return gulp.src('dist/*').pipe($.zip('dist.zip')).pipe(gulp.dest('dist'))
})
// copies images
gulp.task('copy', function () {
return gulp.src(['src/doc/**', 'src/img/**']).pipe(gulp.dest('dist'))
})
// Scan Your HTML For Assets & Optimize Them
gulp.task('html', function () {
return gulp.src('src/*.html')
.pipe($.useref.assets())
// Concatenate And Minify JavaScript
.pipe($.if ('*.js', uglify('', {
mangle: {
toplevel: true,
screw_ie8: true
},
compress: {
screw_ie8: true,
sequences: true,
properties: true,
dead_code: true,
drop_debugger: true,
comparisons: true,
conditionals: true,
evaluate: true,
booleans: true,
loops: true,
unused: false,
hoist_funs: true,
if_return: true,
join_vars: true,
cascade: true,
negate_iife: true,
drop_console: true
}
})))
// for faster execution
.pipe($.if('*.js', $.replace('"use strict";', '')))
// Concatenate And Minify Styles
.pipe($.if ('*.css', $.csso()))
.pipe($.useref.restore())
.pipe($.useref())
// Minify Any HTML
.pipe($.minifyHtml())
// Output Files
.pipe(gulp.dest('dist'))
.pipe($.size({
title: 'html'
}));
});
// Clean Output Directory
gulp.task('clean', function (cb) {
rimraf('dist', cb);
});
// Build Production Files
gulp.task('build', function (cb) {
runSequence(['json', 'html', 'copy'], 'zip', cb);
});
// Default Task
gulp.task('default', ['clean'], function (cb) {
gulp.start('build', cb);
});
{
"devDependencies": {
"gulp": "^3.6.0",
"gulp-csso": "^0.2.6",
"gulp-if": "^1.2.1",
"gulp-load-plugins": "^0.5.0",
"gulp-minify-html": "^0.1.3",
"gulp-size": "^0.4.0",
"gulp-uglifyjs": "^0.3.0",
"gulp-uncss": "^0.4.4",
"gulp-useref": "^0.4.3",
"rimraf": "^2.2.8",
"run-sequence": "^0.3.6",
"gulp-replace": "^0.3.0",
"gulp-jsonminify": "^0.0.1",
"gulp-zip": "^0.4.0"
},
"engines": {
"node": ">=0.10.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment