使用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
。
Many coffee source files in /assets/src/coffee
etc (./child/paths
and so on) and I'd like to output them to assets/js/
and assets/js/child/paths
.
看起来我已经接近,但它不工作。使用 grunt-contrib-coffee
和 grunt-contrib-watch
。
It looks like I've gotten close, but it's not working. Using grunt-contrib-coffee
and grunt-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
中。
but the file actually ends up in: assets/src/coffee/projAlpha/dl.coffee
... and it's coffee script. It should be in assets/js/projAlpha/dl.js
.
我已经得到了grunt coffee工具来编译所有文件,并将它们放在正确的位置。我宁愿他们一次编译一个,虽然,因为我现在有几个文件,并添加更多的时间。
I've gotten the grunt coffee utility to compile all the files at once and put them in the right place. I'd rather they got compiled one at a time though, since I've got a few files now and am adding more all the time.
推荐答案
在您的glob模式文件路径配置中,我注意到两个问题:
In your glob-pattern file path config, I notice 2 problems:
- glob模式选项不在文件键下,而是直接在选项键下的
。 - 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
。
这篇关于使用grunt,是否可以编译和输出单个更改的文件到不同的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!