Join multiple Coffeescript files into one file? (Multiple subdirectories)

前端 未结 7 1782
臣服心动
臣服心动 2020-12-31 13:19

I\'ve got a bunch of .coffee files that I need to join into one file.

I have folders set up like a rails app:

/src/controller/log_controller.coffee
/         


        
相关标签:
7条回答
  • 2020-12-31 13:53

    From the CoffeeScript documentation:

    -j, --join [FILE] : Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects.

    So, you can achieve your goal at the command line (I use bash) like this:

    coffee -cj path/to/compiled/file.js file1 file2 file3 file4
    

    where file1 - fileN are the paths to the coffeescript files you want to compile.

    0 讨论(0)
  • Alternatively, you could use the --bare flag, compile to JavaScript, and then perhaps wrap the JS if necessary. But this would likely create problems; for instance, if you have one file with the code

    i = 0
    foo = -> i++
    ...
    foo()
    

    then there's only one var i declaration in the resulting JavaScript, and i will be incremented. But if you moved the foo function declaration to another CoffeeScript file, then its i would live in the foo scope, and the outer i would be unaffected.

    So concatenating the CoffeeScript is a wiser solution, but there's still potential for confusion there; the order in which you concatenate your code is almost certainly going to matter. I strongly recommend modularizing your code instead.

    0 讨论(0)
  • 2020-12-31 13:59

    I've just release an alpha release of CoffeeToaster, I think it may help you. http://github.com/serpentem/coffee-toaster

    0 讨论(0)
  • 2020-12-31 14:09

    You could write a shell script or Rake task to combine them together first, then compile. Something like:

    find . -type f -name '*.coffee' -print0 | xargs -0 cat > output.coffee

    Then compile output.coffee

    Adjust the paths to your needs. Also make sure that the output.coffee file is not in the same path you're searching with find or you will get into an infinite loop.

    http://man.cx/find | http://www.rubyrake.org/tutorial/index.html

    Additionally you may be interested in these other posts on Stackoverflow concerning searching across directories:

    • How to count lines of code including sub-directories
    • Bash script to find a file in directory tree and append it to another file
    • Unix script to find all folders in the directory
    0 讨论(0)
  • 2020-12-31 14:11

    The most easy way to use coffee command line tool.

    coffee --output public --join --compile app

    app is my working directory holding multiple subdirectories and public is where ~output.js file will be placed. Easy to automate this process if writing app in nodejs

    0 讨论(0)
  • 2020-12-31 14:12

    This helped me (-o output directory, -j join to project.js, -cw compile and watch coffeescript directory in full depth):

    coffee -o web/js -j project.js -cw coffeescript
    
    0 讨论(0)
提交回复
热议问题