From a4e52c5eb19eed6e30aaa31439cdbaadd6a75694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Gon=C3=A7alves=20da=20Silva?= Date: Mon, 25 Mar 2019 13:34:48 -0300 Subject: [PATCH] Add type definitions to webpack-plugin-serve (#33948) * feat(wps): add webpack-plugin-serve types * fix export method * fix declaration * add strictFunctionTypes * fix declaration problems * remove redundant declare * add correct level types * export history api interfaces * reuse koa-compress and koa-static * fix padding --- types/connect-history-api-fallback/index.d.ts | 38 +++++----- types/koa-compress/index.d.ts | 22 +++--- types/webpack-plugin-serve/index.d.ts | 69 +++++++++++++++++++ types/webpack-plugin-serve/tsconfig.json | 23 +++++++ types/webpack-plugin-serve/tslint.json | 1 + .../webpack-plugin-serve-tests.ts | 39 +++++++++++ 6 files changed, 162 insertions(+), 30 deletions(-) create mode 100644 types/webpack-plugin-serve/index.d.ts create mode 100644 types/webpack-plugin-serve/tsconfig.json create mode 100644 types/webpack-plugin-serve/tslint.json create mode 100644 types/webpack-plugin-serve/webpack-plugin-serve-tests.ts diff --git a/types/connect-history-api-fallback/index.d.ts b/types/connect-history-api-fallback/index.d.ts index a39fbfdbd6..b32e61e147 100644 --- a/types/connect-history-api-fallback/index.d.ts +++ b/types/connect-history-api-fallback/index.d.ts @@ -10,26 +10,28 @@ import { Url } from 'url'; import * as core from "express-serve-static-core"; -declare function historyApiFallback(options?: Options): core.RequestHandler; -declare namespace historyApiFallback {} export = historyApiFallback; -interface Options { - disableDotRule?: true; - htmlAcceptHeaders?: string[]; - index?: string; - logger?: typeof console.log; - rewrites?: Rewrite[]; - verbose?: boolean; -} +declare function historyApiFallback(options?: historyApiFallback.Options): core.RequestHandler; -interface Context { - match: RegExpMatchArray; - parsedUrl: Url; -} -type RewriteTo = (context: Context) => string; +declare namespace historyApiFallback { + interface Options { + disableDotRule?: true; + htmlAcceptHeaders?: string[]; + index?: string; + logger?: typeof console.log; + rewrites?: Rewrite[]; + verbose?: boolean; + } -interface Rewrite { - from: RegExp; - to: string | RegExp | RewriteTo; + interface Context { + match: RegExpMatchArray; + parsedUrl: Url; + } + type RewriteTo = (context: Context) => string; + + interface Rewrite { + from: RegExp; + to: string | RegExp | RewriteTo; + } } diff --git a/types/koa-compress/index.d.ts b/types/koa-compress/index.d.ts index 24b7a1e1af..9b0a4415be 100644 --- a/types/koa-compress/index.d.ts +++ b/types/koa-compress/index.d.ts @@ -16,12 +16,18 @@ /// /// -declare module "koa-compress" { +import * as Koa from "koa"; +import * as zlib from "zlib"; - import * as Koa from "koa"; - import * as zlib from "zlib"; +/** + * Compress middleware for Koa + */ +declare function koaCompress(options?: koaCompress.CompressOptions): Koa.Middleware; - interface CompressOptions extends zlib.ZlibOptions { +export = koaCompress; + +declare namespace koaCompress { + export interface CompressOptions extends zlib.ZlibOptions { /** * An optional function that checks the response content type to decide whether to compress. By default, it uses compressible. */ @@ -32,12 +38,4 @@ declare module "koa-compress" { */ threshold?: number } - - /** - * Compress middleware for Koa - */ - function compress(options?: CompressOptions): Koa.Middleware; - - namespace compress {} - export = compress; } diff --git a/types/webpack-plugin-serve/index.d.ts b/types/webpack-plugin-serve/index.d.ts new file mode 100644 index 0000000000..1233333ebc --- /dev/null +++ b/types/webpack-plugin-serve/index.d.ts @@ -0,0 +1,69 @@ +// Type definitions for webpack-plugin-serve 0.7 +// Project: https://github.com/shellscape/webpack-plugin-serve +// Definitions by: Matheus Gonçalves da Silva +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 +/// + +import { Url } from 'url'; +import { Config as HttpProxyMiddlewareConfig, Proxy } from 'http-proxy-middleware'; +import * as Koa from 'koa'; +import { + ServerOptions as Http2ServerOptions, + SecureServerOptions as Http2SecureServerOptions, +} from 'http2'; +import { ServerOptions as HttpsServerOptions } from 'https'; +import { ZlibOptions } from 'zlib'; +import { Compiler } from 'webpack'; +import { Options as HistoryApiFallbackOptions } from 'connect-history-api-fallback'; +import { CompressOptions } from 'koa-compress'; +import { Options as KoaStaticOptions } from 'koa-static'; + +export interface Builtins { + proxy: (args: HttpProxyMiddlewareConfig) => Proxy; + compress: (opts: CompressOptions) => void; + static: (paths: string[], opts?: KoaStaticOptions) => void; + historyFallback: (opts: HistoryApiFallbackOptions) => void; + websocket: () => void; + four0four: (fn?: (ctx: Koa.Context) => void) => void; +} + +export interface WebpackPluginServeOptions { + client?: { + address?: string; + retry?: boolean; + silent?: boolean; + }; + compress?: boolean; + historyFallback?: boolean | HistoryApiFallbackOptions; + hmr?: boolean; + host?: string | Promise; + http2?: boolean | Http2ServerOptions | Http2SecureServerOptions; + https?: HttpsServerOptions; + liveReload?: boolean; + log?: { + level: 'trace' | 'debug' | 'info' | 'warn' | 'error'; + timestamp?: boolean; + }; + middleware?: (app: Koa, builtins: Builtins) => void; + open?: + | boolean + | { + wait?: boolean; + app?: string | ReadonlyArray; + }; + port?: number | Promise; + progress?: boolean | 'minimal'; + static?: string | string[]; + status?: boolean; + waitForBuild?: boolean; +} + +export class WebpackPluginServe { + constructor(opts?: WebpackPluginServeOptions); + attach(): { + apply(compiler: Compiler): void; + }; + hook(compiler: Compiler): void; + apply(compiler: Compiler): void; +} diff --git a/types/webpack-plugin-serve/tsconfig.json b/types/webpack-plugin-serve/tsconfig.json new file mode 100644 index 0000000000..8b70e1d1d4 --- /dev/null +++ b/types/webpack-plugin-serve/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "strictFunctionTypes": true, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webpack-plugin-serve-tests.ts" + ] +} diff --git a/types/webpack-plugin-serve/tslint.json b/types/webpack-plugin-serve/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/webpack-plugin-serve/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/webpack-plugin-serve/webpack-plugin-serve-tests.ts b/types/webpack-plugin-serve/webpack-plugin-serve-tests.ts new file mode 100644 index 0000000000..7abddcc600 --- /dev/null +++ b/types/webpack-plugin-serve/webpack-plugin-serve-tests.ts @@ -0,0 +1,39 @@ +import { WebpackPluginServe } from 'webpack-plugin-serve'; +import { Configuration } from 'webpack'; + +const usage = (config: Configuration) => { + (config.entry as string[]).push('webpack-plugin-serve/client'); + + config.watch = true; + + config.plugins!.push( + new WebpackPluginServe({ + compress: true, + historyFallback: true, + host: '0.0.0.0', + port: 3808, + liveReload: true, + middleware: (app: any, builtins: any) => + app.use(async (ctx: any, next: any) => { + await next(); + ctx.set('Access-Control-Allow-Headers', '*'); + ctx.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + ctx.set('Access-Control-Allow-Origin', '*'); + }), + static: '/', + + status: true, + progress: true, + }), + ); + + config.output!.publicPath = '/'; + + return config; +}; + +const baseConfig = { + entry: 'index.js' +}; + +const configWithServer = usage(baseConfig);