Skip to content

Commit ad8614a

Browse files
committed
Merge pull request #191 from NativeScript/jasssonpet/require-cache
Cache require paths - close #139.
2 parents 0d44939 + d4a88bf commit ad8614a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/NativeScript/require.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
var isDirectory = new interop.Reference(interop.types.bool, false);
2222

23+
var pathCache = new Map();
2324
function __findModule(moduleIdentifier, previousPath) {
2425
var isBootstrap = !previousPath;
2526
if (isBootstrap) {
@@ -36,6 +37,11 @@
3637
absolutePath = NSString.pathWithComponents([applicationPath, moduleDir, moduleIdentifier]);
3738
}
3839

40+
var requestedPath = absolutePath;
41+
if (pathCache.has(requestedPath)) {
42+
return pathCache.get(requestedPath);
43+
}
44+
3945
if (fileManager.fileExistsAtPathIsDirectory(absolutePath, isDirectory)) {
4046
if (!isDirectory.value) {
4147
throw new ModuleError("Expected '" + absolutePath + "' to be a directory");
@@ -68,11 +74,14 @@
6874

6975
//console.debug('FIND_MODULE:', moduleIdentifier, absolutePath);
7076

71-
return {
77+
var moduleMetadata = {
7278
name: nsstr(moduleIdentifier).lastPathComponent,
7379
path: absolutePath,
7480
bundlePath: absolutePath.substr(applicationPath.length)
7581
};
82+
83+
pathCache.set(requestedPath, moduleMetadata);
84+
return moduleMetadata;
7685
} else {
7786
throw new ModuleError("Failed to find module '" + moduleIdentifier + "' relative to '" + previousPath + "'. Computed path: " + absolutePath);
7887
}

0 commit comments

Comments
 (0)