How does webpack handle multiple files importing the same module React

前端 未结 1 887
温柔的废话
温柔的废话 2020-12-05 10:33

I have a React app which has many components importing the same modules. Does webpack import each module once for each file requesting it, resulting in duplicate code(in thi

相关标签:
1条回答
  • 2020-12-05 10:59

    No, webpack (similar to browserify and other module bundlers) will only bundle it once.

    Every react component will get its own scope, and when it requires/imports another module, webpack will check if the required file was already included or not in the bundle.

    So no, it will not result in duplicate code. However if you import some external packaged libraries, you may have some duplicate code. In that case, you can use Webpack's Deduplication plugin to find these files and deduplicate them. More info here for that: https://github.com/webpack/docs/wiki/optimization#deduplication

    0 讨论(0)
提交回复
热议问题