I have a very simple wrapper module around a global object set by the environment the scripts are run. The wrapper module simply does:
module.exports = global.foobar;
Previously when I used browserify this worked fine. When in the browser, global
was the same as window
.
However, I'm switching to webpack and after running webpack the meaning of global
has changed. In the browser it's no longer an alias to window
, instead it's undefined
, and I get cannot read property foobar of undefined
.
Now, in the case of my wrapper module I can fix that in other ways, but I have other dependencies, and further down the chain the buffer
package is used. That package also uses global
(see here) and also crashes after I've run webpack:
Uncaught TypeError: Cannot read property 'TYPED_ARRAY_SUPPORT' of undefined
Is there some way I can make webpack treat global
the same way browserify did, with global
being an alias of window
?
I finally found the problem. I have two webpack builds: the first one building my library and the second building the demo. The problem was that both configs had:
{
node: {
global: true
}
}
It works fine if the first one (the one building the lib) has global: false
, and the second one has global: true
.
add the following plugins in web.config.js file
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
{
'apply': function(compiler) {
compiler.parser.plugin('expression global', function() {
this.state.module.addVariable('global', "(function() { return this; }()) || Function('return this')()");
return true;
});
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With