2017-03-14 3 views
4

Ich benutze die Webpack 2 Node API und ich möchte die run() Methode unter Verwendung bluebird promitisten.Webpack-Compiler Instanz Promisifikation?

import Promise from 'bluebird' 
import webpack from 'webpack' 

const compiler = webpack(config) 
const runAsync = Promise.promisify(compiler.run) 

runAsync().then(stats => { 
    console.log('stats:', stats) 
}).catch(err => { 
    console.log('err:', err) 
}) 

Der Fehler Ich erhalte ist:

[TypeError: self.applyPluginsAsync is not a function]

Damit ich bin zu raten, die webpack Code nicht in einer Weise geschrieben, die mit drossel promisification kompatibel ist.

Wenn es eine andere Möglichkeit gibt, die run() Methode von webpack zu promitifizieren ..?

Alle diese Rückrufe und if Aussagen sind mir lästig.

Antwort

3

Sie müssen compiler als Kontext zu der promisify Methode übergeben.

const runAsync = Promise.promisify(compiler.run, { context: compiler }); 

oder nennen Sie es wie folgt:

runAsync.call(compiler).then(stats => {... 

Von der Drossel Docs:

Note that if the node function is a method of some object, you can pass the object as the second argument like so:

var redisGet = Promise.promisify(redisClient.get, {context: redisClient}); 
redisGet('foo').then(function() { 
    //... 
}); 
Verwandte Themen