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

前端 未结 6 1162
温柔的废话
温柔的废话 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';
    
    0 讨论(0)
  • 2021-01-31 03:44

    Shimming modules is the way to go: http://webpack.github.io/docs/shimming-modules.html

    I quote from the page:

    plugin ProvidePlugin

    This plugin makes a module available as variable in every module. The module is required only if you use the variable.

    Example: Make $ and jQuery available in every module without writing require("jquery").

    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery"
    })
    

    To use this with your webpack-config just add this object to an array called plugins in the config:

    // the plugins we want to use 
    var plugins = [
       new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery",
          "window.jQuery": "jquery"
       })
    ];
    
    // this is your webpack-config
    module.exports = {
        entry: ...,
        output: ...,
        module: ...,
        plugins: plugins
    }
    
    0 讨论(0)
  • 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 = $('<div />').text('hello world!!!').text();
    console.log(text);
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-31 03:46

    I've had a similar issue using jspm and dygraphs. The way i solved it was to use dynamic loading like you attempted using System.import but the important part was to chain-load each consecutive "part" using System.import again inside the promise onfulfillment handler (then) after setting the global namespace variable. In my scenario I actually had to have several import steps separated between then handlers.

    The reason it didn't work with jspm, and probably why it didn't work for you as well is that the import ... from ... syntax is evaluated before any code, and definitely before System.import which of async.

    In your case it could be as simple as:

    import jQuery from 'jquery';
    
    window.jQuery = jQuery;
    System.import('troublesome').then(troublesome => {
     // Do something with it...
    });
    

    Also note that the System module loader recommendation has been left out of the final ES6 specification, and a new loader spec is being drafted.

    0 讨论(0)
  • 2021-01-31 03:55
    1. run npm install import-loader.
    2. replace import 'troublesome' with import 'imports?jQuery=jquery,$=jquery!troublesome.

    In my opinion, this is the simplest solution to your question. It is similar to the answer you wrote in your question @TN1ck, but without altering your webpack config. For more reading, see: https://github.com/webpack/imports-loader

    0 讨论(0)
  • 2021-01-31 03:58

    Shimming is fine and there are various ways of resolving this, but as per my answer here, the simplest is actually just to revert to using require for the loading of the library that requires the global dependency - then just make sure your window. assignment is before that require statement, and they are both after your other imports, and your ordering should remain as intended. The issue is caused by Babel hoisting imports such that they all get executed before any other code.

    0 讨论(0)
提交回复
热议问题