bundling something that uses colors.js breaks on dynamic require

It looks to me like the bundling of a dynamic require attempts to bundle every file in the package, in case thats the file that is dynamically required. In this case i’m getting stuff like:

Module parse failed: /home/vagrant/backend/node_modules/colors/MIT-LICENSE.txt
Module parse failed: /home/vagrant/backend/node_modules/colors/example.html

Clearly files that would never be required via a dynamic require. In fact, tho, I’m not even using the function that contains the dynamic require. It would be nice if webpack could somehow detect that fact, and remove it from the bundle. Doing this kind of pruning would give a lot of efficiency gains in fact.

What are some ways to work around this?

Author: Fantashit

1 thought on “bundling something that uses colors.js breaks on dynamic require

  1. require(theme) creates a context. A context contains all files in a directory matching a RegExp. Normally the RegExp is extracted by the static analysis, but the ContextReplacementPlugin overrides the RegExp. In my example with /^$/ which matches nothing, resulting in an empty context.

Comments are closed.