browserify / factor-bundle

factor browser-pack bundles into common shared bundles
Other
400 stars 27 forks source link

Lazy loading with factor bundle #88

Closed sassanh closed 6 years ago

sassanh commented 8 years ago

Suppose that I have:

// a.js:
require('./b.js').test()
require('./b.j' + 's').test()
// b.js
function test() {
    console.log(123);
}

Now suppose that I want to lazy load b.js. If I browserify a.js with factor-bundle, b.js's content will end up in common.js. One workaround is changing a.js to something like this:

// a.js:
require('./b.j' + 's'').test()
require('./b.j' + 's'').test()

Now b.js will not be included in common.js and will be in its own bundle. The problem is the require function in a.jsdoesn't know about './b.js' now so it fails to require it.

Questions:

  1. Is it possible to make the require in a.js aware of the id associated with './b.js'?
  2. Generally is it the good approach? What are the best practices for achieving this?

P.S. https://github.com/epeli/browserify-externalize seems to try to solve this issue, but it's not maintained anymore.