Search code examples
browserify

Create multiple bundles for different interface implementations


I would like to achieve the following in a project where I'm using browserify:

I would like to generate 2 different bundles from the same sources, each one including a given implementation of a common interface,

        requires            requires                generates
a.js +------------> b.js +------------> c.impl1.js +-----------> bundle.1.js
                         |
                         +------------> c.impl2.js +-----------> bundle.2.js

How should I require the different implementations from the b.js file and configure browserify to not to end up with a single bundle with all the dependencies included?

Thanks in advance!


Solution

  • I found out a solution while looking to some unrelated code.

    I'm now using this pattern to create an intermediate interface file c.js:

    if (process.env.CLASS_IMPL === 'impl1') {
      module.exports = require('./c.impl1')
    } else {
      module.exports = require('./c.impl2')
    }
    

    So I export one or other implementation depending on an environment variable I set before running the bundling process.