javascript - Gruntjs: replace templates when copying a file -
i writing gruntjs script should
- concatenate + replace template of js files target directory (contrib-concat)
- copies + replace template of other files (contrib-copy)
- package files zip file
contrib-concat has boolean option process replace templates (like <% pkg.version %>) when processing files.
contrib-copy has option processcontent, don't know how trigger template processing 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.
the options.processcontent property indeed function. can hook builtin process templating of grunt.
this snippet <%= 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' } } });
Comments
Post a Comment