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

Could envify be used to enable dynamic-ish require() statements with browserify? #41

Open
JedWatson opened this issue Apr 12, 2016 · 3 comments

Comments

@JedWatson
Copy link

Just following a thread here, not sure if it's possible or not.

As noted in browserify/browserify#377 browserify uses static analysis to figure out what to build.

I'm working on a network of sites where we want to include different config for each property, where the property is identified by an environment variable.

That means we have a config file that looks like this:

if (process.env.SITE_KEY === 'site1') {
    module.exports = require('./site1');
} else if (process.env.SITE_KEY === 'site2') {
    module.exports = require('./site2');
}

Any other module that wants to know something in the site config requires this one.

The problem is that all site configs are bundled into each site's build, even though only the active one is used. Not really a huge issue, but it's not a clean solution either.

It seems like it might be possible to hook envify into the browserify pipeline in a way that replaces environment variables before require statements are evaluated, which would allow the following:

module.exports = require('./' + process.env.SITE_KEY);

If we could rewrite that with envify before browserify goes looking for require statements, it would see:

module.exports = require('./site1');

Any ideas on whether that's possible / feasible? Or do you know of any other ways to achieve this? I went looking but didn't find anything.

@hughsk
Copy link
Owner

hughsk commented Apr 12, 2016

@JedWatson envify does indeed get applied before browserify looks for require statements! I think it should work using require('./' + process.env.SITE_KEY) but gotta try and see :)

@SteffanDonal
Copy link

Weighing in my two pennies here - I can confirm that this does not work.

You must specifically require individual modules using a constant string.

@ewnd9
Copy link

ewnd9 commented Sep 28, 2016

--exclude from browserify itself should work, something like

$ browserify index.js --exclude 'src/site2.js' -g [ envify purge --SITE_KEY=site1  ]

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

4 participants