Skip to content

Instantly share code, notes, and snippets.



Last active Feb 3, 2016
What would you like to do?
build only if modified: gulp browserify babelify sourcemap uglify print size (example)
//example - change the paths
function CompileJsAtPath(jsFile, debug){
return new Promise(function(resolve, reject){
//best example is from
const uglify = require('gulp-uglify');
const sourcemaps = require('gulp-sourcemaps');
//get current filename
const fileName = jsFile.split('/').pop();
//init browserify for this file
var b = require('browserify')({
entries: `.${jsFile}`,
debug: debug, //sourcemap
paths: ['./node_modules','./_/logic']
//exclude core libs we reference globally
//get reference to pipeline
var op =
//perform browserify
//report browserify error
.on('error', function(err) {
OnError('JS: ' + jsFile, err);
//adjust sourcemap to saved location
if (debug)
//convert to stream
//buffer stream for sourcemap
//load in the sourcemap from browserify
.pipe(sourcemaps.init({loadMaps: true}))
//uglify when we want to (takes a few extra seconds)
.pipe( uglify({
compress: false,
ie_proof: false
}) )
//write sourcemap to separate location
.pipe(sourcemaps.write('./', {sourceRoot: '/', includeContent: false}))
.pipe(size({ showFiles: true, showTotal: false }))
//write out the js file to build
.on('finish', function(){
OnComplete('JS: ' + jsFile);
function RunWhenNewer(globsToMonitor, outputFileTimeCheck, taskToRun, cb) {
return gulp.src(globsToMonitor, {read: false})
.pipe(gutil.buffer(function(err, files) {
//handle error
if (err)
//if any files changed, run task
console.log(globsToMonitor, outputFileTimeCheck, files);
if (files.length) {
gulp.series(taskToRun, function(done) { cb(); done(); })();
else {
const pageAScriptGlobs = ['_/logic/**/*.js','!_/logic/build/**', '!_/logic/OtherPage/**'];
//specific task to build scripts for 'Page A' only when required
gulp.task('script-A-ifneeded', function(done){
RunWhenNewer(pageAScriptGlobs, '_/logic/build/page-a.js', 'script-ide', done);
//actual build task to build scripts for 'Page A'
gulp.task('script-a', function(){
return CompileJsAtPath('/_/logic/page-a.js', true)
//default only build what's changed
gulp.task('default', gulp.parallel('script-a-ifneeded'));
//option to force build everything
gulp.task('force', gulp.parallel('script-a'));
gulp.task('watch', function(){
var debounce = require('debounce');
//we must debounce since the debouncedelay option for gulp watch only debounces exact file duplications, and here we only want to run the operation once for each type of file since the operation builds every file in the matching glob
//Page 'A' js change
debounce(gulp.parallel('script-a'), 50));
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.