R
R
retyui2017-02-27 11:48:44
webpack
retyui, 2017-02-27 11:48:44

Webpack 2 how to polyfill a Promise asynchronously?

Let's say:
webpack.config.js

, new webpack.ProvidePlugin({
    Promise: './bluebird-lazy.js'
  })

bluebird-lazy.js
exports default (function(w){
  if('Promise' in w){
    return w.Promise;
  }else{
    //return 
    import(bluebird).then(Promise => window.Promise); // вот тут и встает проблема асинхронности

  }
})(window);

I want to implement polyfill loading on demand!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Gromov, 2017-02-27
@Pathgk

Write a loadPolyfills function that accepts a callback (because you can't be sure at the time of calling it that the promises are implemented), and move all the entry point code into this callback. For purity, you can move this action into a self-written loader or plugin, or maybe there are already similar ones. Technically, this is the only correct way.
In your case, the function might look like this:

exports default function loadPolyfills(callback) {
  if (!window.Promise) {
    require.ensure([], function () {
      window.Promise = require('bluebird');

      callback();
    });
  } else {
    callback();
  }
};

If more polyfills are required, the logic will be more complicated.

V
Viktor Taran, 2017-11-14
@VladimirMelnik

RewriteRule ^blog/articles/([a-z0-9\-/]+) http://site.ru/blog/article.php\?id=$1 [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question