-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.dev.ts
50 lines (43 loc) · 1.31 KB
/
webpack.dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as express from 'express';
import * as path from 'path';
import { merge } from 'webpack-merge';
import * as Webpack from 'webpack';
import * as WebpackDevServer from 'webpack-dev-server';
import common from './webpack.common';
import * as Session from './server/session';
const devConfig: Webpack.Configuration = {
mode: 'development',
devtool: 'eval-source-map',
devServer: {
static: './public',
port: 8080,
setupMiddlewares: (
middlewares: WebpackDevServer.Middleware[],
devServer: WebpackDevServer
) => {
setupDev(devServer);
return middlewares;
},
},
};
const setupDev = async (devServer: WebpackDevServer) => {
const app = devServer.app;
if (!app) {
throw new Error('webpack-dev-server is not defined');
}
const compiler = devServer.compiler;
await Session.setupSession(app);
app.use('*', (req: express.Request, res: express.Response) => {
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem?.readFile(filename, (err: any, result: any) => {
if (err) {
res.status(404).sendFile(path.resolve(__dirname, 'public/error.html'));
return;
}
res.set('Content-Type', 'text/html');
res.send(result);
res.end();
});
});
};
export default merge(common, devConfig);