UNPKG

2.61 kBJavaScriptView Raw
1// Dependencies ---------------------------------------------------------------
2var less = require('less'),
3 uglifyjs = require('uglify-js'),
4 path = require('path'),
5 Task = require('../lib/task/Task');
6
7
8// Built-in concators ---------------------------------------------------------
9var types = {
10
11 js: {
12
13 mode: Task.All,
14
15 data: function(e) {
16 return !e.options.compress;
17 },
18
19 map: function(e, file) {
20
21 if (e.options.compress) {
22 return [file, file + '.map'];
23
24 } else {
25 return file;
26 }
27
28 },
29
30 run: function(e, done) {
31
32 if (e.options.compress) {
33
34 var sources = e.all.map(function(f) {
35 return f.path;
36 });
37
38 var m = uglifyjs.minify(sources, {
39 outSourceMap: path.basename(e.path)
40 });
41
42 // TODO copy source files so they can be found by the dev tools
43 done(null, [
44 m.code + '\n//@ sourceMappingURL=' + path.basename(e.mapped[1])
45 + '\n//# sourceMappingURL=' + path.basename(e.mapped[1]),
46
47 m.map.toString()
48 ]);
49
50 } else {
51
52 var data = e.all.map(function(f) {
53 return f.data.toString();
54 });
55
56 done(null, data.join('\n\n;'));
57
58 }
59
60 }
61
62 },
63
64 less: {
65
66 mode: Task.All,
67 data: true,
68
69 map: function(e) {
70 return e.config.file;
71 },
72
73 run: function(e, done) {
74
75 var data = e.all.map(function(f) {
76 return f.data.toString();
77 });
78
79 var parser = new less.Parser({
80 paths: [e.options.src]
81 });
82
83 // TODO support source maps
84 parser.parse(data.join('\n\n'), function(err, tree) {
85 done(err, err || tree.toCSS({
86 sourceMap: true,
87 compress: e.options.compress
88 }));
89 });
90
91 }
92
93 }
94
95};
96
97
98// Factory --------------------------------------------------------------------
99module.exports = {
100
101 task: function(pattern, type, file) {
102
103 if (!types.hasOwnProperty(type)) {
104 throw new Error('No concatenator found for type "' + type + '"');
105
106 } else {
107 return new Task('Concat: ' + type, pattern, types[type], {
108 type: type,
109 file: file
110 });
111 }
112
113 }
114
115};
116