1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.cssCompilation = function (next) {
|
15 | var NA = this,
|
16 | async = NA.modules.async;
|
17 |
|
18 | if (NA.configuration.generate || NA.webconfig.cssBundlingBeforeResponse) {
|
19 | async.parallel([
|
20 | NA.lessCompilation.bind(NA),
|
21 | NA.stylusCompilation.bind(NA)
|
22 | ], function () {
|
23 | NA.cssMinification(next);
|
24 | });
|
25 | } else {
|
26 | next();
|
27 | }
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.lessCompilation = function (next) {
|
39 | var NA = this,
|
40 | enableLess = NA.webconfig.less,
|
41 | async = NA.modules.async,
|
42 | path = NA.modules.path,
|
43 | less = NA.modules.less,
|
44 | fs = NA.modules.fs,
|
45 | allLessCompiled,
|
46 | data = {},
|
47 | paths = [];
|
48 |
|
49 | if (enableLess && enableLess.files) {
|
50 |
|
51 | allLessCompiled = enableLess.files;
|
52 |
|
53 | if (enableLess.paths && paths.length === 0) {
|
54 | for (var i = 0; i < enableLess.paths.length; i++) {
|
55 | paths[i] = path.join(NA.webconfig.assetsRelativePath, enableLess.paths[i]);
|
56 | }
|
57 | } else if (paths.length === 0) {
|
58 | paths = [
|
59 | NA.webconfig.assetsRelativePath,
|
60 | path.join(NA.webconfig.assetsRelativePath, 'stylesheets'),
|
61 | path.join(NA.webconfig.assetsRelativePath, 'styles'),
|
62 | path.join(NA.webconfig.assetsRelativePath, 'css')
|
63 | ];
|
64 | }
|
65 |
|
66 | async.each(allLessCompiled, function (compiledFile, next) {
|
67 | var currentFile = fs.readFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile), 'utf-8'),
|
68 | prefixLess = new NA.modules.prefixLess(),
|
69 | options = {
|
70 | paths: paths
|
71 | };
|
72 |
|
73 | if (enableLess.autoprefix) {
|
74 | options.plugins = [prefixLess];
|
75 | }
|
76 |
|
77 | less.render(currentFile, options, function (e, output) {
|
78 | if (e) {
|
79 | NA.log(e);
|
80 | }
|
81 |
|
82 | data.pathName = path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile.replace(/\.less$/g,'.css'));
|
83 | fs.writeFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile.replace(/\.less$/g,'.css')), output.css);
|
84 | NA.log(NA.cliLabels.lessGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
85 | next();
|
86 | });
|
87 | }, function () {
|
88 | if (next) {
|
89 | next();
|
90 | }
|
91 | });
|
92 | } else {
|
93 | if (next) {
|
94 | next();
|
95 | }
|
96 | }
|
97 |
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | exports.stylusCompilation = function (next) {
|
109 | var NA = this,
|
110 | enableStylus = NA.webconfig.stylus,
|
111 | async = NA.modules.async,
|
112 | path = NA.modules.path,
|
113 | stylus = NA.modules.stylus,
|
114 | fs = NA.modules.fs,
|
115 | allStylusCompiled,
|
116 | data = {},
|
117 | paths = [];
|
118 |
|
119 | if (enableStylus && enableStylus.files) {
|
120 |
|
121 | allStylusCompiled = enableStylus.files;
|
122 |
|
123 | if (enableStylus.paths && paths.length === 0) {
|
124 | for (var i = 0; i < enableStylus.paths.length; i++) {
|
125 | paths[i] = path.join(NA.webconfig.assetsRelativePath, enableStylus.paths[i]);
|
126 | }
|
127 | } else if (paths.length === 0) {
|
128 | paths = [
|
129 | NA.webconfig.assetsRelativePath,
|
130 | path.join(NA.webconfig.assetsRelativePath, 'stylesheets'),
|
131 | path.join(NA.webconfig.assetsRelativePath, 'styles'),
|
132 | path.join(NA.webconfig.assetsRelativePath, 'css')
|
133 | ];
|
134 | }
|
135 |
|
136 | async.each(allStylusCompiled, function (compiledFile, next) {
|
137 | var currentFile = fs.readFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile), 'utf-8'),
|
138 | stylusFn = stylus(currentFile);
|
139 |
|
140 | if (enableStylus.autoprefix) {
|
141 | stylusFn = stylusFn.use(NA.modules.prefixStylus());
|
142 | }
|
143 |
|
144 | stylusFn
|
145 | .set('paths', paths)
|
146 | .render(function (e, output) {
|
147 | if (e) {
|
148 | NA.log(e);
|
149 | }
|
150 |
|
151 | data.pathName = path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile.replace(/\.styl$/g,'.css'));
|
152 | fs.writeFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compiledFile.replace(/\.styl$/g,'.css')), output);
|
153 | NA.log(NA.cliLabels.stylusGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
154 | next();
|
155 | });
|
156 | }, function () {
|
157 | if (next) {
|
158 | next();
|
159 | }
|
160 | });
|
161 | } else {
|
162 | if (next) {
|
163 | next();
|
164 | }
|
165 | }
|
166 |
|
167 | };
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | exports.cssMinification = function (next) {
|
178 | var NA = this,
|
179 | bundles = NA.webconfig.bundles,
|
180 | cleanCss = NA.modules.cleanCss,
|
181 | async = NA.modules.async,
|
182 | path = NA.modules.path,
|
183 | fs = NA.modules.fs,
|
184 | enable,
|
185 | output = "",
|
186 | data = {},
|
187 | allCssMinified = [];
|
188 |
|
189 |
|
190 | enable = (NA.configuration.generate ||
|
191 |
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | NA.webconfig.cssBundlingBeforeResponse);
|
201 |
|
202 | if (typeof NA.webconfig.cssBundlingEnable === "boolean") {
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | enable = NA.webconfig.cssBundlingEnable;
|
213 | }
|
214 |
|
215 |
|
216 | if (bundles && bundles.stylesheets && enable) {
|
217 |
|
218 | NA.forEach(bundles.stylesheets, function (compressedFile) {
|
219 | allCssMinified.push(compressedFile);
|
220 | });
|
221 |
|
222 | async.each(allCssMinified, function (compressedFile, firstCallback) {
|
223 |
|
224 | async.map(bundles.stylesheets[compressedFile], function (sourceFile, secondCallback) {
|
225 | try {
|
226 | secondCallback(null, fs.readFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, sourceFile), 'utf-8'));
|
227 | } catch (e) {
|
228 | secondCallback(e, "");
|
229 | }
|
230 | }, function(error, results) {
|
231 | if (error) {
|
232 | throw error;
|
233 | }
|
234 | for (var i = 0; i < results.length; i++) {
|
235 | output += results[i];
|
236 | }
|
237 |
|
238 | output = (new cleanCss().minify(output)).styles;
|
239 | compressedFile = compressedFile.replace(/{version}/g, NA.webconfig.version);
|
240 | fs.writeFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compressedFile), output);
|
241 | data.pathName = path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compressedFile);
|
242 | NA.log(NA.cliLabels.cssGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
243 | output = "";
|
244 |
|
245 | firstCallback();
|
246 | });
|
247 |
|
248 | }, function () {
|
249 | if (next) {
|
250 | next();
|
251 | }
|
252 | });
|
253 | } else {
|
254 | if (next) {
|
255 | next();
|
256 | }
|
257 | }
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | exports.jsObfuscation = function (next) {
|
269 | var NA = this,
|
270 | bundles = NA.webconfig.bundles,
|
271 | uglifyEs = NA.modules.uglifyEs,
|
272 | async = NA.modules.async,
|
273 | path = NA.modules.path,
|
274 | fs = NA.modules.fs,
|
275 | enable,
|
276 | output = "",
|
277 | data = {},
|
278 | allJsMinified = [];
|
279 |
|
280 |
|
281 | enable = (NA.configuration.generate ||
|
282 |
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | NA.webconfig.jsBundlingBeforeResponse);
|
292 |
|
293 | if (typeof NA.webconfig.jsBundlingEnable === "boolean") {
|
294 |
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | enable = NA.webconfig.jsBundlingEnable;
|
304 | }
|
305 |
|
306 |
|
307 | if (bundles && bundles.javascripts && enable) {
|
308 |
|
309 | NA.forEach(bundles.javascripts, function (compressedFile) {
|
310 | allJsMinified.push(compressedFile);
|
311 | });
|
312 |
|
313 | async.each(allJsMinified, function (compressedFile, firstCallback) {
|
314 |
|
315 | async.map(bundles.javascripts[compressedFile], function (sourceFile, secondCallback) {
|
316 | var code,
|
317 | result,
|
318 | file;
|
319 |
|
320 |
|
321 | if (path.join("/", NA.webconfig.socketClientFile) === path.join("/", sourceFile)) {
|
322 | file = fs.readFileSync(path.join(NA.nodeatlasPath, "src", "socket.io.js"), "utf-8")
|
323 | .replace(/%urlRelativeSubPath%/g, NA.webconfig.urlRelativeSubPath.slice(1))
|
324 | .replace(/%urlRoot%/g, NA.webconfig.urlRoot);
|
325 |
|
326 | result = uglifyEs.minify(file);
|
327 |
|
328 | } else {
|
329 | code = fs.readFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, sourceFile), "utf-8");
|
330 | result = uglifyEs.minify(code);
|
331 | }
|
332 |
|
333 | secondCallback(null, result.code);
|
334 | }, function(error, results) {
|
335 | if (error) {
|
336 | throw error;
|
337 | }
|
338 |
|
339 | for (var i = 0; i < results.length; i++) {
|
340 | output += results[i];
|
341 | }
|
342 |
|
343 | compressedFile = compressedFile.replace(/{version}/g, NA.webconfig.version);
|
344 | fs.writeFileSync(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compressedFile), output);
|
345 | data.pathName = path.join(NA.serverPath, NA.webconfig.assetsRelativePath, compressedFile);
|
346 | NA.log(NA.cliLabels.jsGenerate.replace(/%([\-a-zA-Z0-9_]+)%/g, function (regex, matches) { return data[matches]; }));
|
347 | output = "";
|
348 |
|
349 | firstCallback();
|
350 | });
|
351 |
|
352 | }, function () {
|
353 |
|
354 | if (next) {
|
355 | next();
|
356 | }
|
357 | });
|
358 | } else {
|
359 | if (next) {
|
360 | next();
|
361 | }
|
362 | }
|
363 | };
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | exports.cssAlreadyParse = function (newPath, allCssFiles, inject) {
|
375 | var NA = this,
|
376 | path = NA.modules.path;
|
377 |
|
378 | for (var i = 0; i < allCssFiles.length; i++) {
|
379 | if (path.join(NA.serverPath, NA.webconfig.assetsRelativePath, newPath) === allCssFiles[i]) {
|
380 | inject = false;
|
381 | }
|
382 | }
|
383 |
|
384 | return inject;
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | exports.injectCssAuth = function (pathFile, allCssFiles, inject) {
|
397 | var NA = this,
|
398 | path = NA.modules.path;
|
399 |
|
400 | if (inject) {
|
401 | allCssFiles.push(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, pathFile));
|
402 | }
|
403 | };
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | exports.prepareCssInjection = function (allCssFiles, injection) {
|
414 | var NA = this,
|
415 | path = NA.modules.path,
|
416 | inject = true,
|
417 |
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | specificInjection = injection,
|
426 |
|
427 | |
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 | commonInjection = NA.webconfig.injectCss;
|
435 |
|
436 |
|
437 | if (typeof commonInjection === 'string') {
|
438 | allCssFiles.push(path.join(NA.serverPath, NA.webconfig.assetsRelativePath, commonInjection));
|
439 | } else if (commonInjection) {
|
440 | for (var i = 0; i < commonInjection.length; i++) {
|
441 |
|
442 | NA.injectCssAuth(NA.webconfig.injectCss[i], allCssFiles, inject);
|
443 | }
|
444 | }
|
445 |
|
446 |
|
447 | if (specificInjection) {
|
448 | if (typeof specificInjection === 'string') {
|
449 |
|
450 | inject = NA.cssAlreadyParse(specificInjection, allCssFiles, inject);
|
451 |
|
452 | NA.injectCssAuth(specificInjection, allCssFiles, inject);
|
453 | } else {
|
454 | for (var j = 0; j < specificInjection.length; j++) {
|
455 |
|
456 | inject = NA.cssAlreadyParse(specificInjection[j], allCssFiles, inject);
|
457 |
|
458 | NA.injectCssAuth(specificInjection[j], allCssFiles, inject);
|
459 | inject = true;
|
460 | }
|
461 | }
|
462 | }
|
463 |
|
464 | return allCssFiles;
|
465 | };
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | exports.injectCss = function (data, injection, mainCallback) {
|
477 | var NA = this,
|
478 | jsdom = NA.modules.jsdom;
|
479 | cssParse = NA.modules.cssParse,
|
480 | async = NA.modules.async,
|
481 | fs = NA.modules.fs,
|
482 | allCssFiles = [],
|
483 | dom = new jsdom.JSDOM(data);
|
484 |
|
485 |
|
486 | allCssFiles = NA.prepareCssInjection(allCssFiles, injection);
|
487 |
|
488 |
|
489 | async.map(allCssFiles, function (sourceFile, callback) {
|
490 |
|
491 | callback(null, fs.readFileSync(sourceFile, 'utf-8'));
|
492 | }, function(error, results) {
|
493 | var output = "",
|
494 | css;
|
495 |
|
496 | for (var i = 0; i < results.length; i++) {
|
497 | output += results[i];
|
498 | }
|
499 |
|
500 |
|
501 | css = cssParse(output);
|
502 |
|
503 |
|
504 | function parseCssRules(callback) {
|
505 | for (var i = 0; i < css.stylesheet.rules.length; i++) {
|
506 | if (typeof css.stylesheet.rules[i].selectors !== 'undefined') {
|
507 | callback(i);
|
508 | }
|
509 | }
|
510 | }
|
511 |
|
512 |
|
513 | function parseCssSelector(i, callback) {
|
514 | for (var j = 0; j < css.stylesheet.rules[i].selectors.length; j++) {
|
515 | callback(j);
|
516 | }
|
517 | }
|
518 |
|
519 |
|
520 | function parseCssDeclaration(i, j) {
|
521 | for (var k = 0; k < css.stylesheet.rules[i].declarations.length; k++) {
|
522 | Array.prototype.forEach.call(dom.window.document.querySelectorAll(css.stylesheet.rules[i].selectors[j]), function (node) {
|
523 | node.style[css.stylesheet.rules[i].declarations[k].property] = css.stylesheet.rules[i].declarations[k].value
|
524 | });
|
525 | }
|
526 | }
|
527 |
|
528 |
|
529 | parseCssRules(function (i) {
|
530 | parseCssSelector(i, function (j) {
|
531 | parseCssDeclaration(i, j);
|
532 | });
|
533 | });
|
534 |
|
535 | |
536 |
|
537 |
|
538 |
|
539 |
|
540 | mainCallback(dom.serialize());
|
541 | });
|
542 | }; |
\ | No newline at end of file |