Use require-kernel for require functionality.
This commit is contained in:
parent
71dfced06d
commit
62e0a8f26c
|
@ -114,7 +114,15 @@ async.waterfall([
|
|||
gracefulShutdown();
|
||||
});
|
||||
|
||||
//serve minified files
|
||||
app.get('/minified/:filename', minify.minifyJS);
|
||||
|
||||
//serve static files
|
||||
app.get('/static/js/require-kernel.js', function (req, res, next) {
|
||||
res.header("Content-Type","application/javascript; charset: utf-8");
|
||||
res.write(minify.requireDefinition());
|
||||
res.end();
|
||||
});
|
||||
app.get('/static/*', function(req, res)
|
||||
{
|
||||
var filePath = path.normalize(__dirname + "/.." +
|
||||
|
|
|
@ -29,6 +29,7 @@ var pro = require("uglify-js").uglify;
|
|||
var path = require('path');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
var gzip = require('gzip');
|
||||
var RequireKernel = require('require-kernel');
|
||||
var server = require('../server');
|
||||
var os = require('os');
|
||||
|
||||
|
@ -173,22 +174,31 @@ function _handle(req, res, jsFilename, jsFiles) {
|
|||
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
|
||||
|
||||
//read the included file
|
||||
fs.readFile(ROOT_DIR + filename, "utf-8", function(err, data)
|
||||
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")
|
||||
{
|
||||
var shortFilename = filename.replace(/^..\/static\/js\//, '');
|
||||
embeds[filename] = compressJS([isolateJS(data, shortFilename)]);
|
||||
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;
|
||||
|
@ -294,21 +304,35 @@ function _handle(req, res, jsFilename, jsFiles) {
|
|||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
for(var i = 0, ii = filesInOrder.length; i < filesInOrder.length; i++) {
|
||||
var filename = filesInOrder[i];
|
||||
write('require(' + JSON.stringify('/' + filename.replace(/^\/+/, '')) + ');\n');
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return '(function (exports, require) {'
|
||||
+ code + '\n'
|
||||
+ '}(function () {return this}(), (function (path) {return (function () {return this}())})));\n';
|
||||
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)
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"name": "Robin Buse" }
|
||||
],
|
||||
"dependencies" : {
|
||||
"require-kernel" : "1.0.0",
|
||||
"socket.io" : "0.8.7",
|
||||
"ueberDB" : "0.1.3",
|
||||
"async" : "0.1.15",
|
||||
|
|
|
@ -216,25 +216,41 @@ 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('<script type="text/javascript">');
|
||||
buffer.push(Ace2Editor.EMBEDED[KERNEL_SOURCE]);
|
||||
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;
|
||||
|
||||
if (embededFiles.length > 0) {
|
||||
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');
|
||||
}
|
||||
buffer.push('<\/script>');
|
||||
}
|
||||
for (var i = 0, ii = remoteFiles.length; i < ii; i++) {
|
||||
var file = remoteFiles[i];
|
||||
file = file.replace(/^\.\.\/static\/js\//, '../minified/');
|
||||
buffer.push('<script type="application/javascript" src="' + file + '"><\/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);
|
||||
|
@ -320,6 +336,7 @@ function Ace2Editor()
|
|||
$$INCLUDE_JS("../static/js/linestylefilter.js");
|
||||
$$INCLUDE_JS("../static/js/domline.js");
|
||||
$$INCLUDE_JS("../static/js/ace2_inner.js");
|
||||
pushRequireScriptTo(iframeHTML);
|
||||
pushScriptTagsFor(iframeHTML, includedJS);
|
||||
|
||||
iframeHTML.push('<style type="text/css" title="dynamicsyntax"></style>');
|
||||
|
|
|
@ -13,14 +13,12 @@
|
|||
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>
|
||||
<link href="../static/custom/pad.css" rel="stylesheet">
|
||||
<script src="../static/custom/pad.js"></script>
|
||||
<style type="text/css" title="dynamicsyntax"></style>
|
||||
<script type="application/javascript">
|
||||
var require = (function (path) {return (function () {return this}())});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<link rel="stylesheet" href="../../static/css/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>
|
||||
|
||||
|
@ -19,8 +20,6 @@
|
|||
// <![CDATA[
|
||||
var clientVars = {};
|
||||
|
||||
var require = (function (path) {return (function () {return this}())});
|
||||
|
||||
/* TODO: These globals shouldn't exist. */
|
||||
padeditbar = require('/pad_editbar').padeditbar;
|
||||
padimpexp = require('/pad_impexp').padimpexp;
|
||||
|
|
Loading…
Reference in New Issue