Skip to content

Commit c699b49

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

File tree

3 files changed

+71
-48
lines changed

3 files changed

+71
-48
lines changed

Diff for: src/NativeScript/require.js

+70-47
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
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 isJSONPath(path) {
40+
return /\.json$/.test(path);
41+
}
42+
43+
function getModuleCacheKey(moduleMetadata) {
44+
return moduleMetadata.bundlePath;
45+
}
46+
3547
function __findModule(moduleIdentifier, previousPath) {
3648
var isBootstrap = !previousPath;
3749
if (isBootstrap) {
@@ -56,48 +68,45 @@
5668
}
5769

5870
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-
}
71-
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);
71+
if (!isJSONPath(absolutePath)) {
72+
if (!fileManager.fileExistsAtPathIsDirectory(absoluteFilePath, isDirectory) &&
73+
fileManager.fileExistsAtPathIsDirectory(absolutePath, isDirectory)) {
74+
if (!isDirectory.value) {
75+
throw new ModuleError(`Expected '${getRelativeToBundlePath(absolutePath)}' to be a directory.`);
76+
}
7677

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

88-
absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName);
89-
} else {
90-
absolutePath = absoluteFilePath;
96+
absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName);
97+
} else {
98+
absolutePath = absoluteFilePath;
99+
}
91100
}
92101
absolutePath = nsstr(absolutePath).stringByStandardizingPath;
93-
var bundlePath = absolutePath.substr(applicationPath.length);
102+
var bundlePath = getRelativeToBundlePath(absolutePath);
94103

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

99108
if (isDirectory.value) {
100-
throw new ModuleError(`Expected '${absolutePath}' to be a file`);
109+
throw new ModuleError(`Expected '${bundlePath}' to be a file`);
101110
}
102111

103112
//console.debug('FIND_MODULE:', moduleIdentifier, absolutePath);
@@ -114,23 +123,36 @@
114123

115124
function __executeModule(moduleMetadata, module) {
116125
var modulePath = moduleMetadata.bundlePath;
117-
module.require = function require(moduleIdentifier) {
118-
return __loadModule(moduleIdentifier, modulePath).exports;
119-
};
120-
module.require.displayName = "__require";
121126
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;
126127

127128
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);
129+
130+
if (isJSONPath(moduleMetadata.path)) {
131+
try {
132+
module.exports = JSON.parse(moduleSource);
133+
hadError = false;
134+
} catch(e){
135+
e.message = `File: 'file:///${moduleMetadata.bundlePath}'. ${e.message}`;
136+
throw e;
137+
} finally {
138+
if (hadError) {
139+
modulesCache.delete(getModuleCacheKey(moduleMetadata));
140+
}
141+
}
142+
} else {
143+
module.require = function __require(moduleIdentifier) {
144+
return __loadModule(moduleIdentifier, modulePath).exports;
145+
};
146+
var dirName = nsstr(moduleMetadata.path).stringByDeletingLastPathComponent.toString();
147+
148+
try {
149+
var moduleFunction = createModuleFunction(moduleSource, "file://" + moduleMetadata.bundlePath);
150+
moduleFunction(module.require, module, module.exports, dirName, moduleMetadata.path);
151+
hadError = false;
152+
} finally {
153+
if (hadError) {
154+
modulesCache.delete(getModuleCacheKey(moduleMetadata));
155+
}
134156
}
135157
}
136158
}
@@ -142,14 +164,15 @@
142164

143165
var moduleMetadata = __findModule(moduleIdentifier, previousPath);
144166

145-
var key = moduleMetadata.bundlePath;
167+
var key = getModuleCacheKey(moduleMetadata);
146168
if (modulesCache.has(key)) {
147169
return modulesCache.get(key);
148170
}
149171

150172
var module = {
151173
exports: {},
152-
id: moduleMetadata.bundlePath
174+
id: moduleMetadata.bundlePath,
175+
filename: moduleMetadata.path,
153176
};
154177

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

0 commit comments

Comments
 (0)