forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jakefile.js
107 lines (90 loc) · 2.32 KB
/
Jakefile.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
/**
* Jake build script for math.js
*/
var jake = require('jake'),
util = require('./tools/jake-utils.js'),
browserify = require('browserify');
/**
* Constants
*/
var INDEX = './index.js';
var HEADER = './lib/header.js';
var SHIM = './lib/shim.js';
var DIST = './dist';
var MATHJS = DIST + '/math.js';
var MATHJS_MIN = DIST + '/math.min.js';
// register start time
var start = +new Date();
/**
* default task
*/
desc('Execute all tasks: bundle and minify the library');
task('default', ['bundle', 'minify'], function () {
// calculate duration
var end = +new Date();
var duration = end - start;
console.log('Done (' + duration + ' ms)');
});
/**
* Bundle task
*/
desc('Bundle the library');
task('bundle', {async: true}, function () {
var b = browserify();
// make directory dist
jake.mkdirP(DIST);
b.add(INDEX);
b.bundle({
standalone: 'mathjs'
}, function (err, code) {
if(err) {
throw err;
}
// add header and shim
var lib = util.read(HEADER) + code + util.read(SHIM);
// write bundled file
util.write(MATHJS, lib);
// update version number and stuff in the javascript files
updateVersion(MATHJS);
console.log('Bundled library ' + MATHJS + ' (' + filesize(lib.length, 1) + ')');
complete();
});
});
/**
* minify task
*/
desc('Minify the library');
task('minify', ['bundle'], function () {
var result = util.minify({
src: MATHJS,
dest: MATHJS_MIN,
header: util.read(HEADER)
});
updateVersion(MATHJS_MIN);
console.log('Minified library ' + MATHJS_MIN + ' (' + filesize(result.code.length, 1) + ')');
});
// TODO: test the bundled library and minified library (not the sources)
/**
* Update version and date patterns in given file.
* Patterns '@@date' and '@@version' will be replaced with current date and
* version.
* @param {String} file
*/
function updateVersion(file) {
// update date and version number
util.replace({
replacements: [
{pattern: '@@date', replacement: util.today()},
{pattern: '@@version', replacement: util.version()}
],
src: file
});
}
/**
* Return the filesize in kilo bytes
* @param {Number} size Size in Bytes
* @return {String} str Formatted string like "50.7KB"
*/
function filesize (size) {
return (size / 1024).toPrecision(3) + 'KB';
}