This repository has been archived by the owner on Sep 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
build-js.js
134 lines (105 loc) · 3.4 KB
/
build-js.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
/* eslint strict: 0, no-var: 0 */
'use strict';
var fs = require('fs'),
path = require('path'),
// Processes
depTree = require('dependency-tree'),
babel = require('babel-core'),
uglify = require('uglify-js'),
// Helpers
log = require('./helpers/log'),
writeTo = require('./helpers/writeTo'),
cleanOutput = require('./helpers/cleanOutput'),
prependBanner = require('./helpers/prependBanner'),
generateGraph = require('./helpers/generateGraph');
module.exports = function(command) {
let babelOptions = JSON.parse(fs.readFileSync('.babelrc', 'utf8')),
options = command.parent;
return new Promise(function(resolve) {
log.title('titon:js');
if (options.modules.length) {
resolve(generateGraph('js', options));
// If no modules selected, pull in the index file
} else {
resolve(['index.js']);
}
})
// Bundle modules
.then(function(paths) {
log('Bundling modules...');
let tree = {};
paths.forEach(function(module) {
let absPath = path.join(options.jsSource, module);
if (fs.existsSync(absPath)) {
depTree.toList({
filename: absPath,
root: options.jsSource,
config: './help/tasks/config/paths.json'
}).forEach(function(item) {
tree[item] = true;
});
log(module, 1);
}
});
return Object.keys(tree);
})
// Compile the code to Babel and combine
.then(function(tree) {
log('Transpiling Babel...');
babelOptions.externalHelpers = 'global';
// Import the Babel helpers
let source = [
'let global = {};',
fs.readFileSync('node_modules/babel-core/external-helpers.js'),
'let babelHelpers = global.babelHelpers;'
];
// Import each dependency
tree.forEach(function(dep) {
let output = babel.transformFileSync(dep, babelOptions).code.trim();
if (dep.indexOf('Titon.js') >= 0) {
output = output.replace('%version%', options.package.version); // Add version
output = output.replace('%build%', Date.now().toString(36)); // Add unique build hash
}
source.push(output);
});
return source.join('\n\n');
})
// Clean up the output
.then(cleanOutput(options))
// Wrap content with an IIFE
.then(function(js) {
log('Wrapping output...');
return [
'(function(window, document) {',
'"use strict";',
js,
'})(window, document);'
].join('\n');
})
// Prepend the banner
.then(prependBanner(options))
// Save the unminified file
.then(writeTo('titon.js', options))
// Minify the JS
.then(function(js) {
log('Minifying JS...');
return uglify.minify(js, {
fromString: true,
mangle: true,
output: {
beautify: false,
comments: /titon/i
},
compress: {
unsafe: true
}
}).code;
})
// Save the minified file
.then(writeTo('titon.min.js', options))
// Finish task
.then(function(js) {
log.success('JavaScript compiled');
return js;
});
};