forked from digitaledgeit/npm-recursive-uglifyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (92 loc) · 3.04 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
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
var fs = require('fs');
var debug = require('debug')('recursive-uglifyjs');
var uglify = require('uglify-js');
var finder = require('finder-on-steroids');
/**
* Uglifies all JavaScript files in a given folder
* @param {string} directory
* @param {function} callback
*/
function recursiveUglifyJS(directory, callback) {
//find files
return finder(directory).files().name('*.js').find(function(err, files) {
if (err) return callback(err);
var
maxProcesses = 4,
activeProcesses = 0,
fileIndex = 0,
errors = []
;
/**
* Called when a process is finished
* @param {String} [file] The file that caused the process to finish
* @param {Error} [error] The error that caused the process to finish
*/
function finished(file, error) {
//keep a list of errors
if (error) {
error.file = file;
errors.push(error);
}
//end the current process
--activeProcesses;
//if there are no more active processes then notify the user we're finished
if (activeProcesses === 0) {
callback(errors.length ? errors : null);
}
}
/**
* Called to process the next file
*/
function next() {
var file;
//check there are more files to uglify
if (fileIndex >= files.length) {
return finished();
}
//get the file name
file = files[fileIndex++];
//get the file source code
fs.readFile(file, function(error, source) {
if (error) return finished(file, error);
//convert the buffer to a string
source = source.toString();
//check the file is not empty or only contains whitespace (uglify-js will throw an error)
if (source.replace(/\S/, '').length === 0) {
debug('Skipped empty file: ' + file);
//uglify the next script
return next();
}
//minify the code
var code;
var map;
var fileName = file.toString().split('\\')[1];
debug(file);
try {
code = uglify.minify(source, {sourceMapIncludeSources: true, outSourceMap: fileName + ".map", outFileName: fileName, fromString: true}).code;
map = uglify.minify(source, {sourceMapIncludeSources: true, outSourceMap: fileName + "", outFileName: fileName, fromString: true}).map;
} catch (error) {
return finished(file, error);
}
var json = {"sourceContent": source};
map = map.replace('"sources":["?"]', '"sources":["' + fileName + '"]');
map = map.replace('null', JSON.stringify(source));
//write the code back to the file
fs.writeFile(file, code, function(error) {
if (error) return finished(file, error);
debug('Uglified: ' + file);
fs.writeFile(file + ".map", map, function(error) {
//uglify next script
next();
});
});
});
}
//start processes
activeProcesses = maxProcesses;
for (var j=0; j<maxProcesses; ++j) {
next()
}
});
}
module.exports = recursiveUglifyJS;