Improved browser module implementation

This commit is contained in:
Jeremy Ruston 2011-12-13 12:48:11 +00:00
parent 8ed8772b82
commit 5383368b51
6 changed files with 22 additions and 69 deletions

View File

@ -278,8 +278,10 @@ Recipe.tiddlerOutputter = {
for(var t=0; t<tiddlers.length; t++) {
var title = tiddlers[t],
tid = this.store.getTiddler(title);
out.push("<" + "script type=\"application/x-js-module\" title=\"" + title + "\">");
out.push("<" + "script type=\"application/javascript\">");
out.push("define(\"" + title + "\",function(require,exports) {");
out.push(tid.fields.text);
out.push("});")
out.push("</" + "script>");
}
}

View File

@ -3,44 +3,6 @@
/*jslint node: true */
"use strict";
/*
A hashmap containing hashmaps about each module:
{
module1: {
text: "<text of module1>",
executed: true
},
module2: {
text: "<text of module2>"
}
}
*/
var modules = {};
/*
The built in modules
*/
var builtInModules = {
util: {
},
path: {
},
fs: {
},
url: {
},
http: {
},
https: {
}
};
/*
Given the absolute path of a srcModule, and a relative reference to a dstModule, return the fully resolved module name
*/
@ -82,39 +44,16 @@ function executeModule(name) {
if(!module) {
throw new Error("Cannot find module named '" + name + "'");
}
if(module.executed) {
if(module.exports) {
return module.exports;
} else {
// This way of executing modules isn't perfect. Everything that is in scope here is available to the
// scripts, so this mechanism should only be used for trusted code
var script = "(function (require,exports){" + modules[name].text + "})(require,exports);"
eval(script);
module.executed = true;
module.module(require,exports);
module.exports = exports;
return exports;
}
}
function findModules(childNodes) {
// Iterate using the DOM directly; jQuery methods seem to bypass comment nodes
childNodes = childNodes || document.childNodes;
for(var t=0; t<childNodes.length; t++) {
var node = childNodes[t];
if(node.nodeName.toLowerCase() === "script" && node.type === "application/x-js-module") {
modules[node.title] = {text: node.textContent};
} else if(childNodes[t].childNodes.length > 0) {
findModules(childNodes[t].childNodes);
}
}
}
$(function() {
// Start with the embedded JavaScript modules
for(var t in builtInModules) {
modules[t] = builtInModules;
}
// Add any modules packed into script tags in the page
findModules();
// Execute the main module
executeModule("js/Main.js");
})

7
tiddlywiki5/BootStart.js Normal file
View File

@ -0,0 +1,7 @@
var modules = {};
var define = function(name,module) {
modules[name] = {module: module};
}
define("util",function(require,exports) {});

View File

@ -18,7 +18,8 @@ jsmodule: ../js/WikiTextRules.js
jsmodule: ../js/WikiTextRenderer.js
jsmodule: ../js/Main.js
jsboot: BootLoader.js
jsbootstart: BootStart.js
jsbootend: BootLoader.js
recipe: ../test/tiddlywiki.2.6.5/source/tiddlywiki.com/tiddlywiki-com-ref/split.recipe
recipe: ../test/tiddlywiki.2.6.5/source/tiddlywiki.com/tiddlywiki-com/split.recipe

View File

@ -24,9 +24,12 @@
<script id="jsLibArea" type="text/javascript">
<!--@@jslib@@-->
</script>
<script id="jsBootStartArea" type="text/javascript">
<!--@@jsbootstart@@-->
</script>
<!--@@jsmodule@@-->
<script id="jsBootArea" type="text/javascript">
<!--@@jsboot@@-->
<script id="jsBootEndArea" type="text/javascript">
<!--@@jsbootend@@-->
</script>
</body>
</html>

5
tw5.sh
View File

@ -4,9 +4,10 @@
# create a temporary directory if it doesn't already exist
mkdir -p tmp
mkdir -p tmp/tw5
# cook TiddlyWiki5
node tiddlywiki.js --recipe $PWD/tiddlywiki5/tiddlywiki5.recipe --savewiki tmp/tw5 || exit 1
# open the result
open -a /Applications/Google\ Chrome.app tmp/tw5/index.html
#open -a /Applications/Google\ Chrome.app tmp/tw5/index.html