From 3c715c5e0d1117ca32760d4da6578cd27af06172 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 23 Nov 2016 18:13:26 +0000 Subject: [PATCH] Extend require() to try appending index.js as well as .js Better Common/JS compatibility --- boot/boot.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index 260604f95..8783779f0 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -643,9 +643,25 @@ $tw.utils.Crypto = function() { Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot' */ $tw.modules.execute = function(moduleName,moduleRoot) { - var name = moduleName[0] === "." ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName, - moduleInfo = $tw.modules.titles[name] || $tw.modules.titles[name + ".js"] || $tw.modules.titles[moduleName] || $tw.modules.titles[moduleName + ".js"] , - tiddler = $tw.wiki.getTiddler(name) || $tw.wiki.getTiddler(name + ".js") || $tw.wiki.getTiddler(moduleName) || $tw.wiki.getTiddler(moduleName + ".js") , + var name = moduleName; + if(moduleName.charAt(0) === ".") { + name = $tw.utils.resolvePath(moduleName,moduleRoot) + } + if(!$tw.modules.titles[name]) { + if($tw.modules.titles[name + ".js"]) { + name = name + ".js"; + } else if($tw.modules.titles[name + "/index.js"]) { + name = name + "/index.js"; + } else if($tw.modules.titles[moduleName]) { + name = moduleName; + } else if($tw.modules.titles[moduleName + ".js"]) { + name = moduleName + ".js"; + } else if($tw.modules.titles[moduleName + "/index.js"]) { + name = moduleName + "/index.js"; + } + } + var moduleInfo = $tw.modules.titles[name], + tiddler = $tw.wiki.getTiddler(name), _exports = {}, sandbox = { module: {exports: _exports},