-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseajs-compress.js
60 lines (49 loc) · 1.71 KB
/
seajs-compress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var fs = require('fs');
var gcc = require('closure-compiler')
exports.compress = function(srcFile,toFile,callback,option){
if (!(srcFile&&toFile)){
console.log('usage:node grunt-sea.js src-path to-path callback compress_option');
return;
}
callback = callback || function(){};
option = option || {};
var preFunction = 'function require(){alert(arguments.length)}';
fs.exists(srcFile, function (flag) {
if (flag){
fs.readFile(srcFile, "utf-8",function (err, data) {
if (err) throw err;
var content = data;
//replace require and make require a global function
content = content.replace(/define\(function\(([\w\s,]*)\)/ig, function(a,b){
if (!b) return a;
var arr = b.split(',');
arr[0]='henry';
return a.replace(b,arr.join(','));
});
content = preFunction + content;
fs.writeFile(toFile, content, {encoding:'utf8',flag:'w+'}, function (err) {
if (err) throw err;
gcc.compile(fs.readFileSync(toFile), {}, function(err, stdout, stderr){
if (err) throw err;
//delete global require function and replace define function's first argument
var content = stdout.replace(preFunction,'');
content = content.replace(/define\(function\(([\w\s,]*)\)/ig, function(a,b){
if (!b){
return a.replace('function()','function(require)') ;
}else{
var first = b.split(',')[0];
return a.replace(b,b.replace(first,'require'))
}
});
fs.writeFile(toFile, content, {encoding:'utf8',flag:'w+'}, function (err) {
if (err) throw err;
callback(true,'['+toFile+'] '+srcFile+' is compressed.');
});
});
});
});
}else{
callback(false,'error: src-path is not exist!');
}
});
}