Can I use an ES6/2015 module import to set a reference in 'global' scope?

前端 未结 6 1166
温柔的废话
温柔的废话 2021-01-31 03:22

I have this situation where I am trying to import an existing library, which I\'ll call troublesome (using Webpack/Babel FWIW) and it has a global reference to

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 03:34

    Importing jQuery into your module does not make it available for 'troublesome'. Instead, you could create a thin wrapper module for 'troublesome' that provides jQuery and any other required "globals".

    troublesome-module.js:

    // Bring jQuery into scope for troublesome.
    import jQuery from 'jquery';
    // Import any other 'troublesome'-assumed globals.
    
    // Paste or have build tool interpolate existing troublesome.js code here.
    

    Then in your code you should be able to

    import 'troublesome-module';
    

提交回复
热议问题