| 1 |
|
| 2 |
define([ |
| 3 |
"../../buildControl", |
| 4 |
"../../fs", |
| 5 |
"./stripConsole", |
| 6 |
"dojo/_base/lang", |
| 7 |
"./uglify_worker", |
| 8 |
"../writeAmd", |
| 9 |
"require" |
| 10 |
], function(bc, fs, stripConsole, lang, uglify, writeAmd, require){ |
| 11 |
if(!uglify){ |
| 12 |
throw new Error("Unknown host environment: only nodejs is supported by uglify optimizer."); |
| 13 |
} |
| 14 |
|
| 15 |
if(bc.maxOptimizationProcesses){ |
| 16 |
var nodeReq = require.nodeRequire, |
| 17 |
processes = [], |
| 18 |
fork = nodeReq("child_process").fork, |
| 19 |
proc, jobs = {}, currentIndex = 0, queue = [], |
| 20 |
worker = require.toUrl("./uglify_worker.js"); |
| 21 |
if(bc.maxOptimizationProcesses<0){ |
| 22 |
bc.maxOptimizationProcesses = nodeReq('os').cpus().length; |
| 23 |
} |
| 24 |
for(var i = 0; i < bc.maxOptimizationProcesses; i++){ |
| 25 |
proc = fork(worker); |
| 26 |
proc.on("message", function(data){ |
| 27 |
if(jobs[data.dest]){ |
| 28 |
var func = jobs[data.dest]; |
| 29 |
delete jobs[data.dest]; |
| 30 |
func(data); |
| 31 |
} |
| 32 |
}); |
| 33 |
processes.push(proc); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return function(resource, text, copyright, optimizeSwitch, callback){ |
| 38 |
copyright = copyright || ""; |
| 39 |
|
| 40 |
var options = bc.optimizeOptions || {}; |
| 41 |
|
| 42 |
options.filename = writeAmd.getDestFilename(resource).split("/").pop(); |
| 43 |
|
| 44 |
if(optimizeSwitch.indexOf(".keeplines") > -1){ |
| 45 |
options.gen_options = options.gen_options || {}; |
| 46 |
options.gen_options.beautify = true; |
| 47 |
options.gen_options.indent_level = 0; |
| 48 |
} |
| 49 |
if(optimizeSwitch.indexOf(".comments") > -1){ |
| 50 |
throw new Error("'comments' option is not supported by uglify optimizer."); |
| 51 |
} |
| 52 |
|
| 53 |
var handleResult = function(data){ |
| 54 |
try{ |
| 55 |
if(data.error){ |
| 56 |
throw data.error; |
| 57 |
} |
| 58 |
var result = copyright + "//>>built" + bc.newline + data.text; |
| 59 |
|
| 60 |
fs.writeFile(resource.dest, result, resource.encoding, function(err){ |
| 61 |
if(err){ |
| 62 |
bc.log("optimizeFailedWrite", ["filename", resource.dest]); |
| 63 |
} |
| 64 |
callback(resource, err); |
| 65 |
}); |
| 66 |
}catch(e){ |
| 67 |
bc.log("optimizeFailed", ["module identifier", resource.mid, "exception", e + ""]); |
| 68 |
callback(resource, 0); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
if(bc.maxOptimizationProcesses){ |
| 73 |
jobs[resource.dest] = handleResult; |
| 74 |
processes[currentIndex].send({ |
| 75 |
text: stripConsole(text), |
| 76 |
options: options, |
| 77 |
dest: resource.dest, |
| 78 |
useSourceMaps: bc.useSourceMaps |
| 79 |
}); |
| 80 |
currentIndex = (currentIndex+1) % processes.length; |
| 81 |
} else { |
| 82 |
process.nextTick(function(){ |
| 83 |
var o = {}; |
| 84 |
try{ |
| 85 |
o.text = uglify(stripConsole(text), options, resource.dest, bc.useSourceMaps); |
| 86 |
}catch(e){ |
| 87 |
o.error = e; |
| 88 |
} |
| 89 |
handleResult(o); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
return callback; |
| 94 |
}; |
| 95 |
}); |