问题
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
.
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'])}"))
)
Ok, so the output of that looks like:
assets/src/coffee/projAlpha/dl.coffee has changed
src: assets/src/coffee/projAlpha/dl.coffee
dest: assets/js/projAlpha/dl.coffee
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
.
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.
回答1:
In your glob-pattern file path config, I notice 2 problems:
- The glob-pattern options do not come under the "files" key, but directly under the options key.
- The CWD is only meant for the src files. The dest directory does not use cwd, so you must specify the full path from the base directory (where your Gruntfile is).
dev:
options:
sourceMap: true
expand: true
cwd: "assets/src/coffee/"
src: "**/*.coffee"
dest: "assets/js/"
ext: ".js"
This will move, for example, assets/src/coffee/User/controller.coffee
to assets/js/User/controller.js
.
来源:https://stackoverflow.com/questions/16203741/using-grunt-is-it-possible-to-compile-and-output-a-single-changed-file-to-a-dif