Create static asset bundles from a config file: a common interface to combining, minifying, revisioning and more. Stack agnostic. Production ready.
By default uses the following gulp modules under the covers when creating bundles:
This project's stream architecture also allows you to plugin any gulp transform you wish.
$ npm install gulp-bundle-assets --save-dev
Create the following files:
// gulpfile.js
var gulp = require('gulp'),
bundle = require('gulp-bundle-assets');
gulp.task('bundle', function() {
return gulp.src('./bundle.config.js')
.pipe(bundle())
.pipe(gulp.dest('./public'));
});
// bundle.config.js
module.exports = {
bundle: {
main: {
scripts: [
'./content/js/foo.js',
'./content/js/baz.js'
],
styles: './content/**/*.css'
},
vendor: {
scripts: './bower_components/angular/angular.js'
}
},
copy: './content/**/*.{png,svg}'
};
Then, calling
$ gulp bundle
Will result in the following folder structure:
-- public
|-- content
| |-- fonts
| |-- images
`main-8e6d79da08.css
`main-5f17cd21a6.js
`vendor-d66b96f539.js
See the examples folder for many other config options. The full example shows most all available options.
Also check out our api docs.
You can programmatically render your bundles into your view via
your favorite templating engine
and the resulting bundle.result.json
file. To generate the bundle.result.json
, add a call to bundle.results
:
// gulpfile.js
var gulp = require('gulp'),
bundle = require('gulp-bundle-assets');
gulp.task('bundle', function() {
return gulp.src('./bundle.config.js')
.pipe(bundle())
.pipe(bundle.results('./')) // arg is destination of bundle.result.json
.pipe(gulp.dest('./public'));
});
Which results in a bundle.result.json
file similar to:
{
"main": {
"styles": "<link href='main-8e6d79da08.css' media='screen' rel='stylesheet' type='text/css'/>",
"scripts": "<script src='main-5f17cd21a6.js' type='text/javascript'></script>"
},
"vendor": {
"scripts": "<script src='vendor-d66b96f539.js' type='text/javascript'></script>",
"styles": "<link href='vendor-23d5c9c6d1.css' media='screen' rel='stylesheet' type='text/css'/>"
}
}
The order of the bundles will be the same as the order in which they were specified in the config.
See here for a full example using hogan
NODE_ENV=production gulp bundle
could produce a set of bundles with minified src while just gulp bundle
would have unminified src gulp-less
, gulp-sass
, gulp-coffee
, etc to further transform your filesThere are a number of ways to bundle static assets for use in your webapp. Take for example: lumbar, brunch, webpack, browserify, optimizer, cartero, assetify, assets-packager, or simply a mashup of custom grunt or gulp plugins. All of these approaches are good in their own way but none of them did everything we needed:
gulp-bundle-assets
accomplishes all these goals and more. A main guiding
principle behind this project is to provide all necessary bundling functionality
while still being as flexible and customizable as possible.
result.json
ordering #71 (@PlasmaPower)gulp-less
3.1.0, gulp-coffee
2.3.2 and gulp-if
2.0.1gulp-clean-css
2.0.7 instead of deprecated gulp-minify-css
modulegulp-less
3.0.5 and gulp-uglify
1.5.3gulp-rev
4.0.0, gulp-less
3.0.3, gulp-sourcemaps
1.5.2gulp-minify-css
1.1.1 (@ZaleskiR)result.json
url separator on windows #52 (@gregorymaertens)bundle.watch
#47bundle.watch
(@roberto)gulp-rev
2.0.1, gulp-sourcemaps
1.2.8, gulp-uglify
1.0.1bundle.watch
for copy files #33bundle.watch
for bundles #26