Search code examples
coffeescriptcakefile

Calling methods from task in Cakefile


I'm setting up a Cakefile that will compile and minify my CoffeeScript and minify my Vanilla libs.

I created different tasks for each case (whether it was a coffee file or not) but I want to combine them into one task.

The problem I'm having is calling a method from the task; I can call a method with no problem under some circumstances, but otherwise I receive

    TypeError: undefined is not a function

The object I'm working on looks like

source = 
  libs: [
    'lib/jquery-1.7.1.min.js'
    'lib/backbone.js'
    'lib/underscore.js'
  ]
  coffees: [
    'app/800cart.coffee'
    'app/models/coffee/cart.coffee'
    'app/models/coffee/contact.coffee'
  ]

And Im wanting to do this, and I get the error

 task 'build', 'Concat, compile, and minify files', ->
    for fileType, files of source
        concatinate files

    concatinate = (files) ->
      console.log 'concatinating'

The part that I'm really confused by is if I call the method with a condition it runs fine

 task 'build', 'Concat, compile, and minify files', ->
    for fileType, files of source
        concatinate files if fileType is 'coffees'

    concatinate = (files) ->
      console.log 'concatinating'

What am I doing wrong here?


Solution

  • The problem is that you're trying to call concatinate before you define concatinate with the line concatinate =. Just move up the declaration, or better yet, move it outside of the task definition.

    You're probably used to JavaScript's function concatinate syntax, which automatically moves the function to the top of the scope. CoffeeScript compiles to the concatinate = function syntax instead, mainly because the function cocatinate syntax behaves inconsistently across different JS runtimes (particularly IE). So, CoffeeScript functions simply obey ordinary variable assignment rules.