UNPKG

3.77 kBTypeScriptView Raw
1// Type definitions for webpack-dev-middleware 5.0
2// Project: https://github.com/webpack/webpack-dev-middleware
3// Definitions by: Benjamin Lim <https://github.com/bumbleblym>
4// reduckted <https://github.com/reduckted>
5// Chris Abrams <https://github.com/chrisabrams>
6// Piotr Błażejewicz <https://github.com/peterblazejewicz>
7// Ma Tianqi <https://github.com/tqma113>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 3.7
10
11import * as webpack from "webpack";
12import { IncomingMessage, NextHandleFunction } from "connect";
13import { ServerResponse } from "http";
14
15export = WebpackDevMiddleware;
16
17declare function WebpackDevMiddleware(
18 compiler: webpack.Compiler | webpack.MultiCompiler,
19 options?: WebpackDevMiddleware.Options,
20): WebpackDevMiddleware.WebpackDevMiddleware & NextHandleFunction;
21
22declare namespace WebpackDevMiddleware {
23 interface Options {
24 /**
25 * This property allows a user to register custom mime types or extension mappings
26 */
27 mimeTypes?: MimeTypeMap;
28 /**
29 * If true, the option will instruct the module to write files to the configured location on disk as specified in your webpack config file
30 * This option also accepts a Function value, which can be used to filter which files are written to disk
31 */
32 writeToDisk?: boolean | ((filename: string) => boolean);
33 /**
34 * This property allows a user to pass the list of HTTP request methods accepted by the server.
35 * @default [ 'GET', 'HEAD' ]
36 */
37 methods?: string[];
38 /** This property allows a user to pass custom HTTP headers on each request. eg. { "X-Custom-Header": "yes" } */
39 headers?: Record<string, string> | HeadersHandle;
40 /** The public path that the middleware is bound to */
41 publicPath?: string;
42 /** Instructs the module to enable or disable the server-side rendering mode */
43 serverSideRender?: boolean;
44 /** Stats options object or preset name. */
45 stats?: webpack.Configuration['stats'];
46 /**
47 * Set the default file system which will be used by webpack as primary destination of generated files
48 */
49 outputFileSystem?: webpack.Compiler['outputFileSystem'];
50 /**
51 * The index path for web server, defaults to "index.html".
52 * If falsy (but not undefined), the server will not respond to requests to the root URL.
53 */
54 index?: string | boolean;
55 }
56
57 interface MimeTypeMap {
58 [type: string]: string;
59 }
60
61 type Callback = (stats?: webpack.Stats) => void;
62
63 interface Context {
64 state: boolean;
65 stats: webpack.Stats | null;
66 callbacks: Callback[];
67 options: Options;
68 compiler: webpack.Compiler;
69 watching: webpack.Watching | null;
70 }
71
72 interface WebpackDevMiddleware {
73 /** Executes a callback function when the compiler bundle is valid, typically after compilation */
74 waitUntilValid(callback?: Callback): void;
75 /** Instructs a webpack-dev-middleware instance to recompile the bundle. e.g. after a change to the configuration. */
76 invalidate(callback?: Callback): void;
77 /** A function executed once the middleware has stopped watching. */
78 close(callback?: () => void): void;
79 /**
80 * Get filename from URL.
81 * @param url URL for the requested file.
82 */
83 getFilenameFromUrl(url: string): string | undefined;
84 context: Context;
85 }
86
87 interface HeadersHandle {
88 (req: IncomingMessage, res: ServerResponse, context: Context): Record<string, string> | void;
89 }
90}