Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing in window to this in browserify

One of my dependencies uses the following to pass in window to its closure

(function (window) {
   //
})(this)

For the time being I can just change it to something more sensible so that it doesn't break browserify, but is there some method whereby I can force a value for this in a browserified module?

like image 954
wheresrhys Avatar asked Dec 22 '13 00:12

wheresrhys


1 Answers

I wrote a browserify transform called "moduleify" that should generally do what you want, i.e. wrap the offending code in an IIFE that looks kinda like this:

(function () {
// this === window
}.call(window));

In fact, my implementation is not much more sophisticated than that.

The original idea was to export a globals-polluting "module" as if it were a CommonJS module (e.g. have AngularJS export window.angular), but because it contains that wrapper, it should do the trick.

For instructions, see the README. If the offending script doesn't actually have anything it could reasonably export, just have it export window (which will result in module.exports = window['window']) or an arbitrary name that doesn't exist (resulting in undefined).

If you want to access the window object in your own browserify code, also check out the global module, which provides a nice wrapper to access browser globals safely in CommonJS modules.

like image 106
Alan Plum Avatar answered Sep 18 '22 13:09

Alan Plum