The behavior is correct. In general, Webpack will walk over the tree of dependencies (included transitive dependencies i.e. dependencies of dependencies) of your application.
Also, webpack ensures that it packs the some dependency exactly once as long as all the imports leading to that dependency are resolved to a same file. This behavior changes only when:
- You have symbolic links (or shortcut links) in your code or node_modules.
- You have multiple versions of the same dependency.
- Accidental upper / lower case letter changes leads to a different file (Sometimes, it happens when code written on Windows which is case-insensitive is being built on Linux or vice a versa.)
- In rare case, if you are using dynamic imports, Webpack sometimes cannot figure out the exact dependencies.
You can use Webpack analyzer to figure out such anomalies in your bundle. Further, you can also use Webpack resolve functionality to guide the Webpack to never pack similar dependencies twice (which defer only by patch number or so).