Commit ff12c243 ff12c243ee66e674a8fccf534916142a004b0921 by Nicolas Perriault

Merge pull request #774 from hexid/phantomjs2-require-bc

Allow usage of require.paths
2 parents c55c2670 b2c7ada7
...@@ -76,6 +76,7 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -76,6 +76,7 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
76 76
77 // casperjs env initialization 77 // casperjs env initialization
78 (function(global, phantom){ 78 (function(global, phantom){
79 /*jshint maxstatements:99*/
79 "use strict"; 80 "use strict";
80 // phantom args 81 // phantom args
81 // NOTE: we can't use require('system').args here for some very obscure reason 82 // NOTE: we can't use require('system').args here for some very obscure reason
...@@ -352,22 +353,23 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -352,22 +353,23 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
352 }; 353 };
353 })(phantom.casperPath); 354 })(phantom.casperPath);
354 355
355 if ("slimer" in global) { 356 if ("paths" in global.require) {
356 // for SlimerJS, use the standard API to declare directories 357 // declare a dummy patchRequire function
357 // where to search modules 358 global.patchRequire = function(req) {return req;};
359
358 require.paths.push(fs.pathJoin(phantom.casperPath, 'modules')); 360 require.paths.push(fs.pathJoin(phantom.casperPath, 'modules'));
359 require.paths.push(fs.workingDirectory); 361 require.paths.push(fs.workingDirectory);
360 362 } 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; 363 global.__require = require;
369 global.patchRequire = patchRequire; // must be called in every casperjs module as of 1.1 364 global.patchRequire = patchRequire; // must be called in every casperjs module as of 1.1
370 global.require = patchRequire(global.require); 365 global.require = patchRequire(global.require);
366 }
367
368 if ("slimer" in global) {
369 require.globals.patchRequire = global.patchRequire;
370 require.globals.CasperError = CasperError;
371 phantom.casperEngine = "slimerjs";
372 } else {
371 phantom.casperEngine = "phantomjs"; 373 phantom.casperEngine = "phantomjs";
372 } 374 }
373 375
...@@ -378,11 +380,13 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -378,11 +380,13 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
378 initCasperCli(phantom.casperArgs); 380 initCasperCli(phantom.casperArgs);
379 } 381 }
380 382
381 if ("slimer" in global && phantom.casperScriptBaseDir) { 383 if ("paths" in global.require) {
382 // initCasperCli has set casperScriptBaseDir 384 if ((phantom.casperScriptBaseDir || "").indexOf(fs.workingDirectory) === 0) {
383 // use it instead of fs.workingDirectory 385 require.paths.push(phantom.casperScriptBaseDir);
384 require.paths.pop(); 386 } else {
385 require.paths.push(phantom.casperScriptBaseDir); 387 require.paths.push(fs.pathJoin(fs.workingDirectory, phantom.casperScriptBaseDir));
388 }
389 require.paths.push(fs.pathJoin(require.paths[require.paths.length-1], 'node_modules'));
386 } 390 }
387 391
388 // casper loading status flag 392 // casper loading status flag
......