DefinitelyTyped/types/webpack-dev-server/webpack-dev-server-tests.ts
James Bromwell 15d979e92a [webpack-dev-server] add tests for Stats, only allow specific strings (#28788)
Please fill in this template.

- [X] Use a meaningful title for the pull request. Include the name of the package modified.
- [X] Test the change in your own code. (Compile and run.)
- [X] Add or edit tests to reflect the change. (Run with `npm test`.)
- [X] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request).
- [X] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes).
- [X] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present).

Select one of these and delete the others:

If changing an existing definition:
- [X] Provide a URL to documentation or source code which provides context for the suggested changes: https://webpack.js.org/configuration/stats/#stats
- [ ] Increase the version number in the header if appropriate.
- [ ] If you are making substantial changes, consider adding a `tslint.json` containing `{ "extends": "dtslint/dt.json" }`.


Ref #28362
2018-09-17 00:37:38 -07:00

129 lines
3.4 KiB
TypeScript

import webpack = require('webpack');
import WebpackDevServer = require('webpack-dev-server');
import { Application } from 'express';
const compiler = webpack({});
const multipleCompiler = webpack([]);
// basic example
let server = new WebpackDevServer(compiler, {
publicPath: "/assets/"
});
server.listen(8080);
// Configuration can be used as a type
const config: WebpackDevServer.Configuration = {
// webpack-dev-server options
inline: true,
// Toggle between the dev-server's two different modes --- inline (default, recommended for HMR) or iframe.
contentBase: "/path/to/directory",
// or: contentBase: "http://localhost/",
public: 'public-host.ru',
// Public host for server
disableHostCheck: true,
// Disable public host check, use it carefully
hot: true,
// Enable special support for Hot Module Replacement
// Page is no longer updated, but a "webpackHotUpdate" message is send to the content
// Use "webpack/hot/dev-server" as additional module in your entry point
// Note: this does _not_ add the `HotModuleReplacementPlugin` like the CLI option does.
// Set this as true if you want to access dev server from arbitrary url.
// This is handy if you are using a html5 router.
historyApiFallback: false,
// Set this if you want to enable gzip compression for assets
compress: true,
// Set this if you want webpack-dev-server to delegate a single path to an arbitrary server.
// Use "**" to proxy all paths to the specified server.
// This is useful if you want to get rid of 'http://localhost:8080/' in script[src],
// and has many other use cases (see https://github.com/webpack/webpack-dev-server/pull/127 ).
proxy: {
"**": "http://localhost:9090"
},
setup: (app: Application) => {
// Here you can access the Express app object and add your own custom middleware to it.
// For example, to define custom handlers for some paths:
app.get('/some/path', (req, res) => {
res.json({ custom: 'response' });
});
},
// pass [static options](http://expressjs.com/en/4x/api.html#express.static) to inner express server
staticOptions: {
},
stats: {
assets: false,
warningsFilter: /1/,
},
// webpack-dev-middleware options
quiet: false,
noInfo: false,
lazy: true,
filename: "bundle.js",
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
// It's a required option.
publicPath: "/assets/",
headers: { "X-Custom-Header": "yes" }
};
const c2: WebpackDevServer.Configuration = {
stats: false,
};
const c3: WebpackDevServer.Configuration = {
stats: "verbose",
};
// API example
server = new WebpackDevServer(compiler, config);
server.listen(8080, "localhost", () => { });
// HTTPS example
server = new WebpackDevServer(compiler, {
publicPath: "/assets/",
https: true
});
server.listen(8080, "localhost", () => { });
server.close();
const webpackConfig: webpack.Configuration = {
context: __dirname,
mode: 'development',
target: 'node',
devServer: config
};
WebpackDevServer.addDevServerEntrypoints(webpackConfig, {
publicPath: "/assets/",
https: true
});
WebpackDevServer.addDevServerEntrypoints(
[webpackConfig],
{
publicPath: "/assets/",
https: true
},
{
address: () => ({ port: 80 })
}
);
// multiple compilers
server = new WebpackDevServer(multipleCompiler, config);