Skip to content

Comments

add basic compatiblity with browserify#2

Open
calvinmetcalf wants to merge 4 commits intokrisnye:masterfrom
calvinmetcalf:browserify-compat
Open

add basic compatiblity with browserify#2
calvinmetcalf wants to merge 4 commits intokrisnye:masterfrom
calvinmetcalf:browserify-compat

Conversation

@calvinmetcalf
Copy link

adds basic compatibility with some browserify idioms, process.browser to check if we are in the browser or not, and global as a shortcut to whatever the global object happens to be

@krisnye
Copy link
Owner

krisnye commented May 20, 2014

Shouldn't it use this instead of self?
}({browser: true},self))

@calvinmetcalf
Copy link
Author

I'm not sure that will work in strict mode
On May 19, 2014 9:57 PM, "Kris Nye" notifications@github.com wrote:

Shouldn't it use this instead of self?
}({browser: true},self))


Reply to this email directly or view it on GitHubhttps://github.com//pull/2#issuecomment-43578999
.

@calvinmetcalf
Copy link
Author

so what browserify does is typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}

@krisnye
Copy link
Owner

krisnye commented May 20, 2014

self is not defined in node though, and these shimmed files should work in both node and the browser. 'this' works in both. If you change self to this then it will be fine.

I actually recommend just including a file on the browser that shims for node properties you need right before you include the require.js

something like global.js:

var global = function() { return this; }();
global.global = global;
global.process = {browser:true};

This way, you can provide whatever you need without having to continually extend the output of each module.
For instance, these files are also usable by a Rhino interpreter on the back end running on google app engine. It's not node, but it's also not a browser, so I probably wouldn't want to depend on process.browser.

@calvinmetcalf
Copy link
Author

that won't work in strict mode (will break if somebody enables global strict) and also messes with the global scope which many people don't like, updated this to more accurately tell if you're in a browser, and accurately get the right global object.

@krisnye
Copy link
Owner

krisnye commented May 20, 2014

If you wrap it in a function call then it won't violate strict:

(function() {
    this.global = this;
    this.process = {browser:true};
})();

If you want compatibility with the node environment in the browser, then both 'global' and 'process' should be defined as global variables, not as locally scoped variables. That's exactly how they are provided in nodejs. The only variables that are locally scoped in nodejs are module, exports and require which is exactly what we provide.

@calvinmetcalf
Copy link
Author

'use strict'
(function() {
    this.global = this;
    this.process = {browser:true};
})();

one last commit, should work the same in node if it has the wrapper or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants