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