GloryOfThe80s
GloryOfThe80s

Reputation: 161

Grunt: change file extension at build

I would like to write a Grunt task that, during build, will copy all .html files that I have and make an .asp version of it in /dist as well.

I have been trying to use grunt-contrib-copy to accomplish this, and here's what I have:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      option: {
        process: function (content, srcpath) {
          return srcpath.replace(".asp");
        }
      }
    }]
  } //end asp task
},

I know that the process function is not actually correct... I've tried a few different regex to make it work to no avail. When I run the asp task, the Grunt CLI says that my it has copied 2 files, but they're nowhere to be found. Any help is appreciated.

Upvotes: 1

Views: 1687

Answers (1)

Mario Araque
Mario Araque

Reputation: 4572

You can do that using rename function.

For example:

copy: {
  //some other tasks that work...

  //copy an .asp version of all .html files
  asp: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= config.app %>',
      src: ['{,*/}*.html'],
      dest: '<%= config.dist %>',
      rename: function(dest, src) {
         return dest + src.replace(/\.html$/, ".asp");
      }
    }]
  } //end asp task
},

This should works.

Upvotes: 7

Related Questions