Skip to content

Instantly share code, notes, and snippets.

@mashihua
Created September 3, 2012 07:56
Show Gist options
  • Save mashihua/3607735 to your computer and use it in GitHub Desktop.
Save mashihua/3607735 to your computer and use it in GitHub Desktop.
Phantomjs invoke seajs
define (require, exports, module) ->
console.log 'From hello'
exports.sayHello = ->
"Hellow world!"
return
#!/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