How to avoid bundling mock modules on Webpack build?

Viewed 1889

We are trying to make our React + GraphQL project independent of any other layers in the ecosystem to improve developers experience. In line with that, we have written a thin HOC that wraps Apollo own graphql HOC and uses an internal environment variable to switch between network fetching and mock data. On production builds, all that mock data is, obviously, not used, even if it is imported.

Is there any way to avoid including modules in Webpack's production bundle that you know you are not going to need while keeping everything else the same/not breaking the app?

Something like dynamic import() could do the trick, but that ends up chunking your build rather than omitting what you don't need/want.

UPDATE: The app was created using create-react-app 1.0.17 and later ejected.

2 Answers
Related