Gruntjs:复制文件时替换模板 [英] Gruntjs: replace templates when copying a file

查看:124
本文介绍了Gruntjs:复制文件时替换模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的Gruntjs脚本应该

I am writing a Gruntjs script which should


  • 将一些JS文件的模板替换为目标目录(contrib-concat) / li>
  • 复制并替换其他一些文件的模板

  • 将这些文件打包为zip文件

contrib-concat有一个布尔选项 process 来替换模板(如<%pkg.version%> )。

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

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

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不起作用。请提出解决方案。

processContent above doesn't work. Please suggest solutions.

推荐答案

options.processContent 功能。您可以轻松地使用内置的 grunt的过程模板

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

此片段会影响您的<%= pkg.version%> 技巧。

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

这篇关于Gruntjs:复制文件时替换模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆