使用grunt,是否可以编译并将单个更改的文件输出到不同的目录?

时间:2013-04-24 23:07:23

标签: node.js coffeescript gruntjs

/assets/src/coffee等许多咖啡源文件(./child/paths等),我想将它们输出到assets/js/assets/js/child/paths

看起来我已经接近了,但它没有用。使用grunt-contrib-coffeegrunt-contrib-watch

grunt.initConfig
watch:
  coffee:
    files: '<%= coffee.src %>',
    tasks: 'coffee:dev'
    options:
      nospawn: true

coffee:
  src: 'assets/src/coffee/**/*.coffee'
  dev:
    options:
      sourceMap: true
    files: [
      expand: true
      cwd: "assets/"
      src: "/src/coffee/**/*.coffee"
      dest: "../js/"
      ext: ".js"
    ]

grunt.loadNpmTasks "grunt-contrib-coffee"
grunt.loadNpmTasks 'grunt-contrib-watch' 
# Default task.
grunt.registerTask "default", "watch"

grunt.event.on('watch', (action, filepath) ->
(grunt.log.writeln('\n'+ filepath + ' has ' + action))

dest = "#{path.dirname(filepath.replace("src/coffee", "js"))}"

grunt.config(['coffee', 'dev', 'files', 'src'], [filepath])
grunt.config(['coffee', 'dev', 'files', 'dest'], [dest])

(grunt.log.writeln("src: #{grunt.config(['coffee', 'dev', 'files', 'src'])}"))
(grunt.log.writeln("dest: #{grunt.config(['coffee', 'dev', 'files', 'dest'])}"))

好的,所以输出结果如下:

assets/src/coffee/projAlpha/dl.coffee has changed    
src: assets/src/coffee/projAlpha/dl.coffee    
dest: assets/js/projAlpha/dl.coffee

但文件实际上最终在:assets/src/coffee/projAlpha/dl.coffee ...而且它是咖啡脚本。它应该在assets/js/projAlpha/dl.js中。

我已经获得了grunt coffee实用程序,可以立即编译所有文件并将它们放在正确的位置。我宁愿他们一次编译一个,因为我现在有几个文件并且一直在增加更多。

1 个答案:

答案 0 :(得分:0)

在你的glob-pattern文件路径配置中,我注意到2个问题:

  1. glob-pattern选项不在“files”键下,但是 直接在选项键下。
  2. CWD仅适用于src文件。 dest目录没有 使用cwd,因此必须指定基目录的完整路径 (你的Gruntfile所在的位置)。

  3. dev:
      options:
        sourceMap: true
      expand: true
      cwd: "assets/src/coffee/"
      src: "**/*.coffee"
      dest: "assets/js/"
      ext: ".js"
    

    例如,这会将assets/src/coffee/User/controller.coffee移至assets/js/User/controller.js