dimanche 18 mars 2018

Webpack 4 - Exposing variable to global context

I'm making a lib wrote in ES6 and will transpile it with Babel to the past version to avoid EcmaScript incompatibility.

class MyLib {
  constructor(name) {
    this.name = name;
  }

  sayHello() {
    return `Hello ${this.name}`;
  }
}

// Would this work / is this the right way?
// window.MyLib = MyLib

module.exports = MyLib;

This lib must be in the global scope, for the user does not need to import it from anywhere, or even has a simple HTML + CSS structure without the overhead of having a bundler like webpack configured.

What's the "right way" to make this lib available on the global scope?

A window.MyLib = MyLib before a module.exports would work or is the right way to do so?




Aucun commentaire:

Enregistrer un commentaire