Skip to content

Instantly share code, notes, and snippets.

@arboleya
Created November 22, 2012 11:51
Show Gist options
  • Save arboleya/4130757 to your computer and use it in GitHub Desktop.
Save arboleya/4130757 to your computer and use it in GitHub Desktop.
Custom compiling routine using Coffee Toaster under the hood, injecting javascript's HEADER and FOOTER code to you final JS output file.
###
This file replaces your 'toaster.coffee' config file, you'll not need
that anymore since the config is passed to Toaster as a hash/object.
Keep in mind that in order for this to work, you'll have to use toaster
as a dependency, which can be easily achieved putting the provided
'package.json' file in your base dir and running 'npm install' to install
toaster locally. Or just enter your project folder and run
'npm install coffee-toaster' (not recommend but work as well)
###
# 1) Requiring
Toaster = require( "coffee-toaster" ).Toaster
# 2) Configuration as a hash - just like your 'toaster.coffee' config file.
# Feel free to customize it according your needs.
config =
folders: src:''
vendors: []
packaging: false
minify: false
bare: true
release: "www/js/app.js",
debug: "www/js/app-debug.js"
# 3) Instantiating Toaster passing DIR and OPTIONS/CONFIG. The final argument
# (true) means "DO NOT BUILD AT STARTUP", Toaster constructor is like this:
#
# constructor:( basedir, options = null, skip_initial_build = false )->
#
toaster = new Toaster __dirname, {w:true, d:true, config: config}, true
# 4) Defining the 'recompile' method and linking it with the
# 'toaster.before_build' filter. When this filter is set, toaster WILL NOT
# build your application automatically. You'll need to call 'toaster.build'
# for yourself, and then you'll be able to inject HEADER and FOOTER code - which
# MUST be purely javascript.
toaster.before_build = recompile = ->
header = "(function($){$(document).ready(function(){"
footer = "})}(jQuery))"
toaster.build header, footer
# 5) Compiling for the first time NOW - since we told toaster to NOT BUILD AT
# STARTUP at step #3 (skip_initial_build=true).
recompile()
# 6) Now instead of calling 'toaster -wd' you just call 'coffee compile.coffee'
# and everything should work as expected.
{
"author" : "Anderson Arboleya <anderson@arboleya.me>",
"name" : "custom-compiler",
"description" : "Any compiler that wants to use CoffeeToaster.",
"version" : "0.0.1",
"engines" : {
"node" : ">= 0.8.14"
},
"dependencies" : {
"coffee-toaster" : "0.6.4"
}
}
@zx1986
Copy link

zx1986 commented Nov 27, 2012

WOW! awesome!

@zx1986
Copy link

zx1986 commented Nov 27, 2012

sorry, did I do something wrong?

coffee compile.coffee

ERROR Source folder doens't exist:
        src
Check your toaster.coffee and try again.
        /home/zx1986/Dropbox/Projects/log-watcher-gts/toaster.coffee
#1) Requiring
Toaster = require( "coffee-toaster" ).Toaster

#2) Configuration as a hash - just like your 'toaster.coffee' config file.
# Feel free to customize it according your needs.
config =
    folders: src:'assets/coffeescripts/'
    vendors: []
    packaging: false
    minify: false
    bare: true 
    release: "assets/javascripts/main.js"
    debug: "assets/javascripts/main-debug.js"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment