7

내가Gruntjs : 파일

  • CONCATENATE가 + 대상 디렉토리 (있는 contrib-CONCAT)에 일부 JS 파일의 템플릿을 대체해야 Gruntjs 스크립트를 쓰고 있어요
  • 복사 + 몇 가지의 템플릿을 대체를 복사 할 때 템플릿을 대체 다른 파일 (있는 contrib 복사)
  • 패키지 zip 파일에 파일이

있는 contrib-CONCAT 부울 옵션 과정 교체 할 수있다 파일을 처리 할 때 템플릿 (예 : <% pkg.version %>).

있는 contrib 복사는 그러나 나는이 옵션을 사용하여 템플릿 처리를 실행하는 방법을 모른다, 옵션 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가 작동하지 않습니다. 해결책을 제안하십시오.

답변

8

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

grunt-contrib-copy 버전 0.5.0 이후로,'processContent' 옵션은'process'로 이름이 변경되었습니다. – TLindig

4

다음과 같이 시도해보십시오.

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

감사합니다. processContent에서 "표준"템플릿 대체 함수를 호출 할 수 있습니까? – matejk

+0

예를 주셔서 감사합니다, @A. 이것은 1 분 안에 나에게 달려 들고 달렸다. – SimplGy

관련 문제