The lazo-optimizer was designed to bundle Lazo application JavaScript and CSS. The default implementation creates a single bundle for JavaScript and single bundle for CSS. There are also a number of utility functions that can be leveraged to support custom bundling.
The default options for bundleJS
,
bundleCSS
, and all functions that accept an options
object are:
// the path to lazo; used to get the requirejs config for lazo
lazoPath: require.resolve('lazo') || 'node_modules/lazo',
// the path to the application to be bundled
appPath: '.',
// default requirejs configuration overrides
config: {},
// used to filter out any files from the bundle
jsFilter: function (includes, files, paths) {
return includes;
},
// files to be included in the build
includeExtensions: ['.hbs', '.json', '.js'],
// loader to be used for a file type by extension
loaders: {
'.hbs': 'text',
'.json': 'json'
},
// used to filter CSS files
cssFilter: function (file) {
return path.extname(file) === '.css';
},
// used to sort the order in which CSS files are concatenated
sortCss: function (files) {
return files;
},
// minify the concatenated CSS
minifyCss: true,
// where to write the CSS bundle
cssOut: 'app/bundles/application.css',
// exclude the JS bundle when bundling (app/bundles/application.js)
excludeBundles: function (files) {
return files.filter(function (file) {
return file.indexOf('app/bundles/') === -1;
});
}
The primary interface for bundling JavaScript is the bundleJS
function:
bundleJS
delgates to the Require.js optimizer using a default configuration. Any configuration values can be overriden to handle your specific use case. Please defer to Require.js optimizer documentaton for further help.
var optimizer = require('lazo-optimizer');
// arguments: options object and callback
optimizer.bundleJS({
appPath: 'path/to/your/application'
}, function (err, buildResponse) {
if (err) {
throw err;
}
console.log(buildResponse);
});
JavaScript bundling related utilities.
Note - All callbacks return err
, [results]
.
Copies the text
, json
, and l
(loaders) to the root of an application for
the Require.js optimizer.
lazoPath
(String): Path to the Lazo node module directory.appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the loaders have been copied.
Deletes the text
, json
, and l
(loaders) from the root of an application.
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the loaders have been deleted.
Gets the path configuration for Lazo and an application (reads
conf.json). Delegates to
getLazoPaths
and getAppPaths
merging the results.
lazoPath
(String): Path to the Lazo node module directory.appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the paths have been resolved.
Gets the path configuration for an application (reads conf.json).
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the paths have been resolved.
Gets the path configuration for Lazo. Replaces all module id values with "empty:".
lazoPath
(String): Path to the Lazo node module directory.callback
(Function): Function to be executed once the paths have been resolved.
Reads the application directory returning an array of files. Excludes server/**/*
.
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the application directory has been read.
Prefixes file path with loader, e.g., text!app/app.json
, if required.
filePath
(String): File path to prefix.loaders
(Object): Loaders map, (options.loaders
).
(String): File path with loader prefix if file extension maps to a loader.
Gets the default configuration for the Require.js optimizer.
options
(Object): Overrides for default options.callback
(Function): Function to be executed once the configuration has been generated.
Removes files from the includes that have a module id in the paths configuration.
files
(Array): Include file paths.paths
(Object): Paths configuration.
(Array): Filtered file paths.
Merges the default configuration with an overrides in options.config
.
options
(Object): Overrides for default options.callback
(Function): Function to be executed once the configuration has been merged.
The primary interface for bundling CSS is the bundleCss
function:
var optimizer = require('lazo-optimizer');
// arguments: options object and callback
optimizer.bundleCss({
appPath: 'path/to/your/application'
}, function (err, buildResponse) {
if (err) {
throw err;
}
console.log(buildResponse);
});
The default options for bundleCss
and all functions that accept an options
object
are defined here.
CSS bundling related utilities.
Note - All callbacks return err
, [results]
.
Concat and optionally minify CSS.
css
(Array): Contains CSS definitions,{ path: 'path/to/css/file.css', contents: 'css file contents' }
.minify
(Boolean): Minify the CSS.callback
(Function): Function to be executed once the CSS has been concatenated and optionally minified.
Reads CSS files.
appPath
(String): Path to the application directory.files
(Array): File paths for CSS files to be read.callback
(Function): Function to be executed once the CSS files have been read.
Resolves image URLs is CSS files to absolute paths for an application.
CssStr
(String): CSS file contents.cssFilePath
(Object): CSS file path relative to the application.
(String): CSS file contents with modified image URLs.
Gets the CSS file paths for an application.
appPath
(String): Path to the application directory.filter
(Function): Filter files read from the application directory. Seeoptions.cssFilter
for an example.callback
(Function): Function to be executed once the CSS files paths been read.
An application must define a combo handler in order to take advantage of the bundles:
To turn off combo handling set a the
development=1
cookie,javascript:document.cookie="development=1"
.
Fore more information about combo handling and LazoBundle
please refer to the lazo
wiki.
// app/bundle.js
define(['lazoBundle'], function (LazoBundler) {
'use strict';
return LazoBundler.extend({
response: function (route, uri, options) {
options.success({
js: ['app/bundles/application'],
css: ['/app/bundles/application.css']
});
}
});
});