Import ES6 module into global scope

前端 未结 2 1565
慢半拍i
慢半拍i 2020-12-06 10:47

TLDR: How can I make a module (imported via ES6 syntax) globally scoped (or reference an imported class inside another class)?


I\'m importing

2条回答
  •  星月不相逢
    2020-12-06 11:28

    If you are using webpack it's easy to setup it. So here is a simple example how to implement it.

    webpack.config.js

    module.exports = {
      entry: 'test.js',
      output: {
        filename: 'bundle.js',
        path: 'home',
        library: 'home' // it assigns this module to the global (window) object
      }
      ...
    }
    

    some.html

    
    

    Also if you open your bundle.js file you will see how webpack did it for you.

    var home =  // main point
    /*****/ (function(modules) blablabla)
    ...
    

    Also i suggest look at webpack library configuration.

    I hope it will help you.

    Thanks

提交回复
热议问题