How to avoid multiple helper implementations like

2019-04-11 16:26发布

问题:

I have a Gruntfile.coffee that has a grunt-contrib-coffee configuration like this:

coffee:
  compile:
    files:
      'public/assets/application.js': [
          'multiple/files' #, ...
      ]
    options:
      bare: true

The Problem is, that it generates multiple implementations of helper methods like

    var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

How to make it recognize that these implementations are already compiled?

Update 1

The reason is obviously that the compiled JavaScript gets concatenated after it got compiled. It could be avoided if the CoffeeScript would be concatenated before compiling it. If this is not a configuration option of grunt-contrib-coffee that I'm missing, I'll submit an issue on github.

So the question that remains is: how to concatenate the CoffeeScript before compiling it, to avoid multiple helper implementations?

Update 2

There is a workaround, which involves manual concatenation of the source files with the concat task. This requires a temporary file, that can then be compiled from CoffeeScript to JavaScript. It is mentioned in the issue I created.

In future, tasks will be able to pass their stuff as buffers to each other (see the issue).

回答1:

As of grunt-contrib-coffee-0.6.1 this works with the join option:

coffee:
  compile:
    files:
      'public/assets/application.js': [
          'multiple/files' #, ...
      ]
    options:
      bare: true
      join: true # concatenate coffee files before compiling. default is false