• grunt 自定义任务实现js文件的混淆及加密


     1 //自定义任务
     2 module.exports = function (grunt) {
     3     // 项目配置
     4     var http = require('http');
     5     var qs = require('querystring');
     6     var fs=require("fs");
     7 
     8     function write(name,temp){
     9         fs.writeFile("./release/"+name,temp,"utf8",function(err){
    10             if(err){
    11                 grunt.log.error('BODY: ' + err);
    12             }
    13         });
    14     }
    15 
    16     grunt.initConfig({
    17         minJs:{
    18             version:"0.9.7",
    19             rootPath:"./online/",
    20             resource:[{src:"RongIMClient.js",nameList:"RongIMClient.min.js,RongIMClient-0.9.7.min.js",operate:"pack"},
    21                 {src:"emoji-0.9.2.js",nameList:"RongIMClient.Emoji-0.9.2.min.js",operate:"pack"},
    22                 {src:"protobuf.js",nameList:"protobuf.min.js",operate:"uglify"},
    23                 {src:"swfobject.js",nameList:"swfobject.min.js",operate:"uglify"},
    24                 {src:"voice-0.9.1.js",nameList:"RongIMClient.voice-0.9.1.min.js",operate:"pack"},
    25                 {src:"xhrpolling.js",nameList:"xhrpolling.min.js",operate:"uglify"}],
    26             env:"Release"
    27         }
    28     });
    29     grunt.registerTask('minJs', '自定义压缩js文件', function () {
    30 
    31         this.async();
    32 
    33         grunt.log.writeln('Processing task...');
    34 
    35 
    36         var options = {
    37             hostname: 'tool.lu',
    38             port: 80,
    39             path: '/js/ajax.html',
    40             method: 'POST',
    41             headers: {
    42                 'Content-Type': 'application/x-www-form-urlencoded'
    43             }
    44         };
    45         grunt.config("minJs.resource").forEach(function(x){
    46             fs.readFile(grunt.config("minJs.rootPath")+ x.src,"utf8",function(err,data){
    47                 if(err){
    48                     grunt.log.error(err);
    49                     return;
    50                 }
    51                 if(grunt.config("minJs.env")==="Release"){
    52                     data=data.replace(/["navUrl-Debug"]/g,"["navUrl-Release"]");
    53                 }
    54                 var post_data = {
    55                     code: data,
    56                     operate: x.operate
    57                 };
    58                 var req = http.request(options, function (res) {
    59                     res.setEncoding('utf8');
    60                     var chunk="";
    61                     res.on('data', function (data) {
    62                         chunk+=data;
    63                     });
    64                     res.on("end",function(){
    65                         var temp=JSON.parse(chunk);
    66                         if(temp.status===true){
    67                             x.nameList.split(",").forEach(function(name){
    68                                 write(name,temp.text);
    69                             });
    70                         }else{
    71                             grunt.log.error(temp.message);
    72                         }
    73                     })
    74                 });
    75                 req.on('error', function (e) {
    76                     grunt.log.error('problem with request: ' + e.message);
    77                 });
    78                 req.write(qs.stringify(post_data));
    79                 req.end();
    80             });
    81         });
    82 
    83     });
    84 };
    View Code
  • 相关阅读:
    Apache Pulsar:实时数据处理中消息,计算和存储的统一
    在STICORP使用Apache Pulsar构建数据驱动的应用程序
    简介Apache Pulsar-下一代分布式消息系统
    新一代消息系统Apache Pulsar 2.1 重磅发布
    为什么透过链接服务器写入,速度会很慢
    在Windows7 下调试CodeSmith 注意事项
    魔幻的生活 荒诞的延续
    Git和Github的应用与命令方法总结
    SimpleDateFormat使用简析
    Java中Long与long的区别(转)
  • 原文地址:https://www.cnblogs.com/doublenet/p/4387682.html
Copyright © 2020-2023  润新知