Last active
September 28, 2015 01:39
-
-
Save mikeobrien/3366b90434b18d583170 to your computer and use it in GitHub Desktop.
Bundler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var glob = require('glob'); | |
var useref = require('node-useref'); | |
var concat = require('gulp-concat'); | |
var through = require('through2'); | |
// Yes there are gulp plugins out there that do exactly this | |
// but ran into issues with a number of them so gave up and | |
// wrote this. gulp-usemin used to work perfectly but recent | |
// changes broke where it saves results and the repo owner | |
// doesen't seem very active. | |
module.exports = function(source, target, pipeline) { | |
return Promise.all(glob.sync(source).map(function(htmlFile) { | |
var promises = []; | |
var basePath = path.dirname(htmlFile); | |
var targetHtmlFile = target == '.' ? htmlFile : | |
path.join(basePath, target); | |
var result = useref(fs.readFileSync(htmlFile).toString()); | |
var html = result[0]; | |
for (resourceType in result[1]) { | |
var resource = result[1][resourceType]; | |
for (block in resource) { | |
var assets = resource[block].assets.map(function(x) { | |
return path.join(basePath, x); | |
}); | |
var pipe = gulp.src(assets); | |
if (pipeline[resourceType]) { | |
pipeline[resourceType].forEach(function(plugin) { | |
pipe = pipe.pipe(plugin == 'concat' ? | |
concat(path.basename(block)) : plugin); | |
}); | |
} | |
var writeHtmlFile = function(oldAssetName) { | |
return through.obj(function (file, enc, cb) { | |
var newAssetName = path.basename(file.path); | |
html = html.replace(new RegExp('(["/])' + oldAssetName + | |
'(["?])', 'gi'), '$1' + newAssetName + '$2'); | |
fs.writeFileSync(targetHtmlFile, html); | |
cb(null, file); | |
}); | |
}; | |
promises.push(pipe.pipe(writeHtmlFile(path.basename(block))) | |
.pipe(gulp.dest(path.join(basePath, path.dirname(block))))); | |
} | |
} | |
return promises; | |
})); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var assetBundler = require('./asset-bundler'); | |
var uglify = require('gulp-uglify'); | |
var minifyCss = require('gulp-minify-css'); | |
var rev = require('gulp-rev'); | |
var removeBom = require('fd-gulp-removebom'); | |
gulp.task('default', function() { | |
return assetBundler('src/*/index.html', '.', { | |
css: [ minifyCss(), 'concat', rev() ], | |
js: [ removeBom(), 'concat', uglify(), rev() ] | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment