Skip to content
pvorb edited this page Aug 24, 2011 · 4 revisions

bake allows defining hooks for each file property and some special hooks.

Hook functions are provided as the second parameter when calling the main bake function.

bake(conf, hooks);

Each property of the hooks object defines a function having the form of function(filename, prop). There two different types of hooks. The one's that have a corresponding property and the ones that are called at a specific moment in the baking process.

Property hooks

Property hooks can be used to alter a specific property of the file that is currently being processed.

You could use a property hook like this:

hooks.date = function(filename, prop) {
  var date = new Date(prop.date);
  return (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getYear();
}

This function parses the value provided in prop.date and turns it into a string of the format MM-DD-YYYY.

Note that you can only alter the value that the function applies to.

The hooks are called in the same order as the properties are defined in a file. That's why their order may change from file to file. You should avoid defining property hooks that depend on each other.

Special hooks

There are some special hooks, that may be defined. These are

  • __propBefore,
  • __propAfter,
  • __writeAfter and
  • __complete

__propBefore is called before the property hooks are called.

__propAfter is called after the property hooks have been called.

__writeAfter is called when a single file has been successfully written.

__complete is called when all files have been written.

There is one big difference with these special hooks. __propBefore and __propAfter must return the whole property object. __writeAfter and __complete can't alter the properties and thus don't need to return anything at all.

__content is not a special hook, since __content is a regular property.

Clone this wiki locally