Commit 9b80a0e4 9b80a0e4c5b13f61ecb59d5e24fa6ffc93de20a9 by hexid

Allow usage of require.paths, while maintaining support for versions of PhantomJ…

…S that don't support it
1 parent 5c80139c
...@@ -352,22 +352,23 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -352,22 +352,23 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
352 }; 352 };
353 })(phantom.casperPath); 353 })(phantom.casperPath);
354 354
355 if ("slimer" in global) { 355 if ("paths" in global.require) {
356 // for SlimerJS, use the standard API to declare directories 356 // declare a dummy patchRequire function
357 // where to search modules 357 global.patchRequire = function(req) {return req;};
358
358 require.paths.push(fs.pathJoin(phantom.casperPath, 'modules')); 359 require.paths.push(fs.pathJoin(phantom.casperPath, 'modules'));
359 require.paths.push(fs.workingDirectory); 360 require.paths.push(fs.workingDirectory);
360 361 } else {
361 // declare a dummy patchRequire function
362 require.globals.patchRequire = global.patchRequire = function(req) { return req;};
363 require.globals.CasperError = CasperError;
364 phantom.casperEngine = "slimerjs";
365 }
366 else {
367 // patch require
368 global.__require = require; 362 global.__require = require;
369 global.patchRequire = patchRequire; // must be called in every casperjs module as of 1.1 363 global.patchRequire = patchRequire; // must be called in every casperjs module as of 1.1
370 global.require = patchRequire(global.require); 364 global.require = patchRequire(global.require);
365 }
366
367 if ("slimer" in global) {
368 require.globals.patchRequire = global.patchRequire;
369 require.globals.CasperError = CasperError;
370 phantom.casperEngine = "slimerjs";
371 } else {
371 phantom.casperEngine = "phantomjs"; 372 phantom.casperEngine = "phantomjs";
372 } 373 }
373 374
...@@ -378,11 +379,13 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -378,11 +379,13 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
378 initCasperCli(phantom.casperArgs); 379 initCasperCli(phantom.casperArgs);
379 } 380 }
380 381
381 if ("slimer" in global && phantom.casperScriptBaseDir) { 382 if ("paths" in global.require) {
382 // initCasperCli has set casperScriptBaseDir 383 if ((phantom.casperScriptBaseDir || "").indexOf(fs.workingDirectory) === 0) {
383 // use it instead of fs.workingDirectory 384 require.paths.push(phantom.casperScriptBaseDir);
384 require.paths.pop(); 385 } else {
385 require.paths.push(phantom.casperScriptBaseDir); 386 require.paths.push(fs.pathJoin(fs.workingDirectory, phantom.casperScriptBaseDir));
387 }
388 require.paths.push(fs.pathJoin(require.paths[require.paths.length-1], 'node_modules'));
386 } 389 }
387 390
388 // casper loading status flag 391 // casper loading status flag
......