4

I am trying to create a grunt task to compile coffeescript code spread across multiple files to .js files of the same name. I have the grunt coffeescript plugin and I am looking to use the "glob_to_multiple" spec that is given on this page:

https://www.npmjs.org/package/grunt-contrib-coffee.

 glob_to_multiple: {
    expand: true,
    flatten: true,
    cwd: 'path/to',
    src: ['*.coffee'],
    dest: 'path/to/dest/',
    ext: '.js'
  },

However, this grunt task does not compile .coffee files to .js files of corresponding names - for all .coffee files in a directory and its sub directories. I've been tweaking this config, for a while but I can't get it to do this. Please help.

EternallyCurious
  • 2,345
  • 7
  • 47
  • 78

2 Answers2

7

The pattern *.coffee will only match files that end with .coffee in the cwd folder. The pattern **/*.coffee will match all files that end with .coffee recursively in all sub folders of cwd and the cwd itself.

Try the following config:

glob_to_multiple: {
  expand: true,
  flatten: true,
  cwd: 'path/to',
  src: ['**/*.coffee'],
  dest: 'path/to/dest/',
  ext: '.js'
},

Also remove flatten: true if you want it to recreate the folder structure in path/to/dest/ instead of compiling all files to the single folder.

Kyle Robinson Young
  • 13,732
  • 1
  • 47
  • 38
  • That part worked. But it creates all the .js files in one directory instead of creating them in the same directory as the source .coffee file. And if there are more than one files with the same name ( Eg. `.coffee`), then it creates a common js file for both in the output directory called `.js` and concatenates the .coffee files with the common name into one file in the destination directory named `.src.js`. All I want is for one .js file to produced for each .coffee file – EternallyCurious May 27 '14 at 16:47
2

The accepted answer provided by Kyle does the exact opposite of what you want to achieve!

To make it work like you want to, just set dest: path/to. Make sure to set flatten: false flag.

With the below example you can make it work

coffee: {
  glob_to_multiple: {
      expand: true,
      flatten: false,
      cwd: '',
      src: ['server/api/**/*.coffee', 'client/app/**/*.coffee'],
      dest: '',
      ext: '.js',
      extDot: 'last'
  }
}

In the src:[] array you can put in whatever root/subdirs you want to be crawled. You could also cwd: '' and dest: '' to crawl your whole project including node_modules directory, just as a proof of concept. All js files will get created at the exact same spot where their corresponding coffee origins are located. The extDot: 'last' makes sure that files like myUnitTest.spec.coffee will get processed properly as well.

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147