Skip to content

Instantly share code, notes, and snippets.

@FlandersBurger
Last active October 18, 2017 14:51
Show Gist options
  • Save FlandersBurger/98c3bc807bbb389b7d7d7350357d15c5 to your computer and use it in GitHub Desktop.
Save FlandersBurger/98c3bc807bbb389b7d7d7350357d15c5 to your computer and use it in GitHub Desktop.
Gulp task to make gulp wiredep use all minified (.min/-min) files
/*
* Author: Laurent Debacker
* https://belgocanadian.com
* Creation Date: 18-Oct-2017
* Description: Gulp task to make gulp wiredep use all minified (.min/-min) files
* Note: Some bower componenents do not have a bower.json file. For those the minified files will not be picked. Alternatively you can create a bower.json file for those.
*/
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var bower = require('gulp-bower');
var wiredep = require('gulp-wiredep');
gulp.task('bower', function() {
//Get all the bower.json files in "bower_components"
var bowerFiles = getFiles('bower_components');
for (var i in bowerFiles) {
//Read every bower.json file
var bower = JSON.parse(fs.readFileSync(bowerFiles[i], 'utf8'));
//Save the path of where the bower.json file is located
var bowerPath = path.resolve(bowerFiles[i].substring(0, bowerFiles[i].lastIndexOf('/')));
if (bower.main) {
//bower.main is either a string or an array
if (typeof(bower.main) === 'object') {
for (var j in bower.main) {
bower.main[j] = checkMinFile(bowerPath, bower.main[j]);
}
} else {
bower.main = checkMinFile(bowerPath, bower.main);
}
//Write the new main (pointing to the min file) in the bower file
fs.writeFileSync(bowerFiles[i], JSON.stringify(bower), 'utf8');
}
}
//This executes the standard wiredep functionality t
gulp.src('./layouts/index.html')
.pipe(wiredep())
.pipe(gulp.dest('dist'));
});
//Function that determines whether there is a .min/-min alternative
function checkMinFile (bowerPath, filePath) {
var initialPath = filePath.substring(0, filePath.lastIndexOf('/') + 1);
var filename = filePath.substring(filePath.lastIndexOf('/') + 1);
//Only execute if the current file is not already a minified file
if (filePath.indexOf('min.') <= 0) {
//Check .min
var minFilePath = filePath.substring(0, filePath.lastIndexOf('.')) + '.min' + filePath.substring(filePath.lastIndexOf('.'));
if (fs.existsSync(path.resolve(bowerPath + '/' + minFilePath))) {
filename = filename.substring(0, filename.lastIndexOf('.')) + '.min' + filename.substring(filename.lastIndexOf('.'));
} else {
//Check -min
minFilePath = filePath.substring(0, filePath.lastIndexOf('.')) + '-min' + filePath.substring(filePath.lastIndexOf('.'));
if (fs.existsSync(path.resolve(bowerPath + '/' + minFilePath))) {
filename = filename.substring(0, filename.lastIndexOf('.')) + '-min' + filename.substring(filename.lastIndexOf('.'));
}
}
}
return initialPath + filename;
}
//Generic recursive function to loop through a folder structure
function getFiles (dir, files_){
files_ = files_ || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getFiles(name, files_);
} else {
//Adjusted here to only add bower.json files
if (name.indexOf('bower.json') > 0) {
files_.push(name);
}
}
}
return files_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment