/
builder.js
198 lines (165 loc) · 4.48 KB
/
builder.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var util = require('./util'),
asset = require('./asset'),
path = require('path'),
fs = require('fs'),
uglify = require("uglify-js");
function collect(excluded, assets, includeDependencies) {
var collected = [];
assets.forEach(function(asset) {
if(!asset) {
console.warn('Undefined asset in assets list');
console.log(new Error().stack);
return;
}
var deps = [];
if (excluded.indexOf(asset.id) < 0) {
if (includeDependencies) {
deps = asset.dependencies();
}
excluded = excluded.concat([asset.id]);
collected = collected.concat(
collect(excluded, deps, includeDependencies)
).concat(asset);
}
});
return collected;
}
function dedupe(assets) {
var i, ii, elem, seen = {},
result = [];
for (i = 0, ii = assets.length; i < ii; i++) {
elem = assets[i];
if (!seen[elem.id]) {
seen[elem.id] = true;
result.push(elem);
}
}
return result;
}
function Builder(options) {
this.options = {};
util.extend(this.options, Builder.default_options);
util.extend(this.options, options || {});
this.assets = [];
this.excludes = this.options.excludes || [];
this.matchers = [];
this.preProcessor = options.preProcessor;
}
Builder.default_options = {
path: process.cwd(),
docroot: process.cwd(),
includeDependencies: true,
preProcessor: null
};
util.extend(Builder.prototype, {
include: function(ids) {
this.assets = this.mapAssets(arguments);
return this;
},
exclude: function(ex) {
var excludes;
if (ex.assets) {
excludes = ex.assets.map(function(dep) { return dep.id });
} else {
excludes = [].slice.call(arguments);
}
this.excludes = this.excludes.concat(excludes);
return this;
},
log: function(message, level) {
console.log(message);
},
path: function(id) {
return path.join(this.options.docroot, id);
},
modPath: function(id) {
return path.join(this.options.docroot, this.options.path, id);
},
addMatcher: function(regex, factory) {
this.matchers.push([regex, factory]);
},
matchAsset: function(id) {
var m, dep, asset;
var allMatchers = this.matchers.concat(builder.matchers);
for (var i=0, matcher; matcher = allMatchers[i]; i++) {
var regex = matcher[0], factory = matcher[1];
if (this.excludes.indexOf(id) < 0 && (m = id.match(regex))) {
asset = factory(id);
asset.builder = this;
return asset;
}
}
},
mapAssets: function(assets) {
var mapped = [];
for (var i=0, asset; asset = assets[i]; i++) {
if (typeof asset == 'string') {
asset = this.matchAsset(asset);
}
mapped.push(asset);
}
return mapped;
},
lint: function(options) {
this.collectedAssets().forEach(function(a) {
a.lint(options);
});
return this;
},
minify: function(options) {
if (options === false) {
this.minifyOptions = null;
} else {
this.minifyOptions = (typeof options == 'object') ? options : {};
}
return this;
},
minifySource: function(source) {
this.log('- Minifying bundle');
var ast, opts = util.extend({}, this.options.minify || {});
util.extend(opts, this.minifyOptions);
ast = uglify.parser.parse(source);
ast = uglify.uglify.ast_mangle(ast, opts);
ast = uglify.uglify.ast_squeeze(ast, opts);
return uglify.uglify.gen_code(ast, opts);
},
toSource: function() {
var source = this.collectedAssets().map(function(a) {
this.log('* ' + a.id);
return a.toSource();
}, this).join('\n');
if (this.minifyOptions) {
source = this.minifySource(source);
}
return source;
},
write: function(path, success) {
fs.writeFile(
path, this.toSource(),
'utf8', success || function() {}
);
this.log('> ' + path);
return this;
},
collectedAssets: function() {
return dedupe(collect(this.excludes, this.assets, this.options.includeDependencies));
}
});
function builder(options) {
return new Builder(options);
}
builder.asset = asset;
builder.analyzer = require('./analyzer');
builder.matchers = [];
builder.matchers.add = function(regex, factory) {
this.unshift([regex, factory]);
}
asset.Module.regexp = /^\.?[a-zA-Z0-9_\-\/]+$/;
builder.matchers.add(asset.Module.regexp, function(id) {
return new asset.Module(id);
})
asset.Script.regexp = /.js$/;
builder.matchers.add(asset.Script.regexp, function(id) {
return new asset.Script(id);
})
module.exports = builder;