【问题标题】:using grunt, is it possible to compile and output a single changed file to a different directory?使用 grunt,是否可以编译单个更改的文件并将其输出到不同的目录?
【发布时间】:2013-04-18 16:44:47
【问题描述】:

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

【问题讨论】:

    标签: node.js coffeescript gruntjs


    【解决方案1】:

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

    1. glob-pattern 选项不在“files”键下,但 直接在选项键下。
    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.coffeeassets/js/User/controller.js

    【讨论】:

    • 嗯不完全是:“警告:EACCES,权限被拒绝'/dev/fd/10'”
    • 我的错,“cwd”应该有一个斜杠。我把它作为“src”中的前导斜杠。
    • 好的,这是可行的,但是我已经在其他一些代码中实现了该功能,但我要做的只是编译已更改的文件而不是所有文件。这就是grunt.event.on 的意义所在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多