Combine tar and isolate, so that everything is done in one definition.

This commit is contained in:
Chad Weider 2012-01-31 14:29:48 -08:00
parent ddc74cd0f1
commit 458b5a4f03
1 changed files with 25 additions and 21 deletions

View File

@ -278,37 +278,41 @@ function requireDefinition() {
} }
function tarCode(jsFiles, write, callback) { function tarCode(jsFiles, write, callback) {
async.forEach(jsFiles, function (item, callback){ write('require.define({');
if (item == 'ace.js') { var initialEntry = true;
async.forEach(jsFiles, function (filename, callback){
if (filename == 'ace.js') {
getAceFile(handleFile); getAceFile(handleFile);
} else { } else {
fs.readFile(JS_DIR + item, "utf8", handleFile); fs.readFile(JS_DIR + filename, "utf8", handleFile);
} }
function handleFile(err, data) { function handleFile(err, data) {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
data = ("\n\n\n/*** File: static/js/" + item + " ***/\n\n\n") + data; var srcPath = JSON.stringify('/' + filename);
if (settings.minify) { var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, ''));
write(compressJS([isolateJS(data, item)]) + ';\n'); if (!initialEntry) {
write('\n,');
} else { } else {
write(isolateJS(data, item)); initialEntry = false;
} }
write(srcPath + ': ')
data = '(function (require, exports, module) {' + data + '})';
if (settings.minify) {
write(compressJS([data]));
} else {
write(data);
}
if (srcPath != srcPathAbbv) {
write('\n,' + srcPathAbbv + ': null');
}
callback(); callback();
} }
}, callback); }, function () {
} write('});\n');
callback();
// Wrap the following code in a self executing function and assign exports to });
// global. This is a first step towards removing symbols from the global scope.
// exports is global and require is a function that returns global.
function isolateJS(code, filename) {
var srcPath = JSON.stringify('/' + filename);
var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, ''));
return 'require.define({'
+ srcPath + ': '
+ 'function (require, exports, module) {' + code + '}'
+ (srcPath != srcPathAbbv ? '\n,' + srcPathAbbv + ': null' : '')
+ '});\n';
} }
function compressJS(values) function compressJS(values)