Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grunt-contrib-coffee one-to-one compile

I have several files named:

  • jquery.a.b.coffee
  • jquery.a.c.coffee
  • jquery.a.d.coffee

and they are all compiled into one jquery.js file in my output directory.

Although I guess this behavior might be nice in some cases, I would like to have them to compile into different files like jquery.a.b.js, jquery.a.c.js and so on. How can I tell grunt-contrib-coffeescript to do so?

My Gruntfile.js looks like this:

module.exports = function (grunt) {
    grunt.initConfig({
        coffee: {
          dist: {
            files: [{
              expand: true,
              flatten: true,
              cwd: 'app/webroot/coffee',
              src: ['{,*/}*.coffee'],
              dest: 'app/webroot/js',
              ext: '.js'
            }]
          }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-coffee');

};

Thanks for your help!

like image 890
Daniel Schmidt Avatar asked Jul 23 '13 08:07

Daniel Schmidt


2 Answers

The problem lies on the filenames having multiple dots.
If it was jquery-a-b.coffee, jquery-a-c.coffee etc, you would have seen the expected output.

It is a known issue (extension is after last period only) and grunt developers made this on purpose.
Here is a quote from one of them:

There's two ways ext could work; it could consider everything after the first dot the extension, or everything after the last dot the extension. We chose the former because the use-case is more common (we encounter .min.js files all the time). That being said, you can use the rename option to specify a function that will use whatever custom naming logic you need.

So, the only workaround for now is to remove ext and use rename like this:

coffee: {
  dist: {
    files: [{
      expand: true,
      cwd: 'app/webroot/coffee',
      src: ['{,*/}*.coffee'],
      dest: 'app/webroot/js',
      rename: function(dest, src) {
        return dest + '/' + src.replace(/\.coffee$/, '.js');
      }
    }]
  }
}

Update as of Grunt 0.4.3:
You can now use the extDot option along with ext

ext: '.js',
extDot: 'last'
like image 111
mutil Avatar answered Oct 06 '22 01:10

mutil


This works so you don't have to add the files by hand in your gruntFile:

coffee: {
    glob_to_multiple: {
    expand: true,
    flatten: true,
    cwd: 'app/webroot/coffee/',
    src: ['*.coffee'],
    dest: 'app/webroot/',
    ext: '.js'
    }
},
  1. cwd: the folder where your files are
  2. src: the matching pattern for your files, using glob
  3. dest: The folder where your files are going.

See https://github.com/gruntjs/grunt-contrib-coffee#usage-examples for some sample usages

like image 32
edi9999 Avatar answered Oct 06 '22 01:10

edi9999