Skip to content

Commit c5a6c18

Browse files
committed
Require json files - close #294
1 parent e256534 commit c5a6c18

File tree

3 files changed

+79
-53
lines changed

3 files changed

+79
-53
lines changed

src/NativeScript/require.js

+78-52
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* You can ignore this file and continue debugging.
55
* To correct errors, edit the source file at: https://github.com/NativeScript/ios-runtime/blob/master/src/NativeScript/require.js
6-
*/
6+
*/
77

88
(function (applicationPath, createModuleFunction) {
99
'use strict';
@@ -32,6 +32,14 @@
3232
var pathCache = new Map();
3333
var modulesCache = new Map();
3434

35+
function getRelativeToBundlePath(absolutePath) {
36+
return absolutePath.substr(applicationPath.length).replace(/^\//, '');
37+
}
38+
39+
function getModuleCacheKey(moduleMetadata) {
40+
return moduleMetadata.bundlePath;
41+
}
42+
3543
function __findModule(moduleIdentifier, previousPath) {
3644
var isBootstrap = !previousPath;
3745
if (isBootstrap) {
@@ -55,83 +63,100 @@
5563
return pathCache.get(requestedPath);
5664
}
5765

58-
var absoluteFilePath = nsstr(absolutePath).stringByAppendingPathExtension("js");
59-
if (!fileManager.fileExistsAtPathIsDirectory(absoluteFilePath, isDirectory) &&
60-
fileManager.fileExistsAtPathIsDirectory(absolutePath, isDirectory)) {
61-
if (!isDirectory.value) {
62-
throw new ModuleError(`Expected '${absolutePath}' to be a directory`);
63-
}
64-
65-
var mainFileName;
66-
if (isBootstrap && fileManager.fileExistsAtPathIsDirectory(NSString.pathWithComponents([applicationPath, USER_MODULES_ROOT, 'bootstrap.js']), null)) {
67-
mainFileName = 'bootstrap.js';
68-
} else {
69-
mainFileName = 'index.js';
70-
}
66+
var moduleMetadata = {
67+
name: nsstr(moduleIdentifier).lastPathComponent,
68+
};
7169

72-
var packageJsonPath = nsstr(absolutePath).stringByAppendingPathComponent("package.json");
73-
var packageJson = NSString.stringWithContentsOfFileEncodingError(packageJsonPath, NSUTF8StringEncoding, null);
74-
if (packageJson) {
75-
//console.debug("PACKAGE_FOUND: " + packageJsonPath);
70+
var absoluteFilePath = nsstr(absolutePath).stringByAppendingPathExtension("js");
71+
if (/\.json$/.test(absolutePath)) {
72+
moduleMetadata.type = 'json';
73+
} else {
74+
if (!fileManager.fileExistsAtPathIsDirectory(absoluteFilePath, isDirectory) &&
75+
fileManager.fileExistsAtPathIsDirectory(absolutePath, isDirectory)) {
76+
if (!isDirectory.value) {
77+
throw new ModuleError(`Expected '${getRelativeToBundlePath(absolutePath)}' to be a directory.`);
78+
}
7679

77-
try {
78-
var packageJsonMain = JSON.parse(packageJson).main;
79-
if (packageJsonMain && !/\.js$/.test(packageJsonMain)) {
80-
packageJsonMain += '.js';
80+
var mainFileName = 'index.js';
81+
82+
var packageJsonPath = nsstr(absolutePath).stringByAppendingPathComponent("package.json");
83+
var packageJson = NSString.stringWithContentsOfFileEncodingError(packageJsonPath, NSUTF8StringEncoding, null);
84+
if (packageJson) {
85+
//console.debug("PACKAGE_FOUND: " + packageJsonPath);
86+
87+
try {
88+
var packageJsonMain = JSON.parse(packageJson).main;
89+
if (packageJsonMain && !/\.js$/.test(packageJsonMain)) {
90+
packageJsonMain += '.js';
91+
}
92+
mainFileName = packageJsonMain || mainFileName;
93+
} catch (e) {
94+
throw new ModuleError(`Error parsing package.json in '${absolutePath}' - ${e}`);
8195
}
82-
mainFileName = packageJsonMain || mainFileName;
83-
} catch (e) {
84-
throw new ModuleError(`Error parsing package.json in '${absolutePath}' - ${e}`);
8596
}
97+
98+
absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName);
99+
} else {
100+
absolutePath = absoluteFilePath;
86101
}
87102

88-
absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName);
89-
} else {
90-
absolutePath = absoluteFilePath;
103+
moduleMetadata.type = 'js';
91104
}
92105
absolutePath = nsstr(absolutePath).stringByStandardizingPath;
93-
var bundlePath = absolutePath.substr(applicationPath.length);
106+
var bundlePath = getRelativeToBundlePath(absolutePath);
94107

95108
if (!fileManager.fileExistsAtPathIsDirectory(absolutePath, isDirectory)) {
96-
throw new ModuleError(`Failed to find module '${moduleIdentifier}' relative to 'file://${previousPath}'. Computed path: '${bundlePath.replace(/^\//, '')}'.`);
109+
throw new ModuleError(`Failed to find module '${moduleIdentifier}' relative to 'file:///${previousPath}'. Computed path: '${bundlePath}'.`);
97110
}
98111

99112
if (isDirectory.value) {
100-
throw new ModuleError(`Expected '${absolutePath}' to be a file`);
113+
throw new ModuleError(`Expected '${bundlePath}' to be a file`);
101114
}
102115

103116
//console.debug('FIND_MODULE:', moduleIdentifier, absolutePath);
104117

105-
var moduleMetadata = {
106-
name: nsstr(moduleIdentifier).lastPathComponent,
107-
path: absolutePath,
108-
bundlePath
109-
};
118+
moduleMetadata.path = absolutePath;
119+
moduleMetadata.bundlePath = bundlePath;
110120

111121
pathCache.set(requestedPath, moduleMetadata);
112122
return moduleMetadata;
113123
}
114124

115125
function __executeModule(moduleMetadata, module) {
116126
var modulePath = moduleMetadata.bundlePath;
117-
module.require = function require(moduleIdentifier) {
118-
return __loadModule(moduleIdentifier, modulePath).exports;
119-
};
120-
module.require.displayName = "__require";
121127
var moduleSource = NSString.stringWithContentsOfFileEncodingError(moduleMetadata.path, NSUTF8StringEncoding, null);
122-
var moduleFunction = createModuleFunction(moduleSource, "file://" + moduleMetadata.bundlePath);
123-
var fileName = moduleMetadata.path;
124-
var dirName = nsstr(moduleMetadata.path).stringByDeletingLastPathComponent.toString();
125-
module.filename = fileName;
126128

127129
var hadError = true;
128-
try {
129-
moduleFunction(module.require, module, module.exports, dirName, fileName);
130-
hadError = false;
131-
} finally {
132-
if (hadError) {
133-
modulesCache.delete(moduleMetadata.bundlePath);
130+
131+
if (moduleMetadata.type === 'json') {
132+
try {
133+
module.exports = JSON.parse(moduleSource);
134+
hadError = false;
135+
} catch (e) {
136+
e.message = `File: 'file:///${moduleMetadata.bundlePath}'. ${e.message}`;
137+
throw e;
138+
} finally {
139+
if (hadError) {
140+
modulesCache.delete(getModuleCacheKey(moduleMetadata));
141+
}
134142
}
143+
} else if (moduleMetadata.type === 'js') {
144+
module.require = function __require(moduleIdentifier) {
145+
return __loadModule(moduleIdentifier, modulePath).exports;
146+
};
147+
var dirName = nsstr(moduleMetadata.path).stringByDeletingLastPathComponent.toString();
148+
149+
try {
150+
var moduleFunction = createModuleFunction(moduleSource, "file:///" + moduleMetadata.bundlePath);
151+
moduleFunction(module.require, module, module.exports, dirName, moduleMetadata.path);
152+
hadError = false;
153+
} finally {
154+
if (hadError) {
155+
modulesCache.delete(getModuleCacheKey(moduleMetadata));
156+
}
157+
}
158+
} else {
159+
throw new ModuleError(`Unknown module type '${moduleMetadata.type}'`);
135160
}
136161
}
137162

@@ -142,14 +167,15 @@
142167

143168
var moduleMetadata = __findModule(moduleIdentifier, previousPath);
144169

145-
var key = moduleMetadata.bundlePath;
170+
var key = getModuleCacheKey(moduleMetadata);
146171
if (modulesCache.has(key)) {
147172
return modulesCache.get(key);
148173
}
149174

150175
var module = {
151176
exports: {},
152-
id: moduleMetadata.bundlePath
177+
id: moduleMetadata.bundlePath,
178+
filename: moduleMetadata.path,
153179
};
154180

155181
modulesCache.set(key, module);
File renamed without changes.

0 commit comments

Comments
 (0)