Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack multiple requires resolving to same file but being imported twice

Hello I just had this problem with 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. However if you look in the output bundle there are two different webpack functions both with the same content. I'm pretty sure I can use an alias to fix this and then just do require('something') in both files. 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.

like image 309
epelc Avatar asked Aug 26 '14 15:08

epelc


People also ask

What happens if I import the same module twice?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

Can you have multiple entry points in a single Webpack configuration file?

webpack.config.jsWe can also pass an array of file paths to the entry property which creates what is known as a "multi-main entry". This is useful when you would like to inject multiple dependent files together and graph their dependencies into one "chunk".

What is entry point in Webpack?

The entry object is where webpack looks to start building the bundle. The context is an absolute string to the directory that contains the entry files.


1 Answers

You can just use the 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()
    ]
};
like image 174
Jessie Schalles Avatar answered Sep 27 '22 18:09

Jessie Schalles