Sharing variables through files using webpack

I have 2 js files. First file:

var object1 = {
    foo: 'bar'
}

Second file

var object2 = {
    foo2: 'bar2'
}
console.log(object1.foo);

These 2 files are created using webpack in the bundle.js file. But in this case I have problems: the variable object1 is not defined.

I analyzed the code in the bundle.js file - webpack create a file for each anonymous function - and I understand why I have an error.

Is it possible to do this with webpack or not?

Off course, I can use this variable as a global variable, but I think this is not a good idea.

0
source share
1 answer

The answer is very simple, just use

var object1 = require('./object1.js');
var object2 = {
    foo2: 'bar2'
}
console.log(object1.foo);
0
source

Source: https://habr.com/ru/post/1673823/


All Articles