|
3 | 3 | *
|
4 | 4 | * You can ignore this file and continue debugging.
|
5 | 5 | * To correct errors, edit the source file at: https://github.com/NativeScript/ios-runtime/blob/master/src/NativeScript/require.js
|
6 |
| -*/ |
| 6 | + */ |
7 | 7 |
|
8 | 8 | (function (applicationPath, createModuleFunction) {
|
9 | 9 | 'use strict';
|
|
32 | 32 | var pathCache = new Map();
|
33 | 33 | var modulesCache = new Map();
|
34 | 34 |
|
| 35 | + function getRelativeToBundlePath(absolutePath) { |
| 36 | + return absolutePath.substr(applicationPath.length).replace(/^\//, ''); |
| 37 | + } |
| 38 | + |
| 39 | + function getModuleCacheKey(moduleMetadata) { |
| 40 | + return moduleMetadata.bundlePath; |
| 41 | + } |
| 42 | + |
35 | 43 | function __findModule(moduleIdentifier, previousPath) {
|
36 | 44 | var isBootstrap = !previousPath;
|
37 | 45 | if (isBootstrap) {
|
|
55 | 63 | return pathCache.get(requestedPath);
|
56 | 64 | }
|
57 | 65 |
|
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 | + }; |
71 | 69 |
|
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 | + } |
76 | 79 |
|
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}`); |
81 | 95 | }
|
82 |
| - mainFileName = packageJsonMain || mainFileName; |
83 |
| - } catch (e) { |
84 |
| - throw new ModuleError(`Error parsing package.json in '${absolutePath}' - ${e}`); |
85 | 96 | }
|
| 97 | + |
| 98 | + absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName); |
| 99 | + } else { |
| 100 | + absolutePath = absoluteFilePath; |
86 | 101 | }
|
87 | 102 |
|
88 |
| - absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName); |
89 |
| - } else { |
90 |
| - absolutePath = absoluteFilePath; |
| 103 | + moduleMetadata.type = 'js'; |
91 | 104 | }
|
92 | 105 | absolutePath = nsstr(absolutePath).stringByStandardizingPath;
|
93 |
| - var bundlePath = absolutePath.substr(applicationPath.length); |
| 106 | + var bundlePath = getRelativeToBundlePath(absolutePath); |
94 | 107 |
|
95 | 108 | 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}'.`); |
97 | 110 | }
|
98 | 111 |
|
99 | 112 | if (isDirectory.value) {
|
100 |
| - throw new ModuleError(`Expected '${absolutePath}' to be a file`); |
| 113 | + throw new ModuleError(`Expected '${bundlePath}' to be a file`); |
101 | 114 | }
|
102 | 115 |
|
103 | 116 | //console.debug('FIND_MODULE:', moduleIdentifier, absolutePath);
|
104 | 117 |
|
105 |
| - var moduleMetadata = { |
106 |
| - name: nsstr(moduleIdentifier).lastPathComponent, |
107 |
| - path: absolutePath, |
108 |
| - bundlePath |
109 |
| - }; |
| 118 | + moduleMetadata.path = absolutePath; |
| 119 | + moduleMetadata.bundlePath = bundlePath; |
110 | 120 |
|
111 | 121 | pathCache.set(requestedPath, moduleMetadata);
|
112 | 122 | return moduleMetadata;
|
113 | 123 | }
|
114 | 124 |
|
115 | 125 | function __executeModule(moduleMetadata, module) {
|
116 | 126 | var modulePath = moduleMetadata.bundlePath;
|
117 |
| - module.require = function require(moduleIdentifier) { |
118 |
| - return __loadModule(moduleIdentifier, modulePath).exports; |
119 |
| - }; |
120 |
| - module.require.displayName = "__require"; |
121 | 127 | 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; |
126 | 128 |
|
127 | 129 | 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 | + } |
134 | 142 | }
|
| 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}'`); |
135 | 160 | }
|
136 | 161 | }
|
137 | 162 |
|
|
142 | 167 |
|
143 | 168 | var moduleMetadata = __findModule(moduleIdentifier, previousPath);
|
144 | 169 |
|
145 |
| - var key = moduleMetadata.bundlePath; |
| 170 | + var key = getModuleCacheKey(moduleMetadata); |
146 | 171 | if (modulesCache.has(key)) {
|
147 | 172 | return modulesCache.get(key);
|
148 | 173 | }
|
149 | 174 |
|
150 | 175 | var module = {
|
151 | 176 | exports: {},
|
152 |
| - id: moduleMetadata.bundlePath |
| 177 | + id: moduleMetadata.bundlePath, |
| 178 | + filename: moduleMetadata.path, |
153 | 179 | };
|
154 | 180 |
|
155 | 181 | modulesCache.set(key, module);
|
|
0 commit comments