jrburke/requirejs — Issue #146: Referencing 'window' in plugin causes error on build
Author: Integralist
Created: 2011-11-28 10:13:34+00:00
State: open

Hi,

I've got the following plugin code: https://github.com/Integralist/RequireJs-Example/blob/master/Assets/Scripts/Plugins/jsonp.js which references the window object (because I needed to set a global method & variable for some jsonp services to access).

But when running a build script I get the following error...

```
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Error: Error evaluating module "undefined" at location "/Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/project-build/Assets/Scripts/Plugins/jsonp.js":
ReferenceError: window is not defined
fileName:/Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/project-build/Assets/Scripts/Plugins/jsonp.js
lineNumber: undefined
http://requirejs.org/docs/errors.html#defineerror
In module tree:
example-jsonp
jsonp

at Function.onError (/Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/Assets/Scripts/Build/r.js:7653:23)
at execManager (/Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/Assets/Scripts/Build/r.js:601:28)
at /Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/Assets/Scripts/Build/r.js:629:25
at execManager (/Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireJs/Assets/Scripts/Build/r.js:606:17)
at /Users/stormcreative/Dropbox/Library/JavaScript/Libraries/RequireMark-McDonnells-iMac:Build stormcreative$
```

I checked @millermedeiros async plugin https://github.com/millermedeiros/requirejs-plugins/blob/master/src/async.js and I can see he is also using a reference to `window`, but he is doing it within the returned `load` method so I'm assuming it's able to reference `window` at that point?

Any ideas how I could change my plugin code so a reference to `window` can be found?

UPDATE: I tried copying @millermedeiros in the sense that I moved my `window` access within the returned `load` method...

```
return {
/**
* @param resource { String } the resource to be loaded
* @param req { Function } a local require() for loading other modules
* @param load { Function } a function to call with the value for name (this tells the loader that the plugin is done loading the resource)
* @param config { Object } the main configuration object RequireJs is using
*/
load: function(resource, req, load, config) {
/*
* NOTE: I originally had set window['rjs_global'] and window['rjs_jsonp'] outside of the returned object literal
* But when running the build script, there was no access to a 'window' object so nodejs choked and stopped the build.
*
* Looking at @millermedeiros' async plugin https://github.com/millermedeiros/requirejs-plugins/blob/master/src/async.js
* I noticed he had his 'window' reference inside the returned 'load' method so I copied that
*/
// Create global variable for jsonp service to execute
window['rjs_global'] = function(data) {
// Create global variable to store returned data
window['rjs_jsonp'] = data;
};

loadScript(resource, function(loaded){
if (loaded && ('rjs_jsonp' in window)) {
load(window['rjs_jsonp']);
}
});
}
};
```

...but the same error occurred when running the build script.

Any ideas?

Kind regards,
Mark