【问题标题】:Gruntjs: replace templates when copying a fileGruntjs:复制文件时替换模板
【发布时间】:2013-07-18 13:15:45
【问题描述】:

我正在编写一个 Gruntjs 脚本,应该

  • 将一些JS文件的模板连接+替换到目标目录(contrib-concat)
  • 副本+替换一些其他文件的模板(contrib-copy)
  • 将文件打包成 zip 文件

contrib-concat 有一个布尔选项 process 用于在处理文件时替换模板(如 <% 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/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 不起作用。请提出解决方案。

【问题讨论】:

    标签: javascript node.js continuous-integration gruntjs grunt-contrib-concat


    【解决方案1】:

    options.processContent 属性确实是一个函数。您可以轻松地将其与内置的 process templating of grunt 连接起来。

    这个 sn-p 可以满足您的 &lt;%= pkg.version %&gt; 技巧。

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

    【讨论】:

    • 从 grunt-contrib-copy 0.5.0 版本开始,选项processContent 被重命名为process
    【解决方案2】:

    试试这样的。

    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 调用“标准”模板替换函数?
    • 谢谢你的例子,@A。这让我在不到一分钟的时间内启动并运行。
    【解决方案3】:

    processContent 是一个函数类型。见文档: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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多