Gruntjs: replace templates when copying a file

[亡魂溺海] 提交于 2019-12-01 04:18:10

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'
      }
    }
});

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;  
}

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!