简体   繁体   English

Webpack multiple需要解析到同一个文件,但要导入两次

[英]Webpack multiple requires resolving to same file but being imported twice

Hello I just had this problem with webpack. 您好我刚刚遇到webpack这个问题。 If I do require('../something') from one file, and then I do require('../../something') in another file they both end up resolving to the same file. 如果我确实从一个文件中require('../something') ,然后我在另一个文件中require('../../something')它们最终都会解析为同一个文件。 However if you look in the output bundle there are two different webpack functions both with the same content. 但是,如果查看输出包,则会有两个具有相同内容的不同webpack函数。 I'm pretty sure I can use an alias to fix this and then just do require('something') in both files. 我很确定我可以使用别名来解决这个问题,然后在两个文件中只require('something') But is this the right way to do it or am I missing something? 但这是正确的做法还是我错过了什么?

Btw I need this because it's causing several problems with angularjs undefining my controllers. 顺便说一句我需要这个,因为它导致了几个问题,angularjs取消定义我的控制器。

You can just use the DedupePlugin. 你可以使用DedupePlugin。 It looks for if the module has already been included in your build and if so, doesn't include it again. 它会查找模块是否已包含在您的构建中,如果已包含,则不会再次包含该模块。 It's easy to set up and you don't need to require or install anything extra. 它很容易设置,您不需要或安装任何额外的东西。

var webpack = require("webpack");

module.exports = {
    // more of your config
    // ...
    plugins: [
        new webpack.optimize.DedupePlugin()
    ]
};

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

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