concept bundle in category javascript

appears as: bundle
JavaScript Application Design: A Build First Approach

This is an excerpt from Manning's book JavaScript Application Design: A Build First Approach.

In so many words, then, asset bundling consists of literally appending each of your files at the end of the previous one. In this way, you might bundle together all of your CSS or all of your JavaScript. Fewer HTTP requests yields better performance, and that’s reason enough to warrant a static asset bundling build step. Figure 2.5 examines the interaction between a human and a site that uses a bundle, and one that doesn’t, and how each affects network connections.

Once you’ve created both the routes.js test module and the runner.html test runner, you should create a Grunt task to build the bundle.

Listing 8.30. Creating a custom Browserify task
var fs = require('fs'); var glob = require('glob'); var mkdirp = require('mkdirp'); var browserify = require('browserify'); var proxyquire = require('proxyquireify'); function browserifyTests () { var done = this.async(); #1 var dir = __dirname + '/test/build'; mkdirp.sync(dir); var bundle = browserify() #2 .transform('brfs') #4 .plugin(proxyquire.plugin); #5 glob .sync('./test/*.js') #6 .map(resolve) #7 .reduce(include, bundle) #8 .bundle() #9 .pipe(fs.createWriteStream(dir + '/test-bundle.js')) #10 .on('done', done); #11 function include (bundle, file) { bundle.require(file, { entry: true }); #12 return bundle; } } function resolve (file) { return require.resolve(file); } grunt.registerTask('browserify_tests', browserifyTests); #1: This is an asynchronous task; call done when it ends. #2: Create a directory structure so the task works even if the directories don’t exist yet. #3: This is the public interface to the Browserify API. #4: The brfs transform is used when compiling Mustache view templates into their JavaScript counterparts. #5: The proxyquireify plugin allows us to intercept require calls and stub implementations. #6: Globbing gives you all the test entry files. Currently you have routes.js. #7: Map the relative globbed paths to absolute paths. #8: Call bundle.require for each test module, while returning the bundle to enable chaining. #9: Create the browser-enabled JavaScript bundle. #10: Pipe that bundle into a file. #11: When the data transfer is completed, tell Grunt the task has finished. #12: Using bundle.require allows external access to the module. The entry flag adds the module as an entry point.