简体   繁体   English

为什么我的 Webpack 捆绑包包含两次 jQuery?

[英]Why does my Webpack bundle include jQuery twice?

I set up a parent module with 2 submodule dependencies.我设置了一个具有 2 个子模块依赖项的父模块。 The parent module has no specified jQuery dependency, but each submodule specifies jQuery ^3.3.1 as a dependency (results in 3.4.1 for each submodule).父模块没有指定 jQuery 依赖项,但每个子模块都指定 jQuery ^3.3.1 作为依赖项(每个子模块的结果为 3.4.1)。 I Webpacked the parent module and then I see in the generated bundle file that jQuery 3.4.1 is included twice.我对父模块进行了 Webpacked,然后在生成的捆绑文件中看到 jQuery 3.4.1 包含两次。 What should I be doing so that the same version of jQuery isn't included twice?我应该怎么做才能不包含两次相同版本的 jQuery? I did try the splitchunks plugin and it did generate chunks but jQuery was still in there twice.我确实尝试了 splitchunks 插件,它确实生成了块,但 jQuery 仍然在那里两次。 I thought that Webpack is supposed to automatically analyze dependencies in the module graph and optimize the bundled code?我以为 Webpack 应该自动分析模块图中的依赖关系并优化捆绑代码? I haven't yet tested NPM peer dependencies or the Webpack de-dupe plugin.我还没有测试过 NPM 对等依赖项或 Webpack 重复数据删除插件。 I'm also wondering if there's something about jQuery itself to where Webpack can't/decides not to de-dupe automatically?我还想知道 jQuery 本身是否与 Webpack 不能/决定不自动删除重复数据有关?

In both submodules' index.js files, I'm using:在两个子模块的 index.js 文件中,我正在使用:

import $ from "jquery"

In each submodule's package.json I specified:在每个子模块的 package.json 中我指定:

"dependencies": {
"jquery" : "^3.3.1"
}

Then I did a npm install on each submodule.然后我在每个子模块上安装了 npm。

module.exports = {
    resolve: {
        alias: {
            // fix every jQuery to our direct jQuery dependency. Shariff 1.24.1 brings its own jQuery and it would be included twice without this alias.
            'jquery': __dirname + '/node_modules/jquery/',
        },
    },
};

Note -happens because Shariff 1.24.1 defines jQuery as its own dependency instead of defining it as peer dependency in the package.json .注意 - 发生是因为 Shariff 1.24.1 将jQuery定义为它自己的依赖项,而不是在package.json中将其定义为对等依赖项。

Refrence参考

I found that module resolution can automatically happen downwards (into child folders) of an entry point folder, but not upwards (in module folders outside of the entry point folder).我发现模块解析可以自动发生在入口点文件夹的下方(进入子文件夹),但不能向上(在入口点文件夹之外的模块文件夹中)。 I found that I needed to "tell" Webpack about these other module locations by adding a "resolve" object to Webpack.config:我发现我需要通过将“解决”object 添加到 Webpack.config 来“告诉”Webpack 关于这些其他模块位置:

resolve: {
        modules: [
          path.resolve(__dirname, "src/modules/pagetype/node_modules"),
          path.resolve(__dirname, "src/modules/sitewide/node_modules"),
          path.resolve(__dirname, "src/modules/template/node_modules"),
          "node_modules"
        ]
      }, // resolve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM