5

我有几个文件名为:

  • jquery.abcoffee
  • jquery.accoffee
  • jquery.adcoffee

并且它们都被编译到jquery.js我的输出目录中的一个文件中。

虽然我猜这种行为在某些情况下可能会很好,但我想让它们编译成不同的文件,比如jquery.a.b.js,jquery.a.c.js等等。我如何告诉 grunt-contrib-coffeescript 这样做?

我的 Gruntfile.js 看起来像这样:

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');

};

谢谢你的帮助!

4

2 回答 2

10

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'
于 2013-07-24T12:52:09.167 回答
3

这有效,因此您不必在 gruntFile 中手动添加文件:

coffee: {
    glob_to_multiple: {
    expand: true,
    flatten: true,
    cwd: 'app/webroot/coffee/',
    src: ['*.coffee'],
    dest: 'app/webroot/',
    ext: '.js'
    }
},
  1. cwd : 你的文件所在的文件夹
  2. src:文件的匹配模式,使用 glob
  3. dest:文件所在的文件夹。

有关一些示例用法,请参阅https://github.com/gruntjs/grunt-contrib-coffee#usage-examples

于 2013-07-23T12:49:07.463 回答