forked from mikaelbr/SocialFeed.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
88 lines (71 loc) · 2.13 KB
/
make.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
require('shelljs/make');
require('shelljs/global');
var fs = require('fs')
, path = require('path')
, browserify = require('browserify')
, UglifyJS = require("uglify-js")
, less = require('less')
;
var buildPath = path.join(__dirname, 'socialfeed.js')
, minifiedPath = path.join(__dirname, 'socialfeed.min.js');
target.all = function () {
target.bundle(function () {
target.less(function () {
target.minify();
console.log('Bundle complete...')
});
});
};
target.bundle = function (cb) {
console.log('Starting bundling');
bundleResources('src/moduletemplates/', 'src/resources.js');
bundle(cb);
};
target.resources = function () {
bundleResources('src/moduletemplates/', 'src/resources.js');
};
target.less = function (cb) {
console.log('Compiling LESS');
var data = cat('src/style.less');
var parser = new(less.Parser)({
paths: ['./src']
, filename: 'style.less'
});
parser.parse(data, function (e, tree) {
var css = tree.toCSS()
, minified = tree.toCSS({ compress: true });
css.to('socialfeed.css');
console.log('Minifying CSS');
minified.to('socialfeed.min.css');
cb();
});
};
target.minify = function () {
console.log('Minifying JavaScript...');
UglifyJS.minify(buildPath).code.to(minifiedPath)
console.log('Minfying succeeded.');
};
function getResourcesList (templateFolder) {
var filenames = fs.readdirSync(path.join(__dirname, templateFolder));
return filenames.map(function (file) {
return file.replace('.html', '');
});
}
function bundleResources (source, target) {
var templates = getResourcesList(source);
var resourceString = "";
templates.forEach(function (template) {
resourceString += '\t"'+template+'": ' + JSON.stringify(cat(source + template + '.html')) + ",\n";
});
cat('src/resources.js.template').replace(/%BODY%/g, resourceString).to(path.join(__dirname, target));
}
function bundle(cb) {
cb = cb || function () {};
b = browserify();
b.add('./src/socialfeed.js')
b.bundle({standalone: 'SocialFeed'}, function (err, src) {
if (err) return console.error(err);
src.to(buildPath);
cb();
});
}