Upgrade to v5.x

This commit is contained in:
miZyind
2018-06-04 04:07:32 +08:00
parent e4a45e562b
commit b956900bc1
2 changed files with 53 additions and 37 deletions

View File

@@ -1,30 +1,35 @@
// Type definitions for koa-webpack 1.0
// Type definitions for koa-webpack 5.x
// Project: https://github.com/shellscape/koa-webpack#readme
// Definitions by: Luka Maljic <https://github.com/malj>
// Lee Benson <https://github.com/leebenson>
// miZyind <https://github.com/miZyind>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import Koa = require('koa');
import webpack = require('webpack');
import webpackDevMiddleware = require('webpack-dev-middleware');
import webpackHotMiddleware = require('webpack-hot-middleware');
import connect = require('connect');
import webpackHotClient = require('webpack-hot-client');
declare function koaWebpack(
options?: koaWebpack.Options
): Koa.Middleware & koaWebpack.CombinedWebpackMiddleware;
): Promise<Koa.Middleware & koaWebpack.CombinedWebpackMiddleware>;
declare namespace koaWebpack {
interface Options {
compiler?: webpack.Compiler;
config?: webpack.Configuration;
dev?: webpackDevMiddleware.Options;
hot?: webpackHotMiddleware.Options | boolean;
devMiddleware?: webpackDevMiddleware.Options;
hotClient?: webpackHotClient.Options | boolean;
}
interface CombinedWebpackMiddleware {
dev: connect.NextHandleFunction & webpackDevMiddleware.WebpackDevMiddleware;
hot: connect.NextHandleFunction & webpackHotMiddleware.EventStream;
devMiddleware: webpackDevMiddleware.WebpackDevMiddleware;
hotClient: {
close: () => void;
options: webpackHotClient.Options;
server: any;
};
}
}

View File

@@ -7,39 +7,50 @@ const config: webpack.Configuration = {};
const compiler = webpack(config);
// Using the middleware
const middleware = koaWebpack({
koaWebpack({
compiler,
config,
dev: {
lazy: true,
watchOptions: {
aggregateTimeout: 300,
poll: true,
},
publicPath: '/assets/',
// Reference: https://github.com/webpack/webpack-dev-middleware#options
devMiddleware: {
headers: { 'X-Custom-Header': 'yes' },
index: 'index.html',
headers: {
'X-Custom-Header': 'yes'
},
stats: {
colors: true,
},
lazy: false,
logger: undefined,
logLevel: 'info',
// logTime: false,
// mimeTypes: null,
publicPath: '/assets/',
reporter: null,
serverSideRender: false
serverSideRender: false,
stats: { context: process.cwd() },
watchOptions: { aggregateTimeout: 200 },
// writeToDisk: false
},
hot: {
log: console.log.bind(console),
path: '/__what',
heartbeat: 2000
// Reference: https://github.com/webpack-contrib/webpack-hot-client#api
hotClient: {
allEntries: false,
autoConfigure: true,
host: 'localhost',
hmr: true,
https: false,
logLevel: 'info',
logTime: false,
port: 0,
reload: true,
server: undefined,
stats: { context: process.cwd() }
}
});
})
.then((middleware) => {
app.use(middleware);
app.use(middleware);
// Accessing the underlying middleware
middleware.dev.close();
middleware.dev.invalidate();
middleware.dev.waitUntilValid();
middleware.hot.publish(null);
// Accessing the underlying middleware
middleware.devMiddleware.close();
middleware.devMiddleware.invalidate();
middleware.devMiddleware.waitUntilValid();
middleware.devMiddleware.getFilenameFromUrl('/public/index.html');
middleware.devMiddleware.fileSystem;
middleware.hotClient.close();
middleware.hotClient.options;
middleware.hotClient.server;
});