-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jakefile
90 lines (74 loc) · 2.07 KB
/
Jakefile
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
var fs = require('fs'),
sys = require('util'),
jshint = require('jshint'),
uglify = require('uglify-js');
var sourceFiles = [
'ancestorscroll.js',
'event.js',
'measure.js',
'zManager.js'
];
function makeDirectoryIfNotExists(path) {
try {
var stats = fs.statSync(path);
if (!stats.isDirectory()) {
fs.mkdirSync(path, 0);
}
} catch (e) {
fs.mkdirSync(path, 0);
}
}
function concatenate(files, outputFile, withPreamble) {
var output = '',
license = fs.readFileSync('src/license.tmpl').toString(),
template = fs.readFileSync('src/result.tmpl').toString();
var all = '';
files.forEach(function(file, i) {
all += fs.readFileSync('src/' + file).toString();
all += '\n';
});
if (withPreamble) {
output += license;
output += template.replace('{{CONTENT}}', all);
} else {
output = all;
}
makeDirectoryIfNotExists('output');
fs.openSync('output/' + outputFile, 'w+');
var out = fs.openSync('output/' + outputFile, 'w+');
fs.writeSync(out, output);
}
desc('Concatenation');
task('concatenate', ['lint'], function(params) {
concatenate(sourceFiles, 'grabbag.js', true);
});
desc('Code Lint');
task('lint', function() {
sourceFiles.forEach(function(file, i) {
var all = fs.readFileSync('src/' + file).toString();
var result = jshint.JSHINT(all, {}, {});
if (!result) {
for (var i = 0; i < jshint.JSHINT.errors.length; i++) {
var error = jshint.JSHINT.errors[i];
console.error('file: ' + file + ', line: ' + error.line + ', char ' + error.character + ': ' + error.reason);
}
}
});
});
desc('Obfuscation and Compression');
task({'minify': ['concatenate']}, function(params) {
function minify(inputFile, outputFile) {
try {
var all = fs.readFileSync('output/' + inputFile).toString(),
out = fs.openSync('output/' + outputFile, 'w+'),
ast = uglify.parser.parse(all);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
fs.writeSync(out, uglify.uglify.gen_code(ast));
} catch(e) {
console.error(e);
}
}
minify('grabbag.js', 'grabbag.min.js');
});
task('default', ['minify'], function() {});