Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var gulp = require('gulp'),
path = require('path'),
folder = require('gulp-folders'),
gulpIf = require('gulp-if'),
insert = require('gulp-insert'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
notify = require('gulp-notify'),
rename = require('gulp-rename'),
source_folder = 'source/js',
destination_folder = 'deploy/j',
rename_serviceworker = rename({
dirname: "../"
});
gulp.task('scripts', folder(source_folder, function(folder){
function createErrorHandler( name )
{
return function (err) {
console.error('Error from ' + name + ' in compress task', err.toString());
};
}
return gulp.src(path.join(source_folder, folder, '*.js'))
.pipe(concat(folder + '.js'))
.pipe(insert.prepend('(function(window,document){'))
.pipe(insert.append('}(this,this.document));'))
.pipe(insert.transform(function(contents, file){
// insert a build time variable
var build_time = (new Date()).getTime() + '';
return contents.replace( '{{BUILD_TIME}}', build_time );
}))
.pipe(gulpIf(folder=='serviceworker',rename_serviceworker))
.pipe(gulp.dest(destination_folder))
.pipe(gulpIf(folder!='serviceworker',uglify().on('error', createErrorHandler('uglify'))))
.pipe(gulpIf(folder!='serviceworker',rename({suffix: '.min'})))
.pipe(gulpIf(folder!='serviceworker',gulp.dest(destination_folder)))
.pipe(notify({ message: 'Scripts task complete' }));
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.