-
Notifications
You must be signed in to change notification settings - Fork 72
/
index.js
66 lines (53 loc) · 1.49 KB
/
index.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
'use strict';
const resolve = require('path').resolve;
const Concat = require('concat-with-sourcemaps');
const defs = {
sep: '',
map: false,
base: '',
output: null
};
module.exports = function (task) {
const notify = (str, obj) => task.emit(`plugin_${str}`, Object.assign(obj, { plugin:'@taskr/concat' }));
const warning = str => notify('warning', { warning:str });
const error = str => notify('error', { error:str });
task.plugin('concat', { every:false }, function * (arr, o) {
if (typeof o === 'string') {
o = { output:o };
}
o = Object.assign({}, defs, o);
if (!o.output) {
return error('Must receive an `output` filename.');
}
if (!arr.length) {
return warning('No source files to concatenate!');
}
const bundle = new Concat(o.map, o.output, o.sep);
for (const file of arr) {
bundle.add(file.base, file.data, file.map || file.sourceMap);
}
// if did not specify a `base`, assume first file's location
const dir = o.base ? resolve(task.root, o.base) : arr[0].dir;
// concat'd content
let data = bundle.content;
// reset
this._.files = [];
if (o.map && bundle.sourceMap) {
const mapFile = o.output.concat('.map');
// add link to sourcemap file
data += new Buffer(`\n//# sourceMappingURL=${mapFile}`);
// add sourcemap file definition
this._.files.push({
dir: dir,
base: mapFile,
data: bundle.sourceMap
});
}
// always add the concat'd file
this._.files.push({
dir: dir,
base: o.output,
data: data
});
});
};