forked from zhangziqiu/oojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake.js
More file actions
144 lines (128 loc) · 4.13 KB
/
Copy pathmake.js
File metadata and controls
144 lines (128 loc) · 4.13 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var make = {
name: 'make',
sourceFiles: {
coreFilePath: "./src/core.js",
eventFilePath: "./src/event.js",
loaderFilePath: "./src/loader.js"
},
source: {
result: "",
path: './bin/oojs.source.js',
stream: null
},
format: {
result: "",
path: './bin/oojs.format.js',
stream: null
},
compress: {
result: "",
path: './bin/oojs.compress.js',
stream: null
},
gzip: {
result: "",
path: './bin/oojs.compress.js.gz',
stream: null
},
compile: function () {
var result = "";
var fs = require('fs');
var path, key;
for (key in this.sourceFiles) {
if (key && this.sourceFiles[key] && this.sourceFiles.hasOwnProperty(key)) {
path = this.sourceFiles[key];
console.log(path);
result += fs.readFileSync(path, 'utf8');
result += '\n';
}
}
return result;
},
make: function () {
//为Function对象添加proxy函数
Function.prototype.proxy = function (context) {
var method = this;
var args = Array.prototype.slice.apply(arguments);
var obj = args.shift();
return function () {
var tempArgs = Array.prototype.slice.apply(arguments);
return method.apply(obj, tempArgs.concat(args));
}
}
try {
var uglify = require('uglify-js');
}
catch (ex) {
this.preinstall(this.build.proxy(this));
}
this.build();
},
preinstall: function (callback) {
console.log('can not find "uglify-js", installing......');
var cp = require('child_process');
cp.exec('npm install uglify-js', function (err, stdout, stderr) {
console.log(stdout);
console.log('uglify-js install finished');
callback();
});
},
build: function () {
console.log('building......');
//获取合并后的文件字符串
console.log('compile start......');
var sourceString = this.compile();
console.log('compile end......');
console.log('uglify parse start......');
var fs = require('fs');
var uglify = require("uglify-js");
var ast;
try {
ast = uglify.parse(sourceString);
}
catch (ex) {
console.log(ex);
}
console.log('uglify parse end......');
//source
this.source.stream = uglify.OutputStream({
beautify: true,
comments: true,
width: 120
});
ast.print(this.source.stream);
this.source.result = this.source.stream.toString();
fs.writeFileSync(this.source.path, this.source.result);
console.log('compile :' + this.source.path + '" successful!');
//format
this.format.stream = uglify.OutputStream({
beautify: true,
comments: false,
width: 120
});
ast.print(this.format.stream);
this.format.result = this.format.stream.toString();
fs.writeFileSync(this.format.path, this.format.result);
console.log('compile :' + this.format.path + '" successful!');
//compress
this.compress.result = uglify.minify(this.source.result, {
fromString: true
}).code;
fs.writeFileSync(this.compress.path, this.compress.result);
console.log('compile :' + this.compress.path + '" successful!');
//gzip
//首先将compress的结果写入文件
fs.writeFileSync(this.gzip.path + "_temp", this.compress.result);
var zlib = require('zlib');
var gz = zlib.createGzip({
level: 9
})
var inp = fs.createReadStream(this.gzip.path + "_temp");
var out = fs.createWriteStream(this.gzip.path);
inp.pipe(gz).pipe(out);
console.log('compile :' + this.gzip.path + '" successful!');
fs.unlinkSync(this.gzip.path + "_temp");
console.log('build finished');
}
}
make.make();