Skip to content

Instantly share code, notes, and snippets.

@schmuli
Last active December 29, 2017 15:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmuli/6422753589261e097a83 to your computer and use it in GitHub Desktop.
Save schmuli/6422753589261e097a83 to your computer and use it in GitHub Desktop.
Minify and Concat all bower components
{
"name": "gulp-bower-components",
"version": "1.0.0",
"description": "Testing gulp and bower dependencies",
"authors": [
"Schmulik Raskin"
],
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "~1.3.5",
"angular-ui-router": "~0.2.13",
"traceur-runtime": "~0.0.78",
"SizzleStats": "~0.1.3"
}
}

This is an example gulpFile that can be used to minify and concat all bower components marked as dependencies. Also includes the bower.json and package.json files for the project.

The 'vendors' task will extract all bower components, using main-bower-files, but replaces any files that have a matching .min file that exists. Any unminified files are then minified using Uglify, and then all files are concatenated into a single vendors.js file.

The bower.json deliberately includes the SizzleStats dependency, as this component doesn't include a minified file.

var path = require('path');
var fs = require('fs');
var gulp = require('gulp');
var utils = require('gulp-util');
var log = utils.log; // Can be used for logging
gulp.task('vendors', function () {
// Get all bower components, but use the minified file
// if it already exists
var bowerFiles = require('main-bower-files');
var files = bowerFiles().map(convertFile);
// Create a filter for files that are not minified
var filter = require('gulp-filter');
var unminified = filter(function (file) {
var basename = path.basename(file.path);
var min = basename.indexOf('.min') === -1;
return min;
});
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
gulp.src(files)
.pipe(unminified) // Filter out minified files
.pipe(uglify()) // Minify unminified files
.pipe(unminified.restore()) // Restore minified files
.pipe(concat('vendors.js')) // Concat all files into one file
.pipe(gulp.dest('dist')); // Write to destination folder
});
/**
* Convert the path to a JS file to the minified version, if it exists.
*
* @param file {String} The full path to the JS file
* @returns {String} The minified file path if it exists, otherwise the
* original file path
*/
function convertFile(file) {
var ext = path.extname(file);
var min = file.substr(0, file.length - ext.length);
min += '.min' + ext;
return fs.existsSync(min) ? min : file;
}
{
"name": "gulp-bower-components",
"version": "1.0.0",
"description": "Testing gulpand bower dependencies",
"author": "Schmulik Raskin",
"license": "MIT",
"devDependencies": {
"gulp": "^3.8.10",
"gulp-concat": "^2.4.2",
"gulp-filter": "^1.0.2",
"gulp-uglify": "^1.0.1",
"gulp-util": "^3.0.1",
"main-bower-files": "^2.4.1",
"through2": "^0.6.3"
}
}
@alwin-rewedigital
Copy link

You could use: bowerMainJavaScriptFilesObject.minifiedNotFound; like described on https://github.com/frodefi/bower-main, so you don't have to filter yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment