javascript – Gruntjs:复制文件时替换模板

前端之家收集整理的这篇文章主要介绍了javascript – Gruntjs:复制文件时替换模板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在写一个应该的Gruntjs脚本

> concatenate将一些JS文件的模板替换为目标目录(contrib-concat)
>副本替换其他一些文件的模板(contrib-copy)
>将文件打包成zip文件

contrib-concat有一个布尔选项进程,用于在处理文件时替换模板(例如<%pkg.version%>).

contrib-copy还有一个选项processContent,但我不知道如何使用此选项触发模板处理.

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/MarksClass.js','src/ViewVersion.js'],dest: 'build/View.js'
            }
        },uglify: {
            options: {
                mangle: {
                    except: ['jQuery','Hammer']
                },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'],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不起作用.请建议解决方案.

解决方法

options.processContent属性确实是一个函数.您可以使用内置的 process templating of grunt轻松连接它.

代码段与您的<%= pkg.version%>特技.

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

猜你在找的JavaScript相关文章