Using grunt, is it possible to compile and output one modified file to another directory?

A lot of source code files in the format /assets/src/coffee , etc. ( ./child/paths etc.) and I would like to output them to assets/js/ and assets/js/child/paths .

Looks like I came up, but it doesn't work. 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 this looks like this:

 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: assets/src/coffee/projAlpha/dl.coffee ... and a coffee script. It should be in assets/js/projAlpha/dl.js

I have a grunt coffee utility to compile all files at once and put them in the right place. I would prefer that they be compiled one at a time, though, since now I have several files, and I'm adding more and more.

0
coffeescript gruntjs
source share
1 answer

In your glob-pattern file path configuration, I notice 2 problems:

  • The parameters of the glob-template do not fall under the "files" key, but directly under the options key.
  • CWD is for src files only. The dest directory do not use cwd, so you must specify the full path from the base directory (where your Grunt file is located).

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

This will move, for example, from assets/src/coffee/User/controller.coffee to assets/js/User/controller.js .

0
source share

All Articles