Eduard Luca
Eduard Luca

Reputation: 6602

Custom CoffeeScript compilation with GruntJS

I have a Gruntfile, which takes all *.coffee files from a certain folder, and compiles them to JS files, keeping the same folder structure (if any).

So with a folder structure like:

scripts
|--widgets
|    |--a.coffee
|--vendor
|    |--b.coffee
|--c.coffee
|--d.coffee

It will generate the same folder structure, but with JS files instead of Coffeescript files. I would like to have a separate rule for the widgets folder and the c.coffee file, ie. I want to compile all of the contents of widgets and c.coffee into one single file.

How can I exclude one file and one folder from the files property of the grunt object? This is the code I'm currently running:

files: [{
    expand: true,
    cwd: '<%= params.app %>/scripts',
    src: '{,*/}*.{coffee,litcoffee,coffee.md}',
    dest: '.tmp/scripts',
    ext: '.js'
}]

I've also seen that there are 2 syntaxes to declare files. One is as an object, and one as an array (what I have above). What is the difference and would the other declaration better help me in my case?

Upvotes: 2

Views: 82

Answers (1)

Patrick J. S.
Patrick J. S.

Reputation: 2935

The documentation on configuring grunt tasks has some words on what you want. Actually, there are four ways to define a files property, of which one is deprecated.

Here is a Gruntfile.coffee, because it is shorter. Use the File Arrays Format with exclusion patterns for the compile subtask and the Compact Format for the compileJoined subtask. I hope you use grunt-contrib-coffee. grunt-coffee is out of maintenance for almost two years now and doesn't seem to have a join option.

module.exports = (grunt) ->
  grunt.initConfig
    params: app: '.' # ignore this, it's just that this file works as expected.

    coffee:
      compile:
        files: [
          cwd: '<%= params.app %>/scripts'
          expand: yes
          src: ['**/*.{coffee,litcoffee,coffee.md}'   # everything coffee in the scripts dir
            '!c.coffee'        # exclude this
            '!widgets/**/*']   # and these
          dest: '.tmp/scripts'
          ext: '.js'
          extDot: 'first' # to make .js files from .coffee.md files
          ]

       compileJoined:
         options: join: yes
         # sadly you can't use expand here, so you'll have to do cwd "by hand".
         src: [
           '<%= params.app %>/scripts/c.coffee'
           '<%= params.app %>/scripts/widgets/**/*.{coffee,litcoffee,coffee.md}'
           ]
         dest: '.tmp/special.js'

  grunt.loadNpmTasks 'grunt-contrib-coffee'

here's a small output from script, it seems to work:

$ tree scripts
scripts
├── c.coffee
├── d.coffee
├── vendor
│   └── b.coffee
└── widgets
    └── a.coffee

2 directories, 4 files
$ rm -rf .tmp
$ grunt coffee
Running "coffee:compile" (coffee) task
>> 2 files created.

Running "coffee:compileJoined" (coffee) task
>> 1 files created.

Done, without errors.
$ tree .tmp
.tmp
├── scripts
│   ├── d.js
│   └── vendor
│       └── b.js
└── special.js

2 directories, 3 files
$ cat scrips/c.coffee
variableInC_coffee = "a variable"
$ cat scripts/widgets/a.coffee

variableInC_coffee = variableInC_coffee.replace /\s+/, '_'
$ cat .tmp/special.js
(function() {
  var variableInC_coffee;

  variableInC_coffee = "a variable";

  variableInC_coffee = variableInC_coffee.replace(/\s+/, '_');

}).call(this);

Upvotes: 1

Related Questions