fixed patched require() could not open local modules
Showing
1 changed file
with
45 additions
and
43 deletions
... | @@ -201,53 +201,55 @@ phantom.loadCasper = function() { | ... | @@ -201,53 +201,55 @@ phantom.loadCasper = function() { |
201 | }; | 201 | }; |
202 | if (phantomBuiltins.indexOf(path) !== -1) { | 202 | if (phantomBuiltins.indexOf(path) !== -1) { |
203 | return phantomRequire(path); | 203 | return phantomRequire(path); |
204 | } | ||
205 | if (path[0] === '.') { | ||
206 | paths.push.apply(paths, [ | ||
207 | fs.absolute(path), | ||
208 | fs.absolute(fs.pathJoin(requireDir, path)) | ||
209 | ]); | ||
210 | } else if (path[0] === '/') { | ||
211 | paths.push(path); | ||
204 | } else { | 212 | } else { |
205 | if (path[0] === '.') { | 213 | dir = fs.absolute(requireDir); |
206 | paths.push(fs.absolute(fs.pathJoin(requireDir, path))); | 214 | while (dir !== '') { |
207 | } else if (path[0] === '/') { | 215 | // nodejs compatibility |
208 | paths.push(path); | 216 | paths.push(fs.pathJoin(dir, 'node_modules', path)); |
209 | } else { | 217 | dir = fs.dirname(dir); |
210 | dir = fs.absolute(requireDir); | ||
211 | while (dir !== '') { | ||
212 | // nodejs compatibility | ||
213 | paths.push(fs.pathJoin(dir, 'node_modules', path)); | ||
214 | dir = fs.dirname(dir); | ||
215 | } | ||
216 | paths.push(fs.pathJoin(requireDir, 'lib', path)); | ||
217 | paths.push(fs.pathJoin(requireDir, 'modules', path)); | ||
218 | } | ||
219 | paths.forEach(function(testPath) { | ||
220 | fileGuesses.push.apply(fileGuesses, [ | ||
221 | testPath, | ||
222 | testPath + '.js', | ||
223 | testPath + '.coffee', | ||
224 | fs.pathJoin(testPath, 'index.js'), | ||
225 | fs.pathJoin(testPath, 'index.coffee'), | ||
226 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'), | ||
227 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee') | ||
228 | ]); | ||
229 | }); | ||
230 | file = null; | ||
231 | for (i = 0; i < fileGuesses.length && !file; ++i) { | ||
232 | if (fs.isFile(fileGuesses[i])) { | ||
233 | file = fileGuesses[i]; | ||
234 | } | ||
235 | } | ||
236 | if (!file) { | ||
237 | throw new Error("CasperJS couldn't find module " + path); | ||
238 | } | 218 | } |
239 | if (file in requireCache) { | 219 | paths.push(fs.pathJoin(requireDir, 'lib', path)); |
240 | return requireCache[file].exports; | 220 | paths.push(fs.pathJoin(requireDir, 'modules', path)); |
241 | } | 221 | } |
242 | try { | 222 | paths.forEach(function(testPath) { |
243 | var scriptCode = phantom.getScriptCode(file); | 223 | fileGuesses.push.apply(fileGuesses, [ |
244 | new Function('module', 'exports', scriptCode)(module, module.exports); | 224 | testPath, |
245 | } catch (e) { | 225 | testPath + '.js', |
246 | phantom.processScriptError(e, file); | 226 | testPath + '.coffee', |
227 | fs.pathJoin(testPath, 'index.js'), | ||
228 | fs.pathJoin(testPath, 'index.coffee'), | ||
229 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'), | ||
230 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee') | ||
231 | ]); | ||
232 | }); | ||
233 | file = null; | ||
234 | for (i = 0; i < fileGuesses.length && !file; ++i) { | ||
235 | if (fs.isFile(fileGuesses[i])) { | ||
236 | file = fileGuesses[i]; | ||
247 | } | 237 | } |
248 | requireCache[file] = module; | ||
249 | return module.exports; | ||
250 | } | 238 | } |
239 | if (!file) { | ||
240 | throw new Error("CasperJS couldn't find module " + path); | ||
241 | } | ||
242 | if (file in requireCache) { | ||
243 | return requireCache[file].exports; | ||
244 | } | ||
245 | try { | ||
246 | var scriptCode = phantom.getScriptCode(file); | ||
247 | new Function('module', 'exports', scriptCode)(module, module.exports); | ||
248 | } catch (e) { | ||
249 | phantom.processScriptError(e, file); | ||
250 | } | ||
251 | requireCache[file] = module; | ||
252 | return module.exports; | ||
251 | }; | 253 | }; |
252 | })(require, phantom.casperPath); | 254 | })(require, phantom.casperPath); |
253 | 255 | ... | ... |
-
Please register or sign in to post a comment