From e68413d9986f099ff855c9790d1eda2412e169ff Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sun, 22 Jan 2012 18:29:00 -0800 Subject: [PATCH 01/10] Extract Ace.js file generation. --- node/utils/Minify.js | 135 ++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index 98774d19..44ae88c0 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -74,7 +74,6 @@ function _handle(req, res, jsFilename, jsFiles) { if(settings.minify) { var fileValues = {}; - var embeds = {}; var latestModification = 0; async.series([ @@ -145,77 +144,20 @@ function _handle(req, res, jsFilename, jsFiles) { { async.forEach(jsFiles, function (item, callback) { - fs.readFile(JS_DIR + item, "utf-8", function(err, data) - { + if (item == 'ace.js') { + getAceFile(handleFile); + } else { + fs.readFile(JS_DIR + item, "utf-8", handleFile); + } + + function handleFile(err, data) + { if(ERR(err, callback)) return; fileValues[item] = data; callback(); - }); + } }, callback); }, - //find all includes in ace.js and embed them - function(callback) - { - //if this is not the creation of pad.js, skip this part - if(jsFilename != "pad.js") - { - callback(); - return; - } - - var founds = fileValues["ace.js"].match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"-]+\)/gi); - - //go trough all includes - async.forEach(founds, function (item, callback) - { - var filename = item.match(/"[^"]*"/g)[0].substr(1); - filename = filename.substr(0,filename.length-1); - - var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length); - - //read the included file - var shortFilename = filename.replace(/^..\/static\/js\//, ''); - if (shortFilename == 'require-kernel.js') { - // the kernel isn’t actually on the file system. - handleEmbed(null, requireDefinition()); - } else { - fs.readFile(ROOT_DIR + filename, "utf-8", handleEmbed); - } - function handleEmbed(err, data) - { - if(ERR(err, callback)) return; - - if(type == "JS") - { - if (shortFilename == 'require-kernel.js') { - embeds[filename] = compressJS([data]); - } else { - embeds[filename] = compressJS([isolateJS(data, shortFilename)]); - } - } - else - { - embeds[filename] = compressCSS([data]); - } - callback(); - } - }, function(err) - { - if(ERR(err, callback)) return; - - fileValues["ace.js"] += ';\n' - fileValues["ace.js"] += - 'Ace2Editor.EMBEDED = Ace2Editor.EMBED || {};\n' - for (var filename in embeds) - { - fileValues["ace.js"] += - 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = ' - + JSON.stringify(embeds[filename]) + ';\n'; - } - - callback(); - }); - }, //put all together and write it into a file function(callback) { @@ -295,6 +237,65 @@ function _handle(req, res, jsFilename, jsFiles) { } } +// find all includes in ace.js and embed them. +function getAceFile(callback) { + fs.readFile(JS_DIR + 'ace.js', "utf8", function(err, data) { + if(ERR(err, callback)) return; + + // Find all includes in ace.js and embed them + var founds = data.match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"-]+\)/gi); + if (!settings.minify) { + founds = []; + } + + data += ';\n'; + data += 'Ace2Editor.EMBEDED = Ace2Editor.EMBEDED || {};\n'; + + //go trough all includes + async.forEach(founds, function (item, callback) { + var filename = item.match(/"([^"]*)"/)[1]; + var type = item.match(/INCLUDE_([A-Z]+)/)[1]; + var shortFilename = (filename.match(/^..\/static\/js\/(.*)$/, '')||[])[1]; + + //read the included files + if (shortFilename) { + if (shortFilename == 'require-kernel.js') { + // the kernel isn’t actually on the file system. + handleEmbed(null, requireDefinition()); + } else { + fs.readFile(ROOT_DIR + filename, "utf8", function (error, data) { + handleEmbed(error, isolateJS(data, shortFilename)); + }); + } + } else { + fs.readFile(ROOT_DIR + filename, "utf8", handleEmbed); + } + + function handleEmbed(error, data_) { + if (error) { + return; // Don't bother to include it. + } + if (settings.minify) { + if (type == "JS") { + try { + data_ = compressJS([data_]); + } catch (e) { + // Ignore, include uncompresseed, which will break in browser. + } + } else { + data_ = compressCSS([data_]); + } + } + data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = ' + + JSON.stringify(data_) + ';\n'; + callback(); + } + }, function(error) { + callback(error, data); + }); + }); +} + exports.requireDefinition = requireDefinition; function requireDefinition() { return 'var require = ' + RequireKernel.kernelSource + ';\n'; From ebb5055ce81bd3d279100a5af230728687f92a10 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Sat, 28 Jan 2012 16:24:11 -0800 Subject: [PATCH 02/10] Extract file retrieval. --- node/utils/Minify.js | 77 +++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index 44ae88c0..5deaa133 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -73,7 +73,7 @@ function _handle(req, res, jsFilename, jsFiles) { //minifying is enabled if(settings.minify) { - var fileValues = {}; + var result = undefined; var latestModification = 0; async.series([ @@ -142,30 +142,20 @@ function _handle(req, res, jsFilename, jsFiles) { //load all js files function (callback) { - async.forEach(jsFiles, function (item, callback) - { - if (item == 'ace.js') { - getAceFile(handleFile); - } else { - fs.readFile(JS_DIR + item, "utf-8", handleFile); - } + var values = []; + tarCode( + jsFiles + , function (content) {values.push(content)} + , function (err) { + if(ERR(err)) return; - function handleFile(err, data) - { - if(ERR(err, callback)) return; - fileValues[item] = data; - callback(); - } - }, callback); + result = values.join(''); + callback(); + }); }, //put all together and write it into a file function(callback) { - //minify all javascript files to one - var values = []; - tarCode(jsFiles, fileValues, function (content) {values.push(content)}); - var result = compressJS(values); - async.parallel([ //write the results plain in a file function(callback) @@ -213,25 +203,11 @@ function _handle(req, res, jsFilename, jsFiles) { //minifying is disabled, so put the files together in one file else { - var fileValues = {}; - - //read all js files - async.forEach(jsFiles, function (item, callback) - { - fs.readFile(JS_DIR + item, "utf-8", function(err, data) - { - if(ERR(err, callback)) return; - fileValues[item] = data; - callback(); - }); - }, - //send all files together - function(err) - { + tarCode( + jsFiles + , function (content) {res.write(content)} + , function (err) { if(ERR(err)) return; - - tarCode(jsFiles, fileValues, function (content) {res.write(content)}); - res.end(); }); } @@ -301,12 +277,25 @@ function requireDefinition() { return 'var require = ' + RequireKernel.kernelSource + ';\n'; } -function tarCode(filesInOrder, files, write) { - for(var i = 0, ii = filesInOrder.length; i < filesInOrder.length; i++) { - var filename = filesInOrder[i]; - write("\n\n\n/*** File: static/js/" + filename + " ***/\n\n\n"); - write(isolateJS(files[filename], filename)); - } +function tarCode(jsFiles, write, callback) { + async.forEach(jsFiles, function (item, callback){ + if (item == 'ace.js') { + getAceFile(handleFile); + } else { + fs.readFile(JS_DIR + item, "utf8", handleFile); + } + + function handleFile(err, data) { + if(ERR(err, callback)) return; + write("\n\n\n/*** File: static/js/" + item + " ***/\n\n\n"); + if (settings.minify) { + write(compressJS([isolateJS(data, item)]) + ';\n'); + } else { + write(isolateJS(data, item)); + } + callback(); + } + }, callback); } // Wrap the following code in a self executing function and assign exports to From ddc74cd0f13afd99d1276a942d0afdd221dba4d9 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Tue, 31 Jan 2012 14:03:10 -0800 Subject: [PATCH 03/10] Make the comment a part of the module. --- node/utils/Minify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index 5deaa133..e025e927 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -287,7 +287,7 @@ function tarCode(jsFiles, write, callback) { function handleFile(err, data) { if(ERR(err, callback)) return; - write("\n\n\n/*** File: static/js/" + item + " ***/\n\n\n"); + data = ("\n\n\n/*** File: static/js/" + item + " ***/\n\n\n") + data; if (settings.minify) { write(compressJS([isolateJS(data, item)]) + ';\n'); } else { From 458b5a4f03b081a8abac68f04999f65c4cf442d3 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Tue, 31 Jan 2012 14:29:48 -0800 Subject: [PATCH 04/10] Combine tar and isolate, so that everything is done in one definition. --- node/utils/Minify.js | 46 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index e025e927..b2c98d52 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -278,37 +278,41 @@ function requireDefinition() { } function tarCode(jsFiles, write, callback) { - async.forEach(jsFiles, function (item, callback){ - if (item == 'ace.js') { + write('require.define({'); + var initialEntry = true; + async.forEach(jsFiles, function (filename, callback){ + if (filename == 'ace.js') { getAceFile(handleFile); } else { - fs.readFile(JS_DIR + item, "utf8", handleFile); + fs.readFile(JS_DIR + filename, "utf8", handleFile); } function handleFile(err, data) { if(ERR(err, callback)) return; - data = ("\n\n\n/*** File: static/js/" + item + " ***/\n\n\n") + data; - if (settings.minify) { - write(compressJS([isolateJS(data, item)]) + ';\n'); + var srcPath = JSON.stringify('/' + filename); + var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, '')); + if (!initialEntry) { + write('\n,'); } 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); -} - -// 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 () { + write('});\n'); + callback(); + }); } function compressJS(values) From 348e7ef1d5e1eb38ab76523cbd6e5fdc1b36adca Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Tue, 31 Jan 2012 00:12:10 -0800 Subject: [PATCH 05/10] Define packaging for iframe JS in the same way as pad.js and timeslider.js. --- node/utils/Minify.js | 12 ++++++++--- node/utils/tar.json | 14 +++++++++++++ static/js/ace.js | 48 ++++++++++++++------------------------------ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index b2c98d52..c078c5c6 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -239,9 +239,15 @@ function getAceFile(callback) { // the kernel isn’t actually on the file system. handleEmbed(null, requireDefinition()); } else { - fs.readFile(ROOT_DIR + filename, "utf8", function (error, data) { - handleEmbed(error, isolateJS(data, shortFilename)); - }); + var contents = ''; + tarCode(tar[shortFilename] || shortFilename + , function (content) { + contents += content; + } + , function () { + handleEmbed(null, contents); + } + ); } } else { fs.readFile(ROOT_DIR + filename, "utf8", handleEmbed); diff --git a/node/utils/tar.json b/node/utils/tar.json index aeafe23f..b319791c 100644 --- a/node/utils/tar.json +++ b/node/utils/tar.json @@ -48,4 +48,18 @@ , "broadcast_revisions.js" , "timeslider.js" ] +, "ace2_inner.js": [ + "ace2_common.js" + , "skiplist.js" + , "virtual_lines.js" + , "easysync2.js" + , "cssmanager.js" + , "colorutils.js" + , "undomodule.js" + , "contentcollector.js" + , "changesettracker.js" + , "linestylefilter.js" + , "domline.js" + , "ace2_inner.js" + ] } diff --git a/static/js/ace.js b/static/js/ace.js index 4cad6b4f..4a313727 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -228,29 +228,23 @@ function Ace2Editor() buffer.push('\ '); - pushScriptTagsFor(iframeHTML, includedJS); + pushScriptsTo(iframeHTML); iframeHTML.push(''); iframeHTML.push(' '); From 4bf9b0c8054504b9560cb0b24fcf061f6fa05d6d Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Mon, 30 Jan 2012 21:37:39 -0800 Subject: [PATCH 06/10] Always include the require kernel. --- node/utils/Minify.js | 1 + static/js/ace.js | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index c078c5c6..859ee07a 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -223,6 +223,7 @@ function getAceFile(callback) { if (!settings.minify) { founds = []; } + founds.push('$$INCLUDE_JS("../static/js/require-kernel.js")'); data += ';\n'; data += 'Ace2Editor.EMBEDED = Ace2Editor.EMBEDED || {};\n'; diff --git a/static/js/ace.js b/static/js/ace.js index 4a313727..3fdfa05e 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -217,15 +217,11 @@ function Ace2Editor() return {embeded: embededFiles, remote: remoteFiles}; } function pushRequireScriptTo(buffer) { - /* Folling is for packaging regular expression. */ - /* $$INCLUDE_JS("../static/js/require-kernel.js"); */ var KERNEL_SOURCE = '../static/js/require-kernel.js'; if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[KERNEL_SOURCE]) { buffer.push(' @@ -297,14 +294,15 @@ diff --git a/static/timeslider.html b/static/timeslider.html index cbcb3e36..fc8e2d4e 100644 --- a/static/timeslider.html +++ b/static/timeslider.html @@ -205,13 +205,12 @@ From 762d39f009859f276d7683ad1113fc7032319160 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Tue, 31 Jan 2012 22:32:46 -0800 Subject: [PATCH 08/10] Move scripts to the bottom of the page. --- static/pad.html | 11 +++++------ static/timeslider.html | 14 ++++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/static/pad.html b/static/pad.html index b8d48028..48892e0f 100644 --- a/static/pad.html +++ b/static/pad.html @@ -11,11 +11,6 @@ - - - - -
- + + + + - - - - - @@ -203,7 +197,11 @@ - + + + +