How to avoid multiple helper implementations like __indexOf() with grunt-contrib-coffee

大城市里の小女人 提交于 2019-12-06 07:23:14

问题


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


来源:https://stackoverflow.com/questions/15165017/how-to-avoid-multiple-helper-implementations-like-indexof-with-grunt-contrib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!