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

Accessing code outside browserified code #210

Open
iamdriz opened this issue Nov 10, 2017 · 4 comments
Open

Accessing code outside browserified code #210

iamdriz opened this issue Nov 10, 2017 · 4 comments

Comments

@iamdriz
Copy link

iamdriz commented Nov 10, 2017

Currently if I want to access code outside of the bundle JS I have to prefix the function or variable so it's attached to the window. e.g.

window.myFunction = function() { }

According to this answer on Stack Overflow:
https://stackoverflow.com/questions/23296094/browserify-how-to-call-function-bundled-in-a-file-generated-through-browserify/28805578#28805578

In addition to using the window attachment you can use a standalone mode... how do you use this with this gem? And once used how would I access these functions using the module?

@iamdriz iamdriz changed the title Accessing code inside browserified code Accessing code outside browserified code Nov 10, 2017
@cymen
Copy link
Member

cymen commented Nov 10, 2017

So you can add browserify arguments (the README has some examples) however the configuration doesn't have the option to add arguments for one module. If you adding --s as a global argument, every module would be exported which you definitely don't want.

So to answer your question, this gem doesn't allow you to do this. You could do a PR to add such configuration but I would also suggest it's probably a bad idea. It increases complexity for this gem and globals are really one of the biggest issues with JavaScript.

You are of course free to do whatever you wish but most of the intent behind this gem is to allow going from old school JavaScript (globals, etc) to modular JavaScript (no need for globals). So such a thing hasn't really been desired I would say and that is why this gem doesn't address such a need.

@cymen
Copy link
Member

cymen commented Nov 10, 2017

That said, pragmatically, if I was in your shoes, I would make a module that was used to both consume and export globals. There isn't a need for the browserify option if you go this route.

Global defined outside of browserified code

So say I had window.myGlobal and I wanted to access it from my browserified JavaScript, I would make a module like this:

globals.js

export const myGlobal = window.myGlobal;

Then I could consume it:

var myGlobal = require('./globals').myGlobal;

Global defined inside of browserified code

globals.js

var myGlobal = require('./my-global');

window.myGlobal = myGlobal;                 // now code outside of browserified code can access it
export const myGlobal = myGlobal;

Above, I set both window.myGlobal with the intent that non-browserified code accesses it that way and I also export myGlobal so that browserified code can do require the globals file and use it that way (instead of accessing window).

The reason I would do this is to make it very clear what is a global and what is not. And I also avoid polluting my modular code with globals.

@iamdriz
Copy link
Author

iamdriz commented Nov 13, 2017

Hmm it seems if I just do module.exports = {} at the end of my file then I can access the functions without having to do anything with window. Is that what you were getting at in you comment? If this is the case, then why would you need to do both window. and exports?

@cymen
Copy link
Member

cymen commented Nov 13, 2017

I might have misunderstood what you meant by if I want to access code outside of the bundle JS. If you can require the file, then yes, just doing module.exports is great.

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

No branches or pull requests

2 participants