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