From b81fdd9cede987d696e2c5edf8b5ff23c08359aa Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 5 May 2012 17:42:42 +0100 Subject: [PATCH] Tweaks to format of JavaScript modules Particularly, we're now picking the module fields out of the script tag, rather than from the comment at the top of the module. This will allow us to minify modules safely (otherwise we'd lose the metadata fields) --- rabbithole/core/boot.js | 13 +++++-- rabbithole/core/modules/serializers.js | 37 +++++++++++++++---- rabbithole/core/tiddlywiki5.template.html.tid | 8 +--- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/rabbithole/core/boot.js b/rabbithole/core/boot.js index 3b0023d96..fde327573 100644 --- a/rabbithole/core/boot.js +++ b/rabbithole/core/boot.js @@ -389,7 +389,7 @@ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","t match = headerCommentRegExp.exec(text); fields.text = text; if(match) { - fields = $tw.utils.parseFields(match[1],fields); + fields = $tw.utils.parseFields(match[1].split("\n\n")[0],fields); } return [fields]; } @@ -473,11 +473,16 @@ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/dom"," var text = node.innerHTML, s = text.indexOf("{"), e = text.lastIndexOf("}"); - if(s !== -1 && e !== -1) { + if(node.hasAttribute("data-module") && s !== -1 && e !== -1) { text = text.substring(s+1,e-1); } - var fields = $tw.wiki.deserializeTiddlers("application/javascript",text)[0]; - fields.title = node.getAttribute("data-tiddler-title"); + var fields = {text: text}, + attributes = node.attributes; + for(var a=0; a\n"); - result.push("$tw.modules.define(\"" + tiddler.fields.title + "\",\"" + tiddler.fields["module-type"] + "\",function(module,exports,require) {"); - result.push(tiddler.fields.text); - result.push("});\n"); - result.push(""); - return result.join(""); + var attributes = { + type: "text/javascript", + "data-module": "yes" + }, // The script type is set to text/javascript for compatibility with old browsers + text = tiddler.fields.text; + text = "$tw.modules.define(\"" + tiddler.fields.title + "\",\"" + tiddler.fields["module-type"] + "\",function(module,exports,require) {" + text + "});\n"; + for(var f in tiddler.fields) { + if(f !== "text") { + attributes["data-tiddler-" + f] = tiddler.getFieldString(f); + } + } + return $tw.Tree.Element( + "script", + attributes, + [$tw.Tree.Raw(text)] + ).render("text/html"); }; exports["application/x-tiddler-html-div"] = function(tiddler) { diff --git a/rabbithole/core/tiddlywiki5.template.html.tid b/rabbithole/core/tiddlywiki5.template.html.tid index ff81714f1..bad15ed3b 100644 --- a/rabbithole/core/tiddlywiki5.template.html.tid +++ b/rabbithole/core/tiddlywiki5.template.html.tid @@ -36,16 +36,12 @@ type: text/x-tiddlywiki-html <<^"[is[tiddler]]" application/x-tiddler-html-div>> - +<<^"$:/core/bootprefix.js" application/javascript>> - +<<^"$:/core/boot.js" application/javascript>>