cleaner code, refactor of native phantomjs require() use
Showing
1 changed file
with
13 additions
and
29 deletions
... | @@ -69,30 +69,28 @@ if (typeof Function.prototype.bind !== "function") { | ... | @@ -69,30 +69,28 @@ if (typeof Function.prototype.bind !== "function") { |
69 | */ | 69 | */ |
70 | function patchRequire(require) { | 70 | function patchRequire(require) { |
71 | "use strict"; | 71 | "use strict"; |
72 | require = require || window.require; | ||
72 | if (require.patched) { | 73 | if (require.patched) { |
73 | return require; | 74 | return require; |
74 | } | 75 | } |
75 | var patchedRequire = function _require(path) { | 76 | var patchedRequire = function _require(path) { |
76 | var fs = require('fs'), moduleFilePath; | 77 | var fs = require('fs'); |
77 | if (phantom.casperBuiltIns.indexOf(path) === -1) { | 78 | var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js'); |
78 | return require(path); | 79 | if (!fs.exists(moduleFilePath)) { |
80 | return require(path); // native phantomjs' require() behavior | ||
79 | } | 81 | } |
80 | try { | 82 | try { |
81 | moduleFilePath = fs.pathJoin(phantom.casperModulesPath, path + '.js'); | ||
82 | return require(moduleFilePath); | 83 | return require(moduleFilePath); |
83 | } catch (e) { | 84 | } catch (e) { |
84 | if (moduleFilePath) { | 85 | var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); |
85 | var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); | 86 | error.file = moduleFilePath; |
86 | error.file = moduleFilePath; | 87 | error.line = e.line; |
87 | error.line = e.line; | 88 | error.stack = e.stack; |
88 | error.stack = e.stack; | 89 | error.stackArray = JSON.parse(JSON.stringify(e.stackArray)); |
89 | error.stackArray = JSON.parse(JSON.stringify(e.stackArray)); | 90 | if (error.stackArray.length > 0) { |
90 | if (error.stackArray.length > 0) { | 91 | error.stackArray[0].sourceURL = moduleFilePath; |
91 | error.stackArray[0].sourceURL = moduleFilePath; | ||
92 | } | ||
93 | throw error; | ||
94 | } | 92 | } |
95 | throw e; | 93 | throw error; |
96 | } | 94 | } |
97 | }; | 95 | }; |
98 | patchedRequire.cache = require.cache; | 96 | patchedRequire.cache = require.cache; |
... | @@ -179,20 +177,6 @@ function bootstrap(global) { | ... | @@ -179,20 +177,6 @@ function bootstrap(global) { |
179 | // standard Error prototype inheritance | 177 | // standard Error prototype inheritance |
180 | global.CasperError.prototype = Object.getPrototypeOf(new Error()); | 178 | global.CasperError.prototype = Object.getPrototypeOf(new Error()); |
181 | 179 | ||
182 | // path to standard casperjs modules directory | ||
183 | phantom.casperModulesPath = fs.pathJoin(phantom.casperPath, 'modules'); | ||
184 | |||
185 | // computing casperjs builtin modules list once for all | ||
186 | phantom.casperBuiltIns = (function getBuiltins() { | ||
187 | var fs = require('fs'); | ||
188 | return fs.list(phantom.casperModulesPath).filter(function(entry) { | ||
189 | var absPath = fs.absolute(fs.pathJoin(phantom.casperModulesPath, entry)); | ||
190 | return entry !== "." && entry !== ".." && !fs.isDirectory(absPath); | ||
191 | }).map(function(moduleFile) { | ||
192 | return moduleFile.replace(/\.js$/, ''); | ||
193 | }); | ||
194 | })(); | ||
195 | |||
196 | // CasperJS version, extracted from package.json - see http://semver.org/ | 180 | // CasperJS version, extracted from package.json - see http://semver.org/ |
197 | phantom.casperVersion = (function getVersion(path) { | 181 | phantom.casperVersion = (function getVersion(path) { |
198 | var parts, patchPart, pkg, pkgFile; | 182 | var parts, patchPart, pkg, pkgFile; | ... | ... |
-
Please register or sign in to post a comment