【问题标题】:Grunt.js task development production coffeescriptGrunt.js 任务开发制作coffeescript
【发布时间】:2014-02-27 16:29:31
【问题描述】:

编辑 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'])

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

    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"
  }
}

【问题讨论】:

  • development 被注释掉后,您可以运行grunt coffee,还是仅作为默认任务的一部分工作?

标签: javascript node.js coffeescript gruntjs


【解决方案1】:

将任务选项包装在 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'
    }
  }

【讨论】:

  • 仅删除 file:[...] 部分似乎已经奏效,但是我无法理解 glob_to_multiple 究竟做了什么(它本身实际上并没有编译任何咖啡脚本)
  • 另请注意,使用compile: 部分不起作用,请参阅我在原始帖子中所做的编辑
  • glob_to_multiple 是他们给它起的名字,因为它采用源代码的cwd 中的任何CoffeeScript 文件并将它们全部编译成单独的js 文件,即dest 目录。 IE。一个文件到多个文件。 coffee 任务任务默认负责编译。在glob_to_multiple 中,我们只是添加了额外的选项,让它按照我们的喜好工作。
  • @mdedetrich 编译部分有什么问题,它会抛出错误吗?您是否检查以确保更改后缩进正确?尝试在详细模式下运行它,这样我们就可以看到出了什么问题:grunt coffee -v
  • 我想我不是在交流我的问题,不是你的代码不起作用(它确实如此),当你将任务拆分为子任务时它让它工作,在我的情况下@ 987654334@ 和development。似乎在这样做时,您省略了compile 选项我不确定为什么会这样,但它按照编辑 1 可以正常工作。此时我只想解释为什么会这样
【解决方案2】:

原来我对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"

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多