2017-05-12 45 views
0

我想用webpack awesome-typescript-loader來捆綁我的項目,但是我遇到了一些模塊不能自行導出的問題。如何導入不使用awesome-typescript-loader導出模塊的npm包?

一個例子是bootstrap-dialog npm軟件包(我安裝了bootstrap-switch和@ types/bootstrap-switch軟件包)。 @types不會導出模塊,只是聲明一個名稱空間,並添加到JQuery接口中。 :https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/bootstrap-switch/index.d.ts

在我app.ts我用它作爲這樣的:

import {OtherClass} from './otherclass' 

$(document).ready(()=>{ 
    let thisPage= new PageEntry(); 
    thisPage.init(); 
} 

class PageEntry { 
    public init(): void { 
    $('.bss').bootstrapSwitch(); 
    let x = new OtherClass(); 
    x.doSomething(); 
    } 
} 

但是從的WebPack輸出不包括bootstrapSwitch代碼。我嘗試添加: import * as BootstrapSwitch from 'bootstrap-switch';但我顯然得到一個錯誤,指出

node_modules/@types/bootstrap-switch/index.d.ts'不是一個模塊。

如何確保引導開關被拉入?

我使用bootstrap-switch作爲例子,但是我有大約5個包含相同問題的包。

+0

我已經能夠得到這個與以下工作: – sheamus

回答

0

如果文件沒有出口,因此進口僅爲其副作用(例如,它增強了jQuery),正確的方法將其導入在ECMAScript中,並推而廣之打字稿,如下

import 'bootstrap-switch'; 

一個更完整的例子:

import $ from 'jquery'; 
import 'bootstrap-switch'; 

$("[name='my-checkbox']").bootstrapSwitch(); 
0

import * as bs from 'bootstrap-switch';給出了一個錯誤,當然是因爲沒有模塊導出。

但是下面兩行工作對我來說...只是不知道這是做的最好的方式:

declare var require: any; 
let bs = require('bootstrap-switch'); 
import {OtherClass} from './otherclass' 

希望這可以幫助別人。讓我知道是否有更好的方法。

+0

絕對是一個更好的方法 –