Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide scope for require() or require.noConflict() #403

Closed
robbles opened this issue May 17, 2013 · 5 comments
Closed

Provide scope for require() or require.noConflict() #403

robbles opened this issue May 17, 2013 · 5 comments

Comments

@robbles
Copy link

robbles commented May 17, 2013

I'm using browserify to compile code for widgets and libraries that can be included on other people's pages. Ideally, it would be nice to avoid conflicts with pages that use some kind of CommonJS or AMD loading using either a standard noConflict([parent object]) method or an option provided to browserify. I can't trust publishers using this code to have a compatible implementation of require, so browserify's version needs to be sandboxed from theirs.

Basically, I want the require method to be attached to a global object of my choice, and for all bundles to use this reference somehow instead of window.require - an example of this can be found in the easyXDM library.

@substack I know you've worked some related ideas with substack/yarnify - any suggestions on how this could work? I think browserify could be incredibly useful for bundling third-party JS libraries if this is possible.

@dominictarr
Copy link
Contributor

browserify falls back (and doesn't overwrite) any pre-existing 'require' that is in scope.
so, this shouldn't actually be a problem.

Also, browserify doesn't add anything to the global scope unless it is explicitly told to. (as far as I can tell)

are you sure this is actually a problem? are you using the latest browserify?

@robbles
Copy link
Author

robbles commented May 21, 2013

I agree that it's not a problem in most cases. I'm mainly worried about trying to use https://github.com/substack/node-browserify#multiple-bundles when there is an incompatible require function defined on the page.

By "incompatible", I mean the page defines a window.require function that doesn't load modules in the expected way (such as an asset loader for a game, or some other utility function). If this is defined by the page in between the evaluation of a bundle and another file that uses it, it'll break.

I can probably work around this by ensuring all browserified files execute together and in the right order, but it would be nice if I could specify a context or an alternate name for require to avoid the problem entirely.

@robbles
Copy link
Author

robbles commented May 21, 2013

Also, as of browserify v2.12.5, it appears to overwrite an existing window.require. I'm assuming it's saving a reference to the original so that it can load external modules though.

@dominictarr
Copy link
Contributor

the best way would be to wrap encapsulate that thing in a closure, (or the browserify bundles)

';(function (require) {' + bundle + '})(' + noConflict + ');'

it's getting complicated to add this as a feature to browserify, you should make another module to do this and publish on npm!

@ghost
Copy link

ghost commented Dec 20, 2013

I think the best way to handle this right now is to just wrap the bundle output.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants