Created
September 3, 2012 07:56
-
-
Save mashihua/3607735 to your computer and use it in GitHub Desktop.
Phantomjs invoke seajs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define (require, exports, module) -> | |
console.log 'From hello' | |
exports.sayHello = -> | |
"Hellow world!" | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env phantomjs | |
# Insert seajs | |
phantom.injectJs "./sea-debug.js" | |
# CoffeeScript plugin | |
define 'coffee',[], (require) -> | |
sdk = seajs.pluginSDK | |
util = sdk.util | |
module = sdk.Module | |
isCoffee = false | |
resolve = module._resolve | |
fetch = module._fetch | |
module._resolve = (id, refUrl) -> | |
uri = resolve id, refUrl | |
if util.indexOf(refUrl,'coffee') > -1 | |
isCoffee = true | |
uri = uri.replace /\.js$/, '' | |
uri | |
module._fetch = (url, callback) -> | |
if isCoffee | |
url = url.replace /^file:\/\//, '' | |
phantom.injectJs url | |
callback() | |
else | |
fetch url, callback | |
return | |
# Configuration of seajs | |
seajs.config | |
preload: ['coffee'] | |
# For phantomjs is not set current location | |
# Use `file://#{phantom.libraryPath}` for config.base | |
base : "file://#{phantom.libraryPath}" | |
# Invoke seejs | |
seajs.use ["hello.coffee"], (hello) -> | |
if hello | |
console.log hello.sayHello() | |
phantom.exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment