Grunt.js任务开发制作coffeescript

时间:2014-02-03 23:30:59

标签: javascript node.js coffeescript gruntjs

编辑2 :如果使用以下配置(这似乎是自然设置),grunt不会编译任何咖啡文件

coffee:
  development:
    compile:
      expand: true
      cwd: "<%= srcDirCoffee %>"
      src: ["**/*.coffee"]
      dest: "<%= jsOutput %>"
      ext: ".js"
    options:
      sourceMap:true

使用-v标志运行时,Grunt输出以下内容 https://gist.github.com/mdedetrich/0ecccb50ddb2fd56dc35

编辑:相关部分现在看起来像这样,似乎正在运作

coffee:
  development:
    expand: true
    cwd: "<%= srcDirCoffee %>"
    src: ["**/*.coffee"]
    dest: "<%= jsOutput %>"
    ext: ".js"
    options:
      sourceMap:true
  production:
    expand:true
    cwd: "<%= srcDirCoffee %>"
    src: ["**/*.coffee"]
    dest: "<%= jsOutput %>"
    ext: ".js"

然而,它有点令人困惑,为什么它只能以这种方式工作(以及为什么需要删除编译部分才能使其工作)

原始问题

我目前正在设置一个Gruntjs任务,看起来像这样

module.exports = (grunt) ->
  grunt.initConfig(
    pkg: grunt.file.readJSON("package.json")
    srcDir: "./src/main"
    srcDirLess: "<%= srcDir %>/less"
    srcDirCoffee: "<%= srcDir %>/coffee"
    scalaVersion: "scala-2.10" #This is the scala version we are using
    resourceManaged: "./target/<%= scalaVersion %>/resource_managed/main"
    cssOutput: "<%= resourceManaged %>/css"
    jsOutput: "<%= resourceManaged %>/js"
    cssRequestPath: "/css"
    jsRequestPath: "/js"

    less:
      development:
        options:
          paths: ["<%= srcDirLess %>"]
#          sourceMap:true
#          sourceMapFilename: "<%= cssOutput %>/index.css.map"
#          sourceMapRootpath: "<%= srcDirLess %>"
#          sourceMapURL:  "<%= cssRequestPath %>/index.css.map"
        files:
          "<%= cssOutput %>/index.css" : "<%= srcDirLess %>/index.less"
      production:
        options:
          paths: ['<%= srcDirLess %>']
          cleancss:true
        files:
          "<%= cssOutput %>/index.css" : "<%= srcDirLess %>/index.less"

    coffee:
      development:
        compile:
          files: [
            expand: true
            cwd: "<%= srcDirCoffee %>"
            src: ["**/*.coffee"]
            dest: "<%= jsOutput %>"
            ext: ".js"
          ]
#        options:
#          sourceMap:true
      production:
        compile:
          files: [
            expand:true
            cwd: "<%= srcDirCoffee %>"
            src: ["**/*.coffee"]
            dest: "<%= jsOutput %>"
            ext: ".js"
          ]

    requirejs:
      production:
        compile:
          options:
            baseUrl: "<%= jsOutput %>"
            mainConfigFile: "<%= jsOutput %>/main"

    watch:
      coffee:
        files: "<%= srcDirCoffee %>/**/*.coffee"
        tasks: ["coffee:development"]
      less:
        files: "<%= srcDirLess %>/**/*.less"
        tasks: ["less:development"]

    clean: ["<%= cssOutput %>","<%= jsOutput %>"]
  )

  grunt.loadNpmTasks('grunt-contrib-less')
  grunt.loadNpmTasks('grunt-contrib-coffee')
  grunt.loadNpmTasks('grunt-contrib-requirejs')
  grunt.loadNpmTasks('grunt-contrib-clean')

  grunt.registerTask('default', ['coffee:development','less:development'])
  grunt.registerTask('production',['less:production','coffee:production','requirejs:production'])

不幸的是,由于某种原因,default coffee任务无法正常工作(即它实际上并不运行任务)。如果我拿出咖啡任务中的开发/生产部分,即

    coffee:
#      development:
      compile:
        files: [
          expand: true
          cwd: "<%= srcDirCoffee %>"
          src: ["**/*.coffee"]
          dest: "<%= jsOutput %>"
          ext: ".js"
        ]

将默认任务更改为

  grunt.registerTask('default', ['coffee','less:development'])

它最终起作用,有谁知道为什么会这样?它适用于less,但由于某种原因,它不适用于coffee

这是我用于Grunt任务的package.json

{
  "name": "test",
  "version": "0.0.1",
  "devDependencies": {
    "grunt": "~0.4.2",
    "grunt-contrib-jshint": "~0.6.3",
    "grunt-contrib-less": "~0.9.0",
    "grunt-contrib-coffee": "~0.9.0",
    "grunt-contrib-cssmin": "~0.7.0",
    "grunt-contrib-requirejs": "~0.4.1",
    "grunt-contrib-clean": "~0.5.0"
  }
}

2 个答案:

答案 0 :(得分:0)

file:[...]中包装任务选项会搞乱你的任务。

请改为:

      compile:
        options:
          sourceMap: true,
          sourceMapDir: 'path/to/maps/' # source map files will be created here

        files:
          "<%= jsOutput %>": ["**/*.coffee"]

git repo explains如何使用compileWithMapsDir示例中的此类选项进行编译。

compileWithMapsDir: {
    options: {
      sourceMap: true,
      sourceMapDir: 'path/to/maps/' // source map files will be created here
    },
    files: {
      'path/to/result.js': 'path/to/source.coffee'
    }
  }

答案 1 :(得分:0)

事实证明,我对compile选项的含义感到困惑。在grunt.js中,compile只是一个占位符,它实际上并不意味着什么,仅用于文档/演示原因

以下版本运行良好且没有任何麻烦

coffee:
  development:
    expand: true
    cwd: "<%= srcDirCoffee %>"
    src: ["**/*.coffee"]
    dest: "<%= jsOutput %>"
    ext: ".js"
    options:
      sourceMap:true
  production:
    expand:true
    cwd: "<%= srcDirCoffee %>"
    src: ["**/*.coffee"]
    dest: "<%= jsOutput %>"
    ext: ".js"