简体   繁体   中英

windows/global object library don't work with webpack5

I coded my library which add Class to global window object:

const Maplat = window.Maplat = {};

Maplat.createObject = blah blah..

With being compiled by webpack4, this works very well.

<html>
  <head>
    <script src="./assets/maplat_core.js"></script>
  </head>
  <body>
    <script>
      Maplat.createObject(...);
      // Works fine
    </script>
  </body>
</html>

But after I updated webpack 5, this does not work.

I checked the compiled result code, it is comiled as:

          ...
          , Tv = window.Maplat = {};
        Tv.createObject = function(t) {
            return new Promise((function(e) {
                var n = new Rv(t);
                n.waitReady.then((function() {
                    e(n)
                }
                ))
            }
            ))
        }
    }
    )()
}
)();

With this compiled result,

const Maplat = window.Maplat = {};

works after

Maplat.createObject(...);

so this compiled result does not work.

How to resolve this? How can I add class to window/global object by webpack5?

Finally it became work with this webpack setting:

  output: {
    path: path.resolve(__dirname, "../dev"),
    filename: '[name].js',
    libraryTarget: 'umd',
    globalObject: 'this',
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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