将缩小的html放入javascript中以执行grunt任务

时间:2018-01-12 11:18:08

标签: gruntjs grunt-contrib-uglify grunt-contrib-concat

我必须缩小我的html并将其添加到缩小的js中作为innerHtml。目前,我正在运行以下grunt任务并手动将缩小的html复制到JS文件。但是,我怎样才能将缩小的html作为输出并将其放入js中,以便我可以在单次运行中完成所有任务。

module.exports = function(grunt) {

   grunt.initConfig({
       concat: {
           css: {
               src: ['src/css/style.css',...],
               dest: 'dest/css/main.css'
             },

           js: {
               src: ['src/js/angular.min.js',...],
               dest: 'dest/js/main.js'
             }
           },
       uglify: {
           js: {
               src: 'dest/js/main.js',
               dest: 'dest/js/main.min.js'
               }
           },
       cssmin: {
           css: {
               src: 'dest/css/main.css',
               dest: 'dest/css/main.min.css'
               }
           },
       minifyHtml: {
           options: {
            cdata: true
           },
           dist: {
            files: {
                'dest/html/index.html': 'src/html/index.html'
            }
        }
    }
       });

   grunt.loadNpmTasks('grunt-contrib-concat');
   grunt.loadNpmTasks('grunt-contrib-uglify');
   grunt.loadNpmTasks('grunt-contrib-cssmin');
   grunt.loadNpmTasks('grunt-minify-html');
   grunt.registerTask('build', ['concat', 'uglify', 'cssmin','minifyHtml']);
};

2 个答案:

答案 0 :(得分:1)

你试过grunt-contrib-copy吗?这是我的任务:

copy: {
  main: {
    expand: true,
    cwd: 'src/img',
    src: '**',
    dest: 'dist/img',
    flatten: true,
    filter: 'isFile',
  }
}

npm install grunt-contrib-copy

答案 1 :(得分:0)

我必须使用grunt-copy-part-of-file来管理这项工作。

'copy-part-of-file': {
      simple_replace_scripts: {
          options: {
              sourceFileStartPattern: '<!-- INDEX START -->',
              sourceFileEndPattern: '<!-- INDEX END -->',
              destinationFileStartPattern: "<!-- JS START -->'",
              destinationFileEndPattern: "'<!-- JS END -->"
          },
          files: {
              'dest/js/my.js': ['src/html/index.html']
          }
      }
  }