Created
July 26, 2013 12:34
-
-
Save SeanMcMillan/6088521 to your computer and use it in GitHub Desktop.
Attempting to load browser-js on node. I can make a fake window and load it, but if I turn `global` into the fake window, it fails with vm.runInNewContext(source, fakewindow); ^
ReferenceError: window is not defined
This file contains hidden or 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
'use strict'; | |
var fakewindow; | |
var fakedocument, fakenode; | |
fakedocument = { | |
createDocumentFragment: function() { | |
return { | |
appendChild: function() {} | |
}; | |
}, | |
documentElement: {}, | |
nodeType: 9, | |
attachEvent: function() {} | |
}; | |
fakenode = { | |
setAttribute: function() {}, | |
getElementsByTagName: function() { | |
return {}; | |
}, | |
ownerDocument: fakedocument | |
}; | |
fakedocument.createElement = function() { | |
return fakenode; | |
}; | |
if (typeof window !== 'undefined') { | |
fakewindow = window; | |
fakewindow.loadGlobalScript = function() {}; | |
} else if (typeof global !== 'undefined') { | |
fakewindow = {}; | |
//fakewindow = global; // If this is uncommeted, it fails | |
fakewindow.window = fakewindow; | |
fakewindow.document = fakedocument; | |
fakewindow.attachEvent = function(){}; | |
fakewindow.console || (fakewindow.console = console); | |
var source, vm; | |
vm = require('vm'); | |
source = '(function(window, x) { console.log(window);})(window);'; | |
vm.runInNewContext(source, fakewindow); | |
} | |
module.exports = fakewindow; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, how did I miss your comment?
Yeah, it was for a testing framework. We eventually went with Testem + PhantomJs, and stopped trying to run on node at all.
I guess that the
global
object is actually derived from some kind of "default globals" object, and only hasOwnProperty of things you define on it directly? that's pretty cool, except that it breaks this use case. (Which is, pretty far from normal, I admit.)