hughsk / hughsk/envify

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

Open
#41 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
895
Forks
57
PR merge metrics
No merged PRs in 30d

Description

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

As noted in substack/node-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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.