browser-build icon indicating copy to clipboard operation
browser-build copied to clipboard

add basic compatiblity with browserify

Open calvinmetcalf opened this issue 10 years ago • 7 comments

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

calvinmetcalf avatar May 19 '14 13:05 calvinmetcalf

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

krisnye avatar May 20 '14 01:05 krisnye

I'm not sure that will work in strict mode On May 19, 2014 9:57 PM, "Kris Nye" [email protected] wrote:

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

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

calvinmetcalf avatar May 20 '14 09:05 calvinmetcalf

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

calvinmetcalf avatar May 20 '14 11:05 calvinmetcalf

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.

krisnye avatar May 20 '14 15:05 krisnye

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.

calvinmetcalf avatar May 20 '14 16:05 calvinmetcalf

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.

krisnye avatar May 20 '14 16:05 krisnye

'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

calvinmetcalf avatar May 20 '14 16:05 calvinmetcalf