Brewer.js

Your asset management best friend.


Brewer.js is a tool that eases all sorts of tasks related to the management of stylesheets and javascript source files. From a single command, it takes charge of :

  • Compilation of coffee-script (or iced) source files to javascript
  • Compilation of LESS or Stylus files to CSS
  • Bundling different files (javascript or CSS) into a single file (optionally compressing it) while maintaining references and their proper ordering.
  • Watching a directory for changes, triggering any or all of the above.

$ brake watch
Info Watching project /path/to/current/working/directory
Compiled coffee/test.coffee
Compiled less/index.less
Packaged build/css/index.css
Compressed build/css/index.css

Installation


To setup Brewer.js, make sure you have installed Node.js. When this is done, enter the terminal and type.

$ npm install -g brewer

The -g flag is used to make the installation global, so you get access to the brake executable. Then, from a project directory you wish to manage using Brewer.js

$ brake init [<template name>]

The <template name> part is optional. Anyway, for the moment, there is only one template available, lesscoffee, which is also the default. This template sets up LESS and Coffeescript support for the current directory.

What this does is create a Brewfile in the current directory, according to the template provided and run brake make (see CLI Usage) on it.

Usage

Configuring using a Brewfile


A Brewfile is a file that sits at the root folder of your project, named, well, "Brewfile", which syntax is plain Coffeescript. It is used to specify the structure of the project to Brewer.js, so it knows what to do when you tell it to make, watch or clean.

Packages

At the top-level of the Brewfile, the following functions are available :

Those two functions can be used interchangeably with js or css to be more concise. The package_name is used to identify certain parts of a project, in case you would like to run $ brake make package_name. A Package is a conceptual component of a project in which you can define bundles and in which files can import each other. A file contained in a package can really be anywhere on the file system -- a package is not bound to a specific directory.

Package can receive options, which can be specified in one of two ways :

The build option specifies where to put bundles aggregated from source files, and compress … well, I think you can guess that one.

@options is a method available in the package definition body, that specifies additional options for the package. Another such method is @bundles(bundle1, bundle2, ...) which specifies file names for bundles in the package (see Bundles below). These two methods are prefixed with a @, meaning that they are members of the package body context. Other methods of this kind are available, such as those to define Sources in a package.

Sources

Sources represent a folder on the file system, where files can be found. One or many can exist within a package, providing it with files to compile, bundle and compress. A source can be of one of many available types : coffeescript (or icedcoffeescript), javascript, css, less or stylus. One restriction about this source type is that a package can only contain sources that are either of its own type, or that can be compiled into its own type (remember a package also have a type).

Now for a real-world example, so we don't get lost too quickly. Say we want to make a cool looking website using jQuery, Bootstrap and Chosen. Each of these libraries have their own source files, of different types and we want to manage all of that neatly. Here is the directory structure :

So there are files that ends up as javascript and those that ends up as css. Let's write a Brewfile that takes this structure into account.

So what we're looking at now is a package for javascripts that looks for coffeescript files in ./coffee, as well as for javascript files in ./vendor, ./vendor/chosen and ./vendor/bootstrap/js. Now let's look at what a bundle would look like :

If we had been running brake watch while we wrote that script and saved it, we would have seen a file appear, named ./js/home.js, containing an aggregate file of jquery, chosen and our little script compiled into javascript. Since we didn't provide the output option, a compiled javascript version of just coffee/home.coffee can be found in ./_cache. If we wanted it to appear somewhere more meaningful, we could set the output option on a source directive. A more complete example of the above could be :

This dedicates a directory ./build, to contain files that would be deployed, and all source files in separate directories. You may have noticed we used @css and @js names to mean stylesheets and javascript respectively. As we said earlier, those are drop-in replacements.

Command-line interface


The command-line interface for brewer provides a few methods to manage your asset project:

Changelog


A more detailed changelog is available on Github

0.3.9
Added support for CoffeeScript's new literate syntax
0.3.8
Made javascript compression work with Uglify-js 2.
0.3.7
Added alternate namings for Brewfile.
0.3.6
Fixed a few bugs and updated for Node.js v0.8.X.
0.3.5
Fixed a few bugs and changed the brewer command for brake.
0.3.4
Fixed a few bugs, improved docs and added IcedCoffeescript support.
0.3.3
Fixed a few bugs and added a brewer install command.
0.3.2
Fixed a bug that made the CSS compression fail under certain circumstances.
0.3.1
Bug fix to make the npm installation work.
0.3.0
Made CLI commands functional. Also added full documentation under docs/
0.2.2
Fixed a minor bug.
0.2.1
Added API for Project and Package to clean impermanent files.
0.2.0
Major code refactor. All the code base is much leaner, cleaner, easier to maintain and to extend.
0.1.0
Initial release. Compilation, compression and packaging works properly for LESS, Stylus, Coffeescript, JS and CSS. More file types to come!

MIT License


Copyright © 2012 Mathieu D'Amours

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.