2016-05-18 64 views
0

我正在尝试为HtmlWebpackPlugin编写打字稿声明文件,但是我无法正常工作。输出类的NodeJS模块的TypeScript声明

HtmlWebpackPlugin默认导出是类HtmlWebpackPlugin的构造函数。我打算像这样使用它。

somefile.ts

import * as HtmlWebpackPlugin from 'html-webpack-plugin' 
new HtmlWebpackPlugin({..some options...}) 

我试图将其定义如下

shim.d.ts

/// <reference path="./shim.d.ts"/> 
declare module 'html-webpack-plugin' { 
    interface Options { 
    title: string, 
    filename: string, 
    template: string, 
    inject: (boolean | 'head' | 'body'), 
    favicon: string, 
    hash: boolean, 
    cache: boolean, 
    showErrors: boolean, 
    chunks: any, 
    chunksSortMode: ('none' | 'auto' | 'dependency'), 
    excludeChunks: any, 
    xhtml: boolean 
    } 
    class HtmlWebpackPlugin { 
    constructor(options: Options); 
    } 
    export default ??? 
} 

我不知道在???填写在这里。我尝试导出HtmlWebpackPlugintypeof HtmlWebpackPlugin,但没有任何效果。它给我以下类型的错误。

回答

0

在这里你去,

export { HtmlWebpackPlugin }; 

    // or you may directly export class 

    export class HtmlWebpackPlugin { 
    constructor(options: Options); 
    } 

希望这有助于!

+0

我不认为这项工作。 HtmlWebpackPlugin是Node.JS中的默认导出,因此声明中HtmlWebpackPlugin的导出也应该是默认的。 –

+0

我已经更新了我的问题,以防你想看看 –

+0

是的,你是对的,这将无法正常工作'new HtmlWebpackPlugin({.. some options ...})',我检查了是否使用'import * as X从“html-webpack-plugin”;'然后'new X.HtmlWebpackPlugin({..})'工作。让我试试看它是否可以直接使用。 –

0

import * as Module from 'module'表单不再适用于AMD/CommonJS,以便与ES6模块(使用此表单导出整个名称空间)有更好的互操作性。

解决方案:

制作HtmlWebpackPlugin出口非模块实体默认。它使用

declare module 'html-webpack-plugin' { 
    interface Options { 
    title: string, 
    filename: string, 
    template: string, 
    inject: (boolean | 'head' | 'body'), 
    favicon: string, 
    hash: boolean, 
    cache: boolean, 
    showErrors: boolean, 
    chunks: any, 
    chunksSortMode: ('none' | 'auto' | 'dependency'), 
    excludeChunks: any, 
    xhtml: boolean 
    } 

    export = class HtmlWebpackPlugin { 
    constructor(options: Options); 
    } 
} 

导入所需的风格

import HtmlWebpackPlugin = require('html-webpack-plugin')