'global' undefined after running webpack

I have a very simple wrapper module around a global object set by the environment in which scripts are run. The wrapper module simply does:

module.exports = global.foobar;

Previously, when I used the browser, this worked fine. When the browser globalwas the same as window.

However, I switch to webpack, and after starting webpack, the value globalhas changed. In the browser, it is no longer an alias window, instead it is undefined, and I get cannot read property foobar of undefined.

Now, in the case of my wrapper module, I can fix it in other ways, but I have other dependencies, and then the package is used in the chain buffer. This package is also used global( see here ), as well as crashing after starting webpack:

Uncaught TypeError: Cannot read property 'TYPED_ARRAY_SUPPORT' of undefined

Is there a way to make webpack treat globaljust like the browser did, with globalbeing an alias window?

+4
source share
3 answers

I finally found the problem. I have two webpack builds: the first built my library, and the second a demo. The problem was that both configurations had:

{
  node: {
    global: true
  }
}

It works fine if the first (one of the lib buildings) has global: false, and the second has global: true.

+5
source

webpackConfig

plugins: [new webpack.DefinePlugin({
    global: {}
})],
0

add the following plugins to the 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;
        });
      }
    }
  ]
0
source

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


All Articles