Skip to content

Support more cases where modules are being required in a dynamic way #9155

Closed
@fabiospampinato

Description

@fabiospampinato

Feature request

What is the expected behavior?

Sometimes modules are imported in a more dynamic way than WebPack currently supports, for instance there are multiple packages for lazily importing modules, so one might write code like this:

const importLazy = require ( 'import-lazy' );
const lodash = importLazy ( 'lodash' );

And WebPack will not include lodash in the bundle, but it should.

What is motivation or use case for adding/changing the behavior?

Some modules I depend on import modules like that, perhaps even in more dynamic ways. It's not always possible to make those packages WebPack-compatible (just import-lazy alone has ~4M+ weekly downloads, we can't update all the packages depending on it), but WebPack should still be smart enough to bundle our code correctly.

How should this be implemented in your opinion?

Solving this without requiring any modification at all to existing packages might be impossible, as I think WebPack would have to start executing the code it's trying to bundle in a very smart way.

But perhaps there could be a way for modules like import-lazy to tell WebPack: "This function may require the following modules when such and such happen", maybe some magic comment like:

function importLazy ( originalRequire ) { /* webpack-interface: () => require */
  return function lazyRequirer ( pkg ) {
    return originalRequire ( pkg );
  };
}

Could instruct webpack to treat it as if it was the provided code or something? And then WebPack could figure out that if foo and bar modules are being passed through that code they are eventually going to get required, so it can include those modules in the bundle too.

Are you willing to work on this yourself?

I'm not sure I'd be able to implement this, but maybe I could try 🤷‍♂

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions