lazo-optimizer
v0.0.6
Published
Runs the RequireJS optimizer using Lazo configuration and loaders
Downloads
18
Maintainers
Readme
lazo-optimizer
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.
Default Options
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;
});
}
JavaScript Bundling
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 Utilities
JavaScript bundling related utilities.
Note - All callbacks return err
, [results]
.
copyLoaders(lazoPath, appPath, callback)
Copies the text
, json
, and l
(loaders) to the root of an application for
the Require.js optimizer.
Arguments
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.
removeLoaders(appPath, callback)
Deletes the text
, json
, and l
(loaders) from the root of an application.
Arguments
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the loaders have been deleted.
getPaths(lazoPath, appPath, callback)
Gets the path configuration for Lazo and an application (reads
conf.json). Delegates to
getLazoPaths
and getAppPaths
merging the results.
Arguments
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.
getAppPaths(appPath, callback)
Gets the path configuration for an application (reads conf.json).
Arguments
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the paths have been resolved.
getLazoPaths(lazoPath, callback)
Gets the path configuration for Lazo. Replaces all module id values with "empty:".
Arguments
lazoPath
(String): Path to the Lazo node module directory.callback
(Function): Function to be executed once the paths have been resolved.
getJSIncludes(appPath, callback)
Reads the application directory returning an array of files. Excludes server/**/*
.
Arguments
appPath
(String): Path to the application directory.callback
(Function): Function to be executed once the application directory has been read.
getLoaderPrefix(filePath, loaders)
Prefixes file path with loader, e.g., text!app/app.json
, if required.
Arguments
filePath
(String): File path to prefix.loaders
(Object): Loaders map, (options.loaders
).
Returns
(String): File path with loader prefix if file extension maps to a loader.
getDefaultConfig(options, callback)
Gets the default configuration for the Require.js optimizer.
Arguments
options
(Object): Overrides for default options.callback
(Function): Function to be executed once the configuration has been generated.
removePathsWithModuleIds: function (files, paths)
Removes files from the includes that have a module id in the paths configuration.
Arguments
files
(Array): Include file paths.paths
(Object): Paths configuration.
Returns
(Array): Filtered file paths.
mergeConfigs(options, callback)
Merges the default configuration with an overrides in options.config
.
Arguments
options
(Object): Overrides for default options.callback
(Function): Function to be executed once the configuration has been merged.
CSS Bundling
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);
});
Default Options
The default options for bundleCss
and all functions that accept an options
object
are defined here.
CSS Bundling Utilities
CSS bundling related utilities.
Note - All callbacks return err
, [results]
.
concatMinifyCss(css, minify, callback)
Concat and optionally minify CSS.
Arguments
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.
readCssFiles(appPath, files, callback)
Reads CSS files.
Arguments
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.
resolveImgPaths(cssStr, cssFilePath)
Resolves image URLs is CSS files to absolute paths for an application.
Arguments
CssStr
(String): CSS file contents.cssFilePath
(Object): CSS file path relative to the application.
Returns
(String): CSS file contents with modified image URLs.
getCssFiles(appPath, filter, callback)
Gets the CSS file paths for an application.
Arguments
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.
Combo Handler
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']
});
}
});
});