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

前端 未结 6 1164
温柔的废话
温柔的废话 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:46

    For es6/2015 I done the following.

    import {jsdom} from 'jsdom';
    import jQuery from 'jquery';
    var window = jsdom(undefined, {}).defaultView;
    var $ = jQuery(window);
    //window.jQuery = $; //probably not needed but it's there if something wrong
    //window.$ = $;//probably not needed but it's there if something wrong
    

    Then you can use it as normal

    var text = $('
    ').text('hello world!!!').text(); console.log(text);

    Hope this helps.

提交回复
热议问题