2016-09-30 114 views
0

我有包裝器模塊在promise-polyfill -如何通過ProvidePlugin設置提供模塊的上下文?

/* jshint node: true */ 
/* global Promise: false */ 
"use strict"; 

if (typeof Promise === "undefined") { 
    module.exports = require('promise-polyfill'); 
} else { 
    module.exports = Promise; 
} 

然後我把它注射到我的所有模塊:

new webpack.ProvidePlugin({ 
    'Promise': 'require-promise', 
}) 

而我得到的一個問題。 Promise是一個空對象,而不是本地Promiseundefined。我如何將上下文(windowglobal)傳遞給此模塊?

生成的代碼看起來像:

function(module, exports, __webpack_require__) { 

/* WEBPACK VAR INJECTION */(function(Promise) {/*** IMPORTS FROM imports-loader ***/ 
(function() { 

/* jshint node: true */ 
/* global Promise: false */ 
"use strict"; 

console.log(Promise); 

if (typeof Promise === "undefined") { 
    module.exports = __webpack_require__(59); 
} else { 
    module.exports = Promise; 
} 


/*** EXPORTS FROM exports-loader ***/ 
module.exports = Promise; 
}.call(window)); 
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(7))) 

/***/ }, 
+0

究竟哪裏是你的問題? 'Promise'被拒絕爲'{}'? – Rohit416

+0

是的。我使用生成的代碼 –

+0

更新了答案,而'console.log(Promise)'輸出到....? – Rohit416

回答

0

修正:

new webpack.ProvidePlugin({ 
    'Promise': 'imports?Promise=>window.Promise!require-promise', 
}) 
相關問題