|
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 isJSONPath(path) { |
| 40 | + return /\.json$/.test(path); |
| 41 | + } |
| 42 | + |
| 43 | + function getModuleCacheKey(moduleMetadata) { |
| 44 | + return moduleMetadata.bundlePath; |
| 45 | + } |
| 46 | + |
35 | 47 | function __findModule(moduleIdentifier, previousPath) {
|
36 | 48 | var isBootstrap = !previousPath;
|
37 | 49 | if (isBootstrap) {
|
|
56 | 68 | }
|
57 | 69 |
|
58 | 70 | 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 | + } |
76 | 77 |
|
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}`); |
81 | 93 | }
|
82 |
| - mainFileName = packageJsonMain || mainFileName; |
83 |
| - } catch (e) { |
84 |
| - throw new ModuleError(`Error parsing package.json in '${absolutePath}' - ${e}`); |
85 | 94 | }
|
86 |
| - } |
87 | 95 |
|
88 |
| - absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName); |
89 |
| - } else { |
90 |
| - absolutePath = absoluteFilePath; |
| 96 | + absolutePath = nsstr(absolutePath).stringByAppendingPathComponent(mainFileName); |
| 97 | + } else { |
| 98 | + absolutePath = absoluteFilePath; |
| 99 | + } |
91 | 100 | }
|
92 | 101 | absolutePath = nsstr(absolutePath).stringByStandardizingPath;
|
93 |
| - var bundlePath = absolutePath.substr(applicationPath.length); |
| 102 | + var bundlePath = getRelativeToBundlePath(absolutePath); |
94 | 103 |
|
95 | 104 | 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}'.`); |
97 | 106 | }
|
98 | 107 |
|
99 | 108 | if (isDirectory.value) {
|
100 |
| - throw new ModuleError(`Expected '${absolutePath}' to be a file`); |
| 109 | + throw new ModuleError(`Expected '${bundlePath}' to be a file`); |
101 | 110 | }
|
102 | 111 |
|
103 | 112 | //console.debug('FIND_MODULE:', moduleIdentifier, absolutePath);
|
|
114 | 123 |
|
115 | 124 | function __executeModule(moduleMetadata, module) {
|
116 | 125 | var modulePath = moduleMetadata.bundlePath;
|
117 |
| - module.require = function require(moduleIdentifier) { |
118 |
| - return __loadModule(moduleIdentifier, modulePath).exports; |
119 |
| - }; |
120 |
| - module.require.displayName = "__require"; |
121 | 126 | 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 | 127 |
|
127 | 128 | 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 | + } |
134 | 156 | }
|
135 | 157 | }
|
136 | 158 | }
|
|
142 | 164 |
|
143 | 165 | var moduleMetadata = __findModule(moduleIdentifier, previousPath);
|
144 | 166 |
|
145 |
| - var key = moduleMetadata.bundlePath; |
| 167 | + var key = getModuleCacheKey(moduleMetadata); |
146 | 168 | if (modulesCache.has(key)) {
|
147 | 169 | return modulesCache.get(key);
|
148 | 170 | }
|
149 | 171 |
|
150 | 172 | var module = {
|
151 | 173 | exports: {},
|
152 |
| - id: moduleMetadata.bundlePath |
| 174 | + id: moduleMetadata.bundlePath, |
| 175 | + filename: moduleMetadata.path, |
153 | 176 | };
|
154 | 177 |
|
155 | 178 | modulesCache.set(key, module);
|
|
0 commit comments