Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript --compile order in sub-directories

Tags:

coffeescript

Is there any way to define CoffeeScript compilation order in sub-directories? Please consider the following example:

Files:

  • src/App.coffee
  • src/view/B.coffee
  • src/view/a/A.coffee

Where class A extends B.

coffee --join js/app.js --compile src/view/ src/App.coffee

This throws an error in the browser:

Uncaught TypeError: Cannot read property 'prototype' of undefined 

If I rename folder a to z, the error is gone and everything works fine.

  • src/view/z/A.coffee

I would expect the compiler to read all .coffee files from src/view/ before it goes into src/view/ sub-directories. Again, is there any way to do that?

Edit: PC Windows 7, CoffeeScript version 1.3.3

like image 937
imbrizi Avatar asked Jul 08 '12 20:07

imbrizi


2 Answers

The only solution I think is to create the compile order manually within a build script. You would create an ordered collection with filenames, where as the loop iterates and concatenates a new big string, which can be compiled as one file.

Create a Cakefile with following content, check Syntax first. And run with cake build. That should work, cake comes with CoffeeScript.

fs = require 'fs'
{exec} = require 'child_process'

viewsDir = "src/view"
coffeeFiles = [
  'B'
  'A'
]

task 'build'
  # loops through coffeeFiles. 
  for file, index in coffeeFiles then do (file, index) ->
    fs.readFile "#{viewsDir}/#{file}", 'utf8', (err, content) ->
      appCoffee[index] = content
      compile() if --remaining is 0

  compile = ->
    fs.writeFile 'js/app.coffee', appCoffee.join('\n\n'), 'utf8', (err)   ->
      throw err if err
      exec 'coffee --compile js/app.coffee', (err, stdout, stderr) ->
        throw err if err
          console.log stdout + stderr

          # you can skip deleting the app.coffee file
          fs.unlink 'js/app.coffee', (err) ->
            throw err if err
            console.log 'Created app.coffe, compiled to app.js and removes app.coffee'

            # maybe additional taks 
            # invoke 'test'

Documented also in Wiki of Coffeescript https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools

Before first loop you could also make it loop through different directories. And just list filenames in coffeeFiles to be processed before the others not in listed and the rest could be added to list with fs.readDir().

like image 161
vik Avatar answered Sep 28 '22 03:09

vik


We created a simple module to solve a similar problem:

https://github.com/Vizir/rehab

Just put #_require [filename].coffee on your file and you're done.

We are using it in productions with complex dependency graphs.

like image 38
David Lojudice Sb. Avatar answered Sep 28 '22 03:09

David Lojudice Sb.