2016-04-08 27 views
0

有什麼方法可以在全球範圍內擁有webpack執行模塊?具體來說,我的使用情況如下庫:在全球範圍內的webpack執行模塊

https://github.com/AzureAD/azure-activedirectory-library-for-js/blob/master/lib/adal.js

哪裏有下面的代碼:

var AuthenticationContext; 
if (typeof module !== 'undefined' && module.exports) { 
    module.exports.inject = function (conf) { 
     return new AuthenticationContext(conf); 
    }; 
} 

正如你可以看到模塊輸出的注入功能(不知道爲什麼他們不不只是出口班)。我能夠使用注入函數成功構造一個新的AuthenticationContext對象。但是,此庫中的一些功能依賴於全局AuthenticationContext類,並且在window.AuthenticationContext === undefined時出錯。我想用webpack捆綁這個模塊,但不知何故,我需要確保AuthenticationContext在全局範圍內可用。有沒有辦法做到這一點?

我已閱讀有關ProvidePlugin,但據我瞭解,ProvidePlugin只需要一個導出的值並將其附加到全局範圍。在這種情況下,我需要確保一個非導出的值在全局範圍內可用。

最明顯的解決方案就是在全局範圍內執行該模塊。不過,我希望這個模塊成爲這個包的一部分。我怎樣才能做到這一點?

在此先感謝。

回答

0

我想我解決了這個用下面的代碼:

import {inject} from 'adal-angular/lib/adal.js'; 
import config from './auth-config'; 

export default class Authenticator { 
    constructor() { 
     this.authContext = inject(config); 
     window.AuthenticationContext = this.authContext.constructor; 
    } 
} 

基本上,我手動暴露構造。