繁体   English   中英

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

[英]using grunt, is it possible to compile and output a single changed file to a different directory?

/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实用程序,可以立即编译所有文件并将它们放在正确的位置。 我宁愿他们一次编译一个,因为我现在有几个文件,并且一直在增加。

在您的glob模式文件路径配置中,我注意到2个问题:

  1. glob-pattern选项不在“文件”键下,而是直接在options键下。
  2. CWD仅用于src文件。 dest目录不使用cwd,因此您必须指定基本目录(Gruntfile所在的目录)的完整路径。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM