From a0b46b09759f0ab41ec18f51d8daa71f04e0cdb1 Mon Sep 17 00:00:00 2001 From: Martin P Date: Fri, 1 Jan 2016 15:13:09 +0100 Subject: [PATCH] Fixed: Allow loading json using require() --- src/main/js/lib/require.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/main/js/lib/require.js b/src/main/js/lib/require.js index 4090e1d..bb4e696 100644 --- a/src/main/js/lib/require.js +++ b/src/main/js/lib/require.js @@ -160,23 +160,12 @@ When resolving module names to file paths, ScriptCraft uses the following rules. } } } else { - // it's of the form ./path - file = new File(parentDir, moduleName); - if ( file.exists() ) { + if ((file = new File(parentDir, moduleName)).exists()) { return fileExists(file); - } else { - // try appending a .js to the end - pathWithJSExt = file.canonicalPath + '.js'; - file = new File( parentDir, pathWithJSExt ); - if (file.exists()) { - return file; - } else { - file = new File(pathWithJSExt); - if ( file.exists() ) { - return file; - } - } - + } else if ((file = new File(parentDir, moduleName + ".js")).exists()) { // try .js extension + return file; + } else if ((file = new File(parentDir, moduleName + ".json")).exists()) { // try .json extension + return file; } } return null; @@ -250,6 +239,9 @@ When resolving module names to file paths, ScriptCraft uses the following rules. } buffered.close(); // close the stream so there's no file locks + if(canonizedFilename.toLowerCase().substring(canonizedFilename.length - 5) === ".json") // patch code when it is json + code = "module.exports = (" + code + ");"; + moduleInfo = { loaded: false, id: canonizedFilename,