Merge pull request #405 from cweider/remote-modules

Remote modules
pull/411/head
Peter 'Pita' Martischka 2012-02-06 03:57:20 -08:00
commit d7e29dba0d
6 changed files with 172 additions and 179 deletions

View File

@ -73,8 +73,7 @@ function _handle(req, res, jsFilename, jsFiles) {
//minifying is enabled
if(settings.minify)
{
var fileValues = {};
var embeds = {};
var result = undefined;
var latestModification = 0;
async.series([
@ -143,87 +142,20 @@ function _handle(req, res, jsFilename, jsFiles) {
//load all js files
function (callback)
{
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();
});
}, 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 isnt 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';
}
var values = [];
tarCode(
jsFiles
, function (content) {values.push(content)}
, function (err) {
if(ERR(err)) return;
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)
@ -271,54 +203,123 @@ 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();
});
}
}
// 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 = [];
}
founds.push('$$INCLUDE_JS("../static/js/require-kernel.js")');
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 isnt actually on the file system.
handleEmbed(null, requireDefinition());
} else {
var contents = '';
tarCode(tar[shortFilename] || shortFilename
, function (content) {
contents += content;
}
, function () {
handleEmbed(null, contents);
}
);
}
} 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';
}
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) {
write('require.define({');
var initialEntry = true;
async.forEach(jsFiles, function (filename, callback){
if (filename == 'ace.js') {
getAceFile(handleFile);
} else {
fs.readFile(JS_DIR + filename, "utf8", handleFile);
}
// 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 handleFile(err, data) {
if(ERR(err, callback)) return;
var srcPath = JSON.stringify('/' + filename);
var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, ''));
if (!initialEntry) {
write('\n,');
} else {
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();
}
}, function () {
write('});\n');
callback();
});
}
function compressJS(values)

View File

@ -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"
]
}

View File

@ -10,7 +10,7 @@
"name": "Robin Buse" }
],
"dependencies" : {
"require-kernel" : "1.0.0",
"require-kernel" : "1.0.1",
"socket.io" : "0.8.7",
"ueberDB" : "0.1.3",
"async" : "0.1.15",

View File

@ -217,40 +217,32 @@ 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';
var KERNEL_BOOT = 'require.setRootURI("../minified/");\nrequire.setGlobalKeyPath("require");'
if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[KERNEL_SOURCE]) {
buffer.push('<script type="text/javascript">');
buffer.push(Ace2Editor.EMBEDED[KERNEL_SOURCE]);
buffer.push(KERNEL_BOOT);
buffer.push('<\/script>');
} else {
buffer.push('<script type="application/javascript" src="'+KERNEL_SOURCE+'"><\/script>');
}
}
function pushScriptTagsFor(buffer, files) {
var sorted = sortFilesByEmbeded(files);
var embededFiles = sorted.embeded;
var remoteFiles = sorted.remote;
for (var i = 0, ii = remoteFiles.length; i < ii; i++) {
var file = remoteFiles[i];
function pushScriptsTo(buffer) {
/* Folling is for packaging regular expression. */
/* $$INCLUDE_JS("../static/js/ace2_inner.js"); */
var ACE_SOURCE = '../static/js/ace2_inner.js';
if (Ace2Editor.EMBEDED && Ace2Editor.EMBEDED[ACE_SOURCE]) {
buffer.push('<script type="text/javascript">');
buffer.push(Ace2Editor.EMBEDED[ACE_SOURCE]);
buffer.push('require("/ace2_inner");');
buffer.push('<\/script>');
} else {
file = ACE_SOURCE;
file = file.replace(/^\.\.\/static\/js\//, '../minified/');
buffer.push('<script type="application/javascript" src="' + file + '"><\/script>');
buffer.push('<script type="text/javascript">');
buffer.push('require("/ace2_inner");');
buffer.push('<\/script>');
}
buffer.push('<script type="text/javascript">');
for (var i = 0, ii = embededFiles.length; i < ii; i++) {
var file = embededFiles[i];
buffer.push(Ace2Editor.EMBEDED[file].replace(/<\//g, '<\\/'));
buffer.push(';\n');
}
for (var i = 0, ii = files.length; i < ii; i++) {
var file = files[i];
file = file.replace(/^\.\.\/static\/js\//, '');
buffer.push('require('+ JSON.stringify('/' + file) + ');\n');
}
buffer.push('<\/script>');
}
function pushStyleTagsFor(buffer, files) {
var sorted = sortFilesByEmbeded(files);
@ -324,18 +316,6 @@ function Ace2Editor()
var includedJS = [];
var $$INCLUDE_JS = function(filename) {includedJS.push(filename)};
$$INCLUDE_JS("../static/js/ace2_common.js");
$$INCLUDE_JS("../static/js/skiplist.js");
$$INCLUDE_JS("../static/js/virtual_lines.js");
$$INCLUDE_JS("../static/js/easysync2.js");
$$INCLUDE_JS("../static/js/cssmanager.js");
$$INCLUDE_JS("../static/js/colorutils.js");
$$INCLUDE_JS("../static/js/undomodule.js");
$$INCLUDE_JS("../static/js/contentcollector.js");
$$INCLUDE_JS("../static/js/changesettracker.js");
$$INCLUDE_JS("../static/js/linestylefilter.js");
$$INCLUDE_JS("../static/js/domline.js");
$$INCLUDE_JS("../static/js/ace2_inner.js");
pushRequireScriptTo(iframeHTML);
// Inject my plugins into my child.
iframeHTML.push('\
@ -346,7 +326,7 @@ function Ace2Editor()
});\
</script>\
');
pushScriptTagsFor(iframeHTML, includedJS);
pushScriptsTo(iframeHTML);
iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>');
iframeHTML.push('</head><body id="innerdocbody" class="syntax" spellcheck="false">&nbsp;</body></html>');

View File

@ -11,14 +11,6 @@
<link href="../static/custom/pad.css" rel="stylesheet">
<style title="dynamicsyntax"></style>
<script>
var clientVars = {};
</script>
<script src="../static/js/require-kernel.js"></script>
<script src="../socket.io/socket.io.js"></script>
<script src="../minified/pad.js"></script>
<script src="../static/custom/pad.js"></script>
<div id="editbar">
<ul id="menu_left">
<li onClick="window.pad&amp;&amp;pad.editbarClick('bold');return false" >
@ -296,15 +288,22 @@
</div>
<script>
/* TODO: These globals shouldn't exist. */
pad = require('/pad').pad;
chat = require('/chat').chat;
padeditbar = require('/pad_editbar').padeditbar;
padimpexp = require('/pad_impexp').padimpexp;
<script type="text/javascript" src="../static/js/require-kernel.js"></script>
<script type="text/javascript" src="../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../minified/pad.js"></script>
<script type="text/javascript" src="../static/custom/pad.js"></script>
<script type="text/javascript">
var clientVars = {};
(function () {
require.setRootURI("../minified/");
require.setGlobalKeyPath("require");
require('/pad').init();
/* TODO: These globals shouldn't exist. */
pad = require('/pad').pad;
chat = require('/chat').chat;
padeditbar = require('/pad_editbar').padeditbar;
padimpexp = require('/pad_impexp').padimpexp;
}());
</script>

View File

@ -7,14 +7,8 @@
<title>Etherpad Lite Timeslider</title>
<link rel="stylesheet" href="../../static/css/pad.css">
<link rel="stylesheet" href="../../static/css/timeslider.css">
<link rel="stylesheet" href="../../static/custom/timeslider.css">
<style type="text/css" title="dynamicsyntax"></style>
<script type="text/javascript" src="../../static/js/require-kernel.js"></script>
<script type="text/javascript" src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../../minified/timeslider.js"></script>
<link href="../../static/custom/timeslider.css" rel="stylesheet">
<script src="../../static/custom/timeslider.js"></script>
</head>
<body id="padbody" class="timeslider limwidth nonpropad nonprouser">
@ -203,15 +197,20 @@
</div>
</div>
<script>
<script type="text/javascript" src="../../static/js/require-kernel.js"></script>
<script type="text/javascript" src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../../minified/timeslider.js"></script>
<script type="text/javascript" src="../../static/custom/timeslider.js"></script>
<script type="text/javascript" >
var clientVars = {};
/* TODO: These globals shouldn't exist. */
padeditbar = require('/pad_editbar').padeditbar;
padimpexp = require('/pad_impexp').padimpexp;
(function () {
var TimeSlider = require('/timeslider').init();
require.setRootURI("../minified/");
require.setGlobalKeyPath("require");
require('/timeslider').init();
/* TODO: These globals shouldn't exist. */
padeditbar = require('/pad_editbar').padeditbar;
padimpexp = require('/pad_impexp').padimpexp;
})();
</script>
</body>