Adding Pre Compilers

laktek edited this page Nov 24, 2012 · 7 revisions

Punch can pre-compile files written in CoffeeScript and LESS. But you can easily add support to any other pre-compiler as well.

Following pre-compilers are already available as plugins:

If you want to write a custom pre-compiler follow these steps:

module.exports = {
	input_extensions: [".less"],
        force_compile: true,

	compile: function(input, filename, callback){
		var output;
		// call the compiler with the input 
		return callback(err, output);
	}

};

Custom pre-compiler should implement a compile function, which takes in an input, filename (Punch will pass the name of the file that needs to be compiled) and a callback. After compilation is done, the callback should be invoked with the output (or an error).

Note: Set the force_compile option to true, if you want to compile the file at every request (in development mode). By default, Punch will compile a file only if it has been modified since last request. However, if the pre-compiler imports other files (like SASSS and LESS), Punch won't detect their modifications, hence this option is necessary.

How to add a custom pre-compiler

To use a custom pre-compiler in a project, you have to define it as a plugin in project's configuration (config.json).

	"plugins": {
		"compilers": {
			".js": "custom_compiler" 
		}
	}

The key should be the output extension of the compiled files (for example if your compiler reads CoffeeScript and outputs JS, key name should be .js). Compiler should be specified as a valid Node.js module path.