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
This commit is contained in:
Matheus Gonçalves da Silva 2019-03-25 13:34:48 -03:00 committed by Sheetal Nandi
parent 0cbf2d2865
commit a4e52c5eb1
6 changed files with 162 additions and 30 deletions

View File

@ -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;
}
}

View File

@ -16,12 +16,18 @@
/// <reference types="node" />
/// <reference types="koa" />
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;
}

69
types/webpack-plugin-serve/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/PlayMa256>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="node" />
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<string>;
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<string>;
};
port?: number | Promise<number>;
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;
}

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -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);