now relying on phantomjs native require()
Showing
11 changed files
with
48 additions
and
101 deletions
... | @@ -69,106 +69,40 @@ if (typeof Function.prototype.bind !== "function") { | ... | @@ -69,106 +69,40 @@ if (typeof Function.prototype.bind !== "function") { |
69 | */ | 69 | */ |
70 | function patchRequire(require, requireDirs) { | 70 | function patchRequire(require, requireDirs) { |
71 | "use strict"; | 71 | "use strict"; |
72 | require('webserver'); // force generation of phantomjs' require.cache for the webserver module | 72 | if (require.patched) { |
73 | var fs = require('fs'); | 73 | return require; |
74 | var phantomBuiltins = ['fs', 'webpage', 'system', 'webserver']; | ||
75 | var phantomRequire = phantom.__orig__require = require; | ||
76 | var requireCache = {}; | ||
77 | function possiblePaths(path, requireDir) { | ||
78 | var dir, paths = []; | ||
79 | if (path[0] === '.') { | ||
80 | paths.push.apply(paths, [ | ||
81 | fs.absolute(path), | ||
82 | fs.absolute(fs.pathJoin(requireDir, path)) | ||
83 | ]); | ||
84 | } else if (path[0] === '/') { | ||
85 | paths.push(path); | ||
86 | } else { | ||
87 | dir = fs.absolute(requireDir); | ||
88 | while (dir !== '' && dir.lastIndexOf(':') !== dir.length - 1) { | ||
89 | paths.push(fs.pathJoin(dir, 'modules', path)); | ||
90 | // nodejs compatibility | ||
91 | paths.push(fs.pathJoin(dir, 'node_modules', path)); | ||
92 | dir = fs.dirname(dir); | ||
93 | } | ||
94 | paths.push(fs.pathJoin(requireDir, 'lib', path)); | ||
95 | paths.push(fs.pathJoin(requireDir, 'modules', path)); | ||
96 | } | ||
97 | return paths; | ||
98 | } | 74 | } |
99 | var patchedRequire = function _require(path) { | 75 | var patchedRequire = function _require(path) { |
100 | var i, paths = [], | 76 | var fs = require('fs'), moduleFilePath; |
101 | fileGuesses = [], | 77 | var modulesPath = fs.pathJoin(phantom.casperPath, 'modules'); |
102 | file, | 78 | var casperModules = fs.list(modulesPath).filter(function(entry) { |
103 | module = { | 79 | var absPath = fs.absolute(fs.pathJoin(modulesPath, entry)); |
104 | exports: {} | 80 | return entry !== "." && entry !== ".." && !fs.isDirectory(absPath); |
105 | }; | 81 | }).map(function(moduleFile) { |
106 | if (phantomBuiltins.indexOf(path) !== -1) { | 82 | return moduleFile.replace(/\.js$/, ''); |
107 | return phantomRequire(path); | ||
108 | } | ||
109 | requireDirs.forEach(function(requireDir) { | ||
110 | paths = paths.concat(possiblePaths(path, requireDir)); | ||
111 | }); | 83 | }); |
112 | paths.forEach(function _forEach(testPath) { | ||
113 | fileGuesses.push.apply(fileGuesses, [ | ||
114 | testPath, | ||
115 | testPath + '.js', | ||
116 | testPath + '.json', | ||
117 | testPath + '.coffee', | ||
118 | fs.pathJoin(testPath, 'index.js'), | ||
119 | fs.pathJoin(testPath, 'index.json'), | ||
120 | fs.pathJoin(testPath, 'index.coffee'), | ||
121 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'), | ||
122 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.json'), | ||
123 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee') | ||
124 | ]); | ||
125 | }); | ||
126 | file = null; | ||
127 | for (i = 0; i < fileGuesses.length && !file; ++i) { | ||
128 | if (fs.isFile(fileGuesses[i])) { | ||
129 | file = fileGuesses[i]; | ||
130 | } | ||
131 | } | ||
132 | if (!file) { | ||
133 | throw new window.CasperError("CasperJS couldn't find module " + path); | ||
134 | } | ||
135 | if (file in requireCache) { | ||
136 | return requireCache[file].exports; | ||
137 | } | ||
138 | if (/\.json/i.test(file)) { | ||
139 | var parsed = JSON.parse(fs.read(file)); | ||
140 | requireCache[file] = parsed; | ||
141 | return parsed; | ||
142 | } | ||
143 | var scriptCode = (function getScriptCode(file) { | ||
144 | var scriptCode = fs.read(file); | ||
145 | if (/\.coffee$/i.test(file)) { | ||
146 | /*global CoffeeScript*/ | ||
147 | try { | ||
148 | scriptCode = CoffeeScript.compile(scriptCode); | ||
149 | } catch (e) { | ||
150 | throw new Error('Unable to compile coffeescript:' + e); | ||
151 | } | ||
152 | } | ||
153 | return scriptCode; | ||
154 | })(file); | ||
155 | var fn = new Function('__file__', 'require', 'module', 'exports', scriptCode); | ||
156 | try { | 84 | try { |
157 | fn(file, _require, module, module.exports); | 85 | if (casperModules.indexOf(path) > -1) { |
86 | moduleFilePath = fs.pathJoin(modulesPath, path + '.js'); | ||
87 | return require(moduleFilePath); | ||
88 | } | ||
89 | return require(path); | ||
158 | } catch (e) { | 90 | } catch (e) { |
159 | var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); | 91 | if (moduleFilePath) { |
160 | error.file = file; | 92 | var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); |
161 | error.line = e.line; | 93 | error.file = moduleFilePath; |
162 | error.stack = e.stack; | 94 | error.line = e.line; |
163 | error.stackArray = JSON.parse(JSON.stringify(e.stackArray)); | 95 | error.stack = e.stack; |
164 | if (error.stackArray.length > 0) { | 96 | error.stackArray = JSON.parse(JSON.stringify(e.stackArray)); |
165 | error.stackArray[0].sourceURL = file; | 97 | if (error.stackArray.length > 0) { |
98 | error.stackArray[0].sourceURL = moduleFilePath; | ||
99 | } | ||
100 | throw error; | ||
166 | } | 101 | } |
167 | throw error; | 102 | throw e; |
168 | } | 103 | } |
169 | requireCache[file] = module; | ||
170 | return module.exports; | ||
171 | }; | 104 | }; |
105 | patchedRequire.cache = require.cache; | ||
172 | patchedRequire.patched = true; | 106 | patchedRequire.patched = true; |
173 | return patchedRequire; | 107 | return patchedRequire; |
174 | } | 108 | } | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError console exports phantom require __utils__*/ | 31 | /*global CasperError console exports phantom __utils__ patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var colorizer = require('colorizer'); | 34 | var colorizer = require('colorizer'); |
34 | var events = require('events'); | 35 | var events = require('events'); |
35 | var fs = require('fs'); | 36 | var fs = require('fs'); | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError console exports phantom require*/ | 31 | /*global CasperError console exports phantom patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var system = require('system'); | 34 | var system = require('system'); |
34 | var utils = require('utils'); | 35 | var utils = require('utils'); |
35 | 36 | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global exports console require*/ | 31 | /*global exports console patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var fs = require('fs'); | 34 | var fs = require('fs'); |
34 | var utils = require('utils'); | 35 | var utils = require('utils'); |
35 | 36 | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError exports require*/ | 31 | /*global CasperError exports patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var utils = require('utils'); | 34 | var utils = require('utils'); |
34 | 35 | ||
35 | exports.create = function create(casper) { | 36 | exports.create = function create(casper) { | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError console exports phantom require*/ | 31 | /*global CasperError console exports phantom patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var utils = require('utils'); | 34 | var utils = require('utils'); |
34 | var f = utils.format; | 35 | var f = utils.format; |
35 | 36 | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError exports phantom require __utils__*/ | 31 | /*global CasperError exports phantom __utils__ patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var fs = require('fs'); | 34 | var fs = require('fs'); |
34 | var events = require('events'); | 35 | var events = require('events'); |
35 | var utils = require('utils'); | 36 | var utils = require('utils'); | ... | ... |
... | @@ -28,7 +28,9 @@ | ... | @@ -28,7 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError console exports phantom require*/ | 31 | /*global CasperError console exports phantom patchRequire*/ |
32 | |||
33 | var require = patchRequire(require); | ||
32 | 34 | ||
33 | /** | 35 | /** |
34 | * Provides a better typeof operator equivalent, able to retrieve the array | 36 | * Provides a better typeof operator equivalent, able to retrieve the array | ... | ... |
... | @@ -28,8 +28,9 @@ | ... | @@ -28,8 +28,9 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global CasperError console exports phantom require*/ | 31 | /*global CasperError console exports phantom patchRequire*/ |
32 | 32 | ||
33 | var require = patchRequire(require); | ||
33 | var utils = require('utils'); | 34 | var utils = require('utils'); |
34 | var fs = require('fs'); | 35 | var fs = require('fs'); |
35 | var TestSuiteResult = require('tester').TestSuiteResult; | 36 | var TestSuiteResult = require('tester').TestSuiteResult; | ... | ... |
1 | /*global phantom CasperError*/ | 1 | /*global phantom CasperError patchRequire*/ |
2 | 2 | ||
3 | if (!phantom.casperLoaded) { | 3 | if (!phantom.casperLoaded) { |
4 | console.log('This script must be invoked using the casperjs executable'); | 4 | console.log('This script must be invoked using the casperjs executable'); |
5 | phantom.exit(1); | 5 | phantom.exit(1); |
6 | } | 6 | } |
7 | 7 | ||
8 | var require = patchRequire(require); | ||
8 | var fs = require('fs'); | 9 | var fs = require('fs'); |
9 | var colorizer = require('colorizer'); | 10 | var colorizer = require('colorizer'); |
10 | var utils = require('utils'); | 11 | var utils = require('utils'); | ... | ... |
-
Please register or sign in to post a comment