Skip to content

Commit 7b309db

Browse files
committed
Allow loading json using require()
1 parent 4aa28c0 commit 7b309db

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/main/js/lib/require.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,12 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
165165
if ( file.exists() ) {
166166
return fileExists(file);
167167
} else {
168-
// try appending a .js to the end
169-
pathWithJSExt = file.canonicalPath + '.js';
170-
file = new File( parentDir, pathWithJSExt );
171-
if (file.exists()) {
172-
return file;
173-
} else {
174-
file = new File(pathWithJSExt);
175-
if ( file.exists() ) {
176-
return file;
177-
}
178-
}
179-
168+
if ((file = new File(parentDir, moduleName)).exists()) {
169+
return fileExists(file);
170+
} else if ((file = new File(parentDir, moduleName + ".js")).exists()) { // try .js extension
171+
return file;
172+
} else if ((file = new File(parentDir, moduleName + ".json")).exists()) { // try .json extension
173+
return file;
180174
}
181175
}
182176
return null;
@@ -250,6 +244,9 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
250244
}
251245
buffered.close(); // close the stream so there's no file locks
252246

247+
if(canonizedFilename.substring(canonizedFilename.length - 5) === ".json") // patch code when it is json
248+
code = "module.exports = (" + code + ");";
249+
253250
moduleInfo = {
254251
loaded: false,
255252
id: canonizedFilename,

0 commit comments

Comments
 (0)