7

Tôi đang viết một kịch bản Gruntjs mà nênGruntjs: thay thế các mẫu khi sao chép một tập tin

  • concatenate + hãy thay bản mẫu của một số tập tin JS vào thư mục đích (contrib-concat)
  • bản + hãy thay bản mẫu của một số các file khác (contrib-copy)
  • gói các tập tin vào một tập tin zip

contrib-concat đã một boolean tùy chọn quá trình để thay thế mẫu (như <% pkg.version %>) khi xử lý tệp.

bản sao đóng góp cũng có tùy chọn processContent, tuy nhiên tôi không biết cách kích hoạt xử lý mẫu bằng tùy chọn này.

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 không hoạt động. Vui lòng đề xuất giải pháp.

Trả lời

8

Thuộc tính options.processContent thực sự là một chức năng . Bạn có thể dễ dàng kết nối nó với builtin process templating of grunt.

Đoạn mã này thực hiện thủ thuật <%= pkg.version %> của bạn.

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

Kể từ phiên bản 0.5.0 của bản sao grunt-contrib, tùy chọn 'processContent' được đổi tên thành 'process'. – TLindig

4

Hãy thử một cái gì đó như thế này.

processContent: function(content, srcpath) { 
    content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, ''); 
    content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]+$/, ''); 
    return content; 
} 
+0

Cảm ơn. Có thể gọi các hàm thay thế mẫu "chuẩn" từ processContent không? – matejk

+0

Cảm ơn ví dụ, @A. Điều này đã cho tôi và chạy trong chưa đầy một phút. – SimplGy

Các vấn đề liên quan