Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / controllers / bundleController.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
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
/*****************************************
 *   Bundle Controller
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
  
var BundleController = (function () {
  
  var url = require('url');
  var fs = require('fs');
  var path = require('path');
  
  var less = require('less');
  var cssmin = require('cssmin');
  var jsmin = require('uglify-js');
  
  var env = require('../environmentManager').EnvironmentManager;
  
  var config = null;
  
  function sortCss(a, b) {
    if (a === 'normalize.css') return -1;
    if (b === 'normalize.css') return 1;
    if (a === 'shCore.css') return -1;
    if (b === 'shCore.css') return 1;
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
  }
  
  function sortScripts(a, b) {
    if (a === 'init.js')
      return 1;
    if (b === 'init.js')
      return -1;
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
  }
  
  function createCssBundle() {
    var cssPath =  config.css.path;
    var bundleFilePath = path.join(config.css.outputPath, '_bundled.css');
    var mainFile = config.css.mainFile;
  
    var bundleCss = function () {
      var filesAfterLESS = fs.readdirSync(cssPath);
      filesAfterLESS.sort(sortCss);
      var bundleData = '';
  
      for (var filename in filesAfterLESS) {
        var fileExt = path.extname(filesAfterLESS[filename]);
  
        if (fileExt === '.css') {
          console.log(' - Bundling CSS ' + path.join(cssPath, filesAfterLESS[filename]));
          var cssFile = fs.readFileSync(path.join(cssPath, filesAfterLESS[filename]), 'utf-8');
          bundleData += (cssFile + '\n');
        }
      }
  
      if (config.css.minimize) {
        bundleData = cssmin.cssmin(bundleData);
      }
  
      fs.writeFileSync(bundleFilePath, bundleData, 'utf-8');
    }
  
    console.log('BundleController: CSS bundle path is ' + bundleFilePath);
    console.log('BundleController: Main LESS/CSS is ' + config.css.mainFile);
      
    try {
      fs.unlinkSync(bundleFilePath);
    } catch (ex) {}
  
  
    var fileExt = path.extname(mainFile);
  
    if (fileExt === '.less') {
      var lessData = fs.readFileSync(path.join(cssPath, mainFile), 'utf-8');
      var lessOptions = {
        paths: ['.', cssPath],  // specify search paths for @import directives
        filename: mainFile      // specify a filename, for better error messages
      };
  
      less.render(lessData, lessOptions, function (e, css) {
        var rawFileName = path.basename(mainFile, '.less');
        var cssFileName = rawFileName + '.css';
        console.log('BundleController: Creating CSS file from ' + mainFile);
        fs.writeFileSync(path.join(cssPath, cssFileName), css, 'utf-8');
        bundleCss();
      });
    } else {
      bundleCss();  
    }
  }
  
  function createScriptsBundle() {
    var jsPath = config.scripts.path;
    var bundleFilePath = path.join(config.scripts.outputPath, '_bundled.js');
    console.log('BundleController: JavaScript bundle path is ' + bundleFilePath);
  
    try {
      fs.unlinkSync(bundleFilePath);
    } catch (ex) {}
  
    var files = fs.readdirSync(jsPath);
    files.sort(sortScripts);
    var bundleData = '';
  
    for (var filename in files) {
      console.log(' - Bundling JavaScript ' + path.join(jsPath, files[filename]));
      var file = fs.readFileSync(path.join(jsPath, files[filename]), 'utf-8');
      bundleData += (file + '\n');
    }
  
    if (config.scripts.minimize) {
      var minified = jsmin.minify(bundleData, { fromString: true });
      bundleData = minified.code;
    }
  
    fs.writeFileSync(bundleFilePath, bundleData, 'utf-8');
  }
  
  return {
  
    init : function (configInit) {
      config = configInit;
      createCssBundle();
      createScriptsBundle();
    }
  
  };
  
}());
  
typeof(exports) != 'undefined' ? exports.BundleController = BundleController : null;