Gruntjs: replace templates when copying a file
Asked Answered
Z

3

7

I am writing a Gruntjs script which should

  • concatenate + replace template of some JS files into target directory (contrib-concat)
  • copies + replace template of some other files (contrib-copy)
  • package the files into a zip file

contrib-concat has a boolean option process to replace templates (like <% pkg.version %>) when processing files.

contrib-copy also has an option processContent, however I don't know how to trigger template processing with this option.

module.exports = function(grunt) {

    grunt.initConfig({
        meta: {
            banner: ' \
/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n \
 * <%= pkg.homepage %>\n \
 */\n\n',
            build_date: '<%= grunt.template.today("yyyy-mm-dd") %>',
            build_num: process.env.BUILD_NUMBER || 0, // Jenkins build number if available
            version_string: '<%= pkg.version %>-<%= meta.build_num %>',
            dist_dir: 'dist/<%= pkg.version %>'
        },
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            options: {
                stripBanners: {
                    block: true
                },
                process: true,
                separator: '\n /* ----- */ \n',
                banner: '<%= meta.banner %>'
            },
            dist: {
                src: [
                    'src/ViewUtility.js',
                    'src/ViewClass.js',
                    'src/ViewClass.js',
                    'src/MarksClass.js',
                    'src/ViewVersion.js'],
                dest: 'build/View.js'
            }
        },
        uglify: {
            options: {
                mangle: {
                    except: ['jQuery', 'Hammer']
                },
                banner: '<%= meta.banner %>'
            },
            dist: {
                src: '<%= pkg.main %>',
                dest: 'build/View.min.js'
            }
        },
        copy: {
            options: {
                processContent: true
            },
            dist: {
                files: [
                    {expand: true, cwd: 'build/', src: ['**'], dest: '<%= meta.dist_dir %>/view/'},
                    {expand: true, cwd: 'src/', src: ['View-tp.js'], dest: '<%= meta.dist_dir %>/view/'},
                    {expand: true, cwd: 'src/', src: ['plugin.json'], dest: '<%= meta.dist_dir %>/'}
                ]
            }
        },
        compress: {
            dist: {
                options: {
                    archive: 'view_' + '<%= meta.version_string %>_<%= meta.build_date %>' + '.zip'
                },
                expand: true,
                cwd: 'dist/',
                src: ['**/*']
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-compress');

    grunt.registerTask('default', ['concat', 'uglify', 'copy', 'compress']);
};

processContent above doesn't work. Please suggest solutions.

Zanazander answered 18/7, 2013 at 13:15 Comment(0)
V
8

The options.processContent property is indeed a function. You can easily hook it up with the builtin process templating of grunt.

This snippet does your <%= pkg.version %> trick.

grunt.initConfig({
    pkg:     grunt.file.readJSON("package.json"),
    distdir: 'dist',
    srcdir:  'src',
    copy:    {
      index:  {
        options: {
          processContent: function (content, srcpath) {
            return grunt.template.process(content);
          }
        },
        src:  '<%= srcdir %>/index.html',
        dest: '<%= distdir %>/index.html'
      }
    }
});
Viviparous answered 5/9, 2013 at 7:26 Comment(1)
Since version 0.5.0 of grunt-contrib-copy, the option processContent is renamed to process.Geis
M
4

Try something like this.

processContent: function(content, srcpath) {  
    content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');  
    content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, '');  
    return content;  
}
Marginal answered 14/8, 2013 at 18:5 Comment(2)
Thanks. Is it possible to call "standard" template replacement functions from processContent?Zanazander
Thanks for the example, @A. This got me up and running in less than a minute.Hilly
S
3

processContent is a function type. See documentation: https://github.com/gruntjs/grunt-contrib-copy#processcontent

processContent
Type: Function(content, srcpath)

This option is passed to grunt.file.copy as an advanced way to control the file contents that are copied.
Substage answered 14/8, 2013 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.