diff --git a/types/webpack-serve/index.d.ts b/types/webpack-serve/index.d.ts index 19983e6303..48fdfcbf38 100644 --- a/types/webpack-serve/index.d.ts +++ b/types/webpack-serve/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for webpack-serve 1.0 +// Type definitions for webpack-serve 2.0 // Project: https://github.com/webpack-contrib/webpack-serve // Definitions by: Ryan Clark // Jokcy @@ -13,85 +13,107 @@ import * as webpack from 'webpack'; import * as webpackDevMiddleware from 'webpack-dev-middleware'; import * as webpackHotClient from 'webpack-hot-client'; import * as https from 'https'; - -export = WebpackServe; +import * as net from 'net'; declare module 'webpack' { - interface Configuration { - serve?: WebpackServe.Options; - } + interface Configuration { + serve?: WebpackServe.Options; + } } -declare function WebpackServe(options: WebpackServe.Options): Promise; +declare function WebpackServe(argv: object, options: WebpackServe.Options): Promise; declare namespace WebpackServe { - interface WebpackServeOpen { - /** Name of the browser to open */ - app?: string; - /** Path on the server to open */ - path?: string; - } + interface Result { + /** An instance of a Koa application, extended with a server property, and stop method, which is used to programatically stop the server */ + app: InitializedKoa; - interface WebpackServeMiddleware { - /** Function to call to add koa-static */ - content: () => koa.Middleware; - /** Function to call to add the webpack-dev-middleware */ - webpack: () => webpackDevMiddleware.WebpackDevMiddleware; - } + /** A function which binds a serve event-name to a function */ + on(type: K, callback: (args: EventMap[K]) => void): void; - interface Instance { - /** Subscriber function for events */ - on(event: 'build-started', listener: (args: { compiler: webpack.Compiler }) => void): this; - on(event: 'build-finished', listener: (args: { compiler: webpack.Compiler, stats: webpack.Stats }) => void): this; - on(event: 'compiler-error' | 'compiler-warning', listener: (args: { compiler: webpack.Compiler, stats: any }) => void): this; - on(event: 'listening', listener: (args: { server: koa, options: NormalisedOptions }) => void): this; + /** Access to a frozen copy of the internal options object used by the module. */ + options: InitializedOptions; + } - /** Close webpack-serve */ - close: () => void; - } + interface Options { + /** Addon to webpack-serve that allows access to the Koa server instance */ + add?: (app: InitializedKoa, middleware: Middleware, options: Options) => void; - interface ListenersObject { - 'build-started': (args: { compiler: webpack.Compiler }) => void; - 'build-finished': (args: { compiler: webpack.Compiler, stats: webpack.Stats }) => void; - 'compiler-error': (args: { compiler: webpack.Compiler, stats: any }) => void; - 'compiler-warning': (args: { compiler: webpack.Compiler, stats: any }) => void; - 'listening': (args: { server: koa, options: NormalisedOptions }) => void; - } + /** Custom instance of a webpack compiler */ + compiler?: webpack.Compiler; - interface Options { - /** Addon to webpack-serve that allows access to the Koa server instance */ - add?: (app: koa, middleware: WebpackServeMiddleware, options: Options) => void; - /** Copy the server URL to the clipboard when the server is started */ - clipboard?: boolean; - /** Custom instance of a webpack compiler */ - compiler?: webpack.Compiler; - /** Webpack configuration for creating a new webpack compiler instance */ - config?: webpack.Configuration; - /** A path or array of paths where content will be served from */ - content?: string | string[]; - /** Options for webpack-dev-middleware */ - dev?: webpackDevMiddleware.Options; - /** The host the server will listen on */ - host?: string; - /** Options for webpack-hot-client */ - hot?: webpackHotClient.Options | boolean; - /** Enable HTTP2 support */ - http2?: boolean; - /** Configuration object for the server to use HTTPS */ - https?: https.ServerOptions; - /** Level of information for webpack-serve to output */ - logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent'; - /** Prepend timestamp to each log line */ - logTime?: boolean; - /** Object of subscribers to webpack-serve bus events */ - on?: ListenersObject; - /** Open the browser when started */ - open?: WebpackServeOpen | boolean; - /** Port that the server listens on */ - port?: number; - } + /** Webpack configuration for creating a new webpack compiler instance */ + config?: webpack.Configuration; - interface NormalisedOptions extends Options { - protocol: 'http' | 'https'; - } + /** A path or array of paths where content will be served from */ + content?: string | string[]; + + /** Copy the server URL to the clipboard when the server is started */ + clipboard?: boolean; + + /** Options for webpack-dev-middleware */ + devMiddleware?: webpackDevMiddleware.Options; + + /** The host the server will listen on */ + host?: string; + + /** Options for webpack-hot-client */ + hotClient?: webpackHotClient.Options | boolean; + + /** Enable HTTP2 support */ + http2?: boolean; + + /** Configuration object for the server to use HTTPS */ + https?: https.ServerOptions; + + /** Level of information for webpack-serve to output */ + logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent'; + + /** Prepend timestamp to each log line */ + logTime?: boolean; + + /** Object of subscribers to webpack-serve bus events */ + on?: OnOptions; + + /** Open the browser when started */ + open?: OpenOptions | boolean; + + /** Port that the server listens on */ + port?: number; + } + + interface OpenOptions { + /** Name of the browser to open */ + app?: string; + /** Path on the server to open */ + path?: string; + } + + type OnOptions = { [K in keyof EventMap]?: (args: EventMap[K]) => void }; + + interface InitializedKoa extends koa { + server: net.Server; + stop: () => void; + } + + interface InitializedOptions extends Options { + protocol: 'http' | 'https'; + } + + interface Middleware { + /** Function to call to add koa-static */ + content: () => void; + /** Function to call to add the webpack-dev-middleware */ + webpack: () => void; + } + + interface EventMap { + 'build-started': { compiler: webpack.Compiler }; + 'build-finished': { compiler: webpack.Compiler; stats: webpack.Stats }; + 'compiler-error': { compiler: webpack.Compiler; stats: any }; + 'compiler-warning': { compiler: webpack.Compiler; stats: any }; + listening: { server: net.Server; options: InitializedOptions }; + } } + +export = WebpackServe; diff --git a/types/webpack-serve/tsconfig.json b/types/webpack-serve/tsconfig.json index 11c218b9c9..696f2d44f5 100644 --- a/types/webpack-serve/tsconfig.json +++ b/types/webpack-serve/tsconfig.json @@ -1,23 +1,23 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "webpack-serve-tests.ts" - ] + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webpack-serve-tests.ts" + ] } diff --git a/types/webpack-serve/v1/index.d.ts b/types/webpack-serve/v1/index.d.ts new file mode 100644 index 0000000000..19983e6303 --- /dev/null +++ b/types/webpack-serve/v1/index.d.ts @@ -0,0 +1,97 @@ +// Type definitions for webpack-serve 1.0 +// Project: https://github.com/webpack-contrib/webpack-serve +// Definitions by: Ryan Clark +// Jokcy +// ZSkycat +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +import * as koa from 'koa'; +import * as webpack from 'webpack'; +import * as webpackDevMiddleware from 'webpack-dev-middleware'; +import * as webpackHotClient from 'webpack-hot-client'; +import * as https from 'https'; + +export = WebpackServe; + +declare module 'webpack' { + interface Configuration { + serve?: WebpackServe.Options; + } +} + +declare function WebpackServe(options: WebpackServe.Options): Promise; + +declare namespace WebpackServe { + interface WebpackServeOpen { + /** Name of the browser to open */ + app?: string; + /** Path on the server to open */ + path?: string; + } + + interface WebpackServeMiddleware { + /** Function to call to add koa-static */ + content: () => koa.Middleware; + /** Function to call to add the webpack-dev-middleware */ + webpack: () => webpackDevMiddleware.WebpackDevMiddleware; + } + + interface Instance { + /** Subscriber function for events */ + on(event: 'build-started', listener: (args: { compiler: webpack.Compiler }) => void): this; + on(event: 'build-finished', listener: (args: { compiler: webpack.Compiler, stats: webpack.Stats }) => void): this; + on(event: 'compiler-error' | 'compiler-warning', listener: (args: { compiler: webpack.Compiler, stats: any }) => void): this; + on(event: 'listening', listener: (args: { server: koa, options: NormalisedOptions }) => void): this; + + /** Close webpack-serve */ + close: () => void; + } + + interface ListenersObject { + 'build-started': (args: { compiler: webpack.Compiler }) => void; + 'build-finished': (args: { compiler: webpack.Compiler, stats: webpack.Stats }) => void; + 'compiler-error': (args: { compiler: webpack.Compiler, stats: any }) => void; + 'compiler-warning': (args: { compiler: webpack.Compiler, stats: any }) => void; + 'listening': (args: { server: koa, options: NormalisedOptions }) => void; + } + + interface Options { + /** Addon to webpack-serve that allows access to the Koa server instance */ + add?: (app: koa, middleware: WebpackServeMiddleware, options: Options) => void; + /** Copy the server URL to the clipboard when the server is started */ + clipboard?: boolean; + /** Custom instance of a webpack compiler */ + compiler?: webpack.Compiler; + /** Webpack configuration for creating a new webpack compiler instance */ + config?: webpack.Configuration; + /** A path or array of paths where content will be served from */ + content?: string | string[]; + /** Options for webpack-dev-middleware */ + dev?: webpackDevMiddleware.Options; + /** The host the server will listen on */ + host?: string; + /** Options for webpack-hot-client */ + hot?: webpackHotClient.Options | boolean; + /** Enable HTTP2 support */ + http2?: boolean; + /** Configuration object for the server to use HTTPS */ + https?: https.ServerOptions; + /** Level of information for webpack-serve to output */ + logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent'; + /** Prepend timestamp to each log line */ + logTime?: boolean; + /** Object of subscribers to webpack-serve bus events */ + on?: ListenersObject; + /** Open the browser when started */ + open?: WebpackServeOpen | boolean; + /** Port that the server listens on */ + port?: number; + } + + interface NormalisedOptions extends Options { + protocol: 'http' | 'https'; + } +} diff --git a/types/webpack-serve/v1/tsconfig.json b/types/webpack-serve/v1/tsconfig.json new file mode 100644 index 0000000000..1a76820546 --- /dev/null +++ b/types/webpack-serve/v1/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "webpack-serve": ["webpack-serve/v1"] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "webpack-serve-tests.ts" + ] +} diff --git a/types/webpack-serve/v1/tslint.json b/types/webpack-serve/v1/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/webpack-serve/v1/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/webpack-serve/v1/webpack-serve-tests.ts b/types/webpack-serve/v1/webpack-serve-tests.ts new file mode 100644 index 0000000000..c2e2cf04af --- /dev/null +++ b/types/webpack-serve/v1/webpack-serve-tests.ts @@ -0,0 +1,64 @@ +import webpack = require('webpack'); +import serve = require('webpack-serve'); + +const config: webpack.Configuration = { + mode: 'development', + entry: ['index.js'], // when use compiler entry must be array or object +}; + +const serveConfig = { + http2: true, + dev: { + publicPath: '/', + logLevel: 'info' + }, + host: 'localhost' +}; + +const compiler = webpack(config); + +const server = serve({ + compiler, + ...serveConfig +}); + +server + .then((server) => { + server.on('listening', () => { + server.close(); + }); + }); + +const config2: webpack.Configuration = { + ...config, + serve: { + ...serveConfig, + port: 8888, + hot: { + port: 8889 + } + }, +}; + +const server2 = serve({ + config: config2 +}); + +server2 + .then((server) => { + server.on('listening', () => { + server.close(); + }); + }); + +const server3 = serve({ + logLevel: 'silent', + config: config2 +}); + +server3 + .then((server) => { + server.on('listening', () => { + server.close(); + }); + }); diff --git a/types/webpack-serve/webpack-serve-tests.ts b/types/webpack-serve/webpack-serve-tests.ts index c2e2cf04af..796d977450 100644 --- a/types/webpack-serve/webpack-serve-tests.ts +++ b/types/webpack-serve/webpack-serve-tests.ts @@ -1,64 +1,49 @@ import webpack = require('webpack'); import serve = require('webpack-serve'); -const config: webpack.Configuration = { - mode: 'development', - entry: ['index.js'], // when use compiler entry must be array or object +const webpackConfig: webpack.Configuration = { + entry: { index: './index.js' }, }; -const serveConfig = { - http2: true, - dev: { - publicPath: '/', - logLevel: 'info' - }, - host: 'localhost' +const webpackCompiler = webpack(webpackConfig); + +const serveConfig: serve.Options = { + add: (app, middleware, options) => { + middleware.content(); + middleware.webpack(); + }, + compiler: webpackCompiler, + config: webpackConfig, + content: './webroot', + clipboard: true, + devMiddleware: { + logLevel: 'warn', + publicPath: '/', + }, + host: 'localhost', + hotClient: { + logLevel: 'warn', + allEntries: true, + }, + http2: true, + https: {}, + logLevel: 'info', + logTime: false, + on: { + 'build-finished': args => { + console.log(args.stats.toString()); + }, + }, + open: { + path: '/index.html', + }, + port: 65080, }; -const compiler = webpack(config); +const promise = serve({}, serveConfig); -const server = serve({ - compiler, - ...serveConfig -}); - -server - .then((server) => { - server.on('listening', () => { - server.close(); +promise.then(result => { + result.on('compiler-error', args => { + console.log(args.stats); }); - }); - -const config2: webpack.Configuration = { - ...config, - serve: { - ...serveConfig, - port: 8888, - hot: { - port: 8889 - } - }, -}; - -const server2 = serve({ - config: config2 }); - -server2 - .then((server) => { - server.on('listening', () => { - server.close(); - }); - }); - -const server3 = serve({ - logLevel: 'silent', - config: config2 -}); - -server3 - .then((server) => { - server.on('listening', () => { - server.close(); - }); - });