1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var fs = require('fs')
|
12 | , socket = require('../lib/io')
|
13 | , uglify = require('uglify-js');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var template = '/*! Socket.IO.%ext% build:' + socket.version + ', %type%. Copyright(c) 2011 LearnBoost <dev@learnboost.com> MIT Licensed */\n'
|
22 | , development = template.replace('%type%', 'development').replace('%ext%', 'js')
|
23 | , production = template.replace('%type%', 'production').replace('%ext%', 'min.js');
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var starttagIF = '// if node'
|
34 | , endtagIF = '// end node';
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var base = [
|
45 | 'io.js'
|
46 | , 'util.js'
|
47 | , 'events.js'
|
48 | , 'json.js'
|
49 | , 'parser.js'
|
50 | , 'transport.js'
|
51 | , 'socket.js'
|
52 | , 'namespace.js'
|
53 | ];
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | var baseTransports = {
|
67 | 'websocket': ['transports/websocket.js']
|
68 | , 'flashsocket': [
|
69 | 'transports/websocket.js'
|
70 | , 'transports/flashsocket.js'
|
71 | , 'vendor/web-socket-js/swfobject.js'
|
72 | , 'vendor/web-socket-js/web_socket.js'
|
73 | ]
|
74 | , 'htmlfile': ['transports/xhr.js', 'transports/htmlfile.js']
|
75 | |
76 |
|
77 | , 'xhr-polling': ['transports/xhr.js', 'transports/xhr-polling.js']
|
78 | , 'jsonp-polling': [
|
79 | 'transports/xhr.js'
|
80 | , 'transports/xhr-polling.js'
|
81 | , 'transports/jsonp-polling.js'
|
82 | ]
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | var builder = module.exports = function () {
|
100 | var transports, options, callback, error = null
|
101 | , args = Array.prototype.slice.call(arguments, 0)
|
102 | , settings = {
|
103 | minify: true
|
104 | , node: false
|
105 | , custom: []
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 | args.forEach(function (arg) {
|
111 | var type = Object.prototype.toString.call(arg)
|
112 | .replace(/\[object\s(\w+)\]/gi , '$1' ).toLowerCase();
|
113 |
|
114 | switch (type) {
|
115 | case 'array':
|
116 | return transports = arg;
|
117 | case 'object':
|
118 | return options = arg;
|
119 | case 'function':
|
120 | return callback = arg;
|
121 | }
|
122 | });
|
123 |
|
124 |
|
125 | options = options || {};
|
126 | transports = transports || Object.keys(baseTransports);
|
127 |
|
128 |
|
129 | for(var option in options) {
|
130 | settings[option] = options[option];
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 | var files = [];
|
136 | base.forEach(function (file) {
|
137 | files.push(__dirname + '/../lib/' + file);
|
138 | });
|
139 |
|
140 | transports.forEach(function (transport) {
|
141 | var dependencies = baseTransports[transport];
|
142 | if (!dependencies) {
|
143 | error = 'Unsupported transport `' + transport + '` supplied as argument.';
|
144 | return;
|
145 | }
|
146 |
|
147 |
|
148 | dependencies.forEach(function (file) {
|
149 | var path = __dirname + '/../lib/' + file;
|
150 | if (!~files.indexOf(path)) files.push(path);
|
151 | })
|
152 | });
|
153 |
|
154 |
|
155 | if (error) return callback(error);
|
156 |
|
157 | var results = {};
|
158 | files.forEach(function (file) {
|
159 | fs.readFile(file, function (err, content) {
|
160 | if (err) error = err;
|
161 | results[file] = content;
|
162 |
|
163 |
|
164 |
|
165 | if (Object.keys(results).length !== files.length) return;
|
166 |
|
167 |
|
168 | if (error) return callback(error);
|
169 |
|
170 |
|
171 | var code = development
|
172 | , ignore = 0;
|
173 |
|
174 | files.forEach(function (file) {
|
175 | code += results[file];
|
176 | });
|
177 |
|
178 |
|
179 | if (settings.custom.length) {
|
180 | settings.custom.forEach(function (content) {
|
181 | code += content;
|
182 | });
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | if (!settings.node) {
|
189 | code = code.split('\n').filter(function (line) {
|
190 |
|
191 | var start = line.indexOf(starttagIF) >= 0
|
192 | , end = line.indexOf(endtagIF) >= 0
|
193 | , ret = ignore;
|
194 |
|
195 |
|
196 | if (start) {
|
197 | ignore++;
|
198 | ret = ignore;
|
199 | }
|
200 |
|
201 |
|
202 | if (end) {
|
203 | ignore--;
|
204 | }
|
205 |
|
206 | return ret == 0;
|
207 | }).join('\n');
|
208 | }
|
209 |
|
210 |
|
211 | if (settings.minify) {
|
212 |
|
213 | var separator = '@@OMGYUCHANGEME@@@';
|
214 | code = code.replace(/(\\ufffd)/g, separator);
|
215 |
|
216 | var ast = uglify.parser.parse(code);
|
217 | ast = uglify.uglify.ast_mangle(ast);
|
218 | ast = uglify.uglify.ast_squeeze(ast);
|
219 |
|
220 | code = production + uglify.uglify.gen_code(ast);
|
221 |
|
222 |
|
223 | code = code.replace(new RegExp('('+ separator + ')', 'g'), '\\ufffd');
|
224 | }
|
225 |
|
226 | callback(error, code);
|
227 | })
|
228 | })
|
229 | };
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | builder.version = socket.version;
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | builder.transports = baseTransports;
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | if (!module.parent){
|
257 |
|
258 | var args = process.argv.slice(2);
|
259 |
|
260 |
|
261 | builder(args.length ? args : false, { minify:false }, function (err, content) {
|
262 | if (err) return console.error(err);
|
263 |
|
264 | fs.write(
|
265 | fs.openSync(__dirname + '/../dist/socket.io.js', 'w')
|
266 | , content
|
267 | , 0
|
268 | , 'utf8'
|
269 | );
|
270 | console.log('Successfully generated the development build: socket.io.js');
|
271 | });
|
272 |
|
273 |
|
274 | builder(args.length ? args : false, function (err, content) {
|
275 | if (err) return console.error(err);
|
276 |
|
277 | fs.write(
|
278 | fs.openSync(__dirname + '/../dist/socket.io.min.js', 'w')
|
279 | , content
|
280 | , 0
|
281 | , 'utf8'
|
282 | );
|
283 | console.log('Successfully generated the production build: socket.io.min.js');
|
284 | });
|
285 | }
|