Tree Shaking with require.context

Hy Guys,

I have just a simple question. If I use require.context to automatic import my files liked that:

const exportFile = (path, req) => {
  const [, componentName] = path.match(/\.\/(.*)\/index.js?$/i);
  const component = req(path);

  module.exports[componentName] = component.default || component;
};

const requireAll = (ctx) =>
  ctx.keys().forEach(path => exportFile(path, ctx));

requireAll(require.context('./', true, /\.\/(.*)\/index.js?$/i));

Webpack will make Tree Shaking as well?

Thanks!

Author: Fantashit

2 thoughts on “Tree Shaking with require.context

  1. is there any workaround to archive this? require.context is really useful but cannot use tree shaking is a big disadvantage.

Comments are closed.