UNPKG

1.16 kBJavaScriptView Raw
1var Promise = require('bluebird');
2var streamRead = require('./stream');
3
4function bundleFiles(list, allfiles, route, concatChar, removeComments) {
5 var p = Promise.resolve('');
6
7 list.filter(function (b) {
8 var file = allfiles.filter(function (path) {
9 return b.indexOf(path) >= 0;
10 });
11 if (file.length > 0) {
12 p = p.then(function (content) {
13 return streamRead(route.get(file[0])).then(function (str) {
14 return combine(content, str, concatChar, removeComments);
15 });
16 });
17 }
18 });
19
20 return p;
21}
22
23function combine(content, target, concatChar, removeComments) {
24 if (content.length > 0) {
25 content += concatChar;
26 if (!removeComments) {
27 // avoiding an issue when the bottom line of target
28 // is the single line comment, add a newline end of it
29 content += '\n';
30 }
31 }
32 if (removeComments) {
33 target = target
34 // remove the target's comments
35 .replace(/\/\*(.|\n)*?\*\//mg, '')
36 // and remove spaces at begin and end
37 .replace(/(^\s*|\s*$)/g, '');
38 }
39 return content + target;
40}
41
42module.exports = {
43 bundleFiles: bundleFiles,
44 combine: combine,
45};