De-Duplicate libraries in app within deeply nested node modules

☆樱花仙子☆ 提交于 2020-01-03 04:51:14

问题


I have a app in which i can add modules as node_modules. Now, these modules and app uses a library XYZ as node module. Also, these modules have other node modules which has their own library XYZ as a node module.

So, this is roughly how the structure of my app looks like

I use gulp and webpack and i am trying to some how de-duplicate library XYZ. I want to build a task that would go through this nested tree of node modules and build out 1 common version of library XYZ. How can I achieve that?

I tries using deDupePlugin, where this is all i added to my gulp default task and it did not work.. Is there anything i missed?

plugins: [
            new webpack.optimize.DedupePlugin()
           // new CommonsChunkPlugin("commons", "commons.js")
        ],

OR, is there any other way to achieve that? Any help will be really appreciated


回答1:


DedupePlugin will only dedupe files that are completely equal. Your modules might depend on different versions of the library, which is why it might not always get deduped. And even when files are deduped, the modules themselves won't be, so your requirement of a single instance won't be satisfied.

You can use resolve.alias to redirect all require('libraryXYZ') calls to the same top-level instance.

resolve: {
  alias: {
    libraryXYZ: require('path').resolve('./node_modules/libraryXYZ'),
  },
},

Here is a comparison repository I made that showcases different ways to dedupe a module.



来源:https://stackoverflow.com/questions/33398396/de-duplicate-libraries-in-app-within-deeply-nested-node-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!