-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfrontendMiddleware.js
99 lines (86 loc) · 3.02 KB
/
frontendMiddleware.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-disable global-require */
const express = require('express');
const path = require('path');
const compression = require('compression');
const pkg = require(path.resolve(process.cwd(), 'package.json'));
// Dev middleware
const addDevMiddlewares = (app, webpackConfig) => {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compiler = webpack(webpackConfig);
const middleware = webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
silent: true,
stats: 'errors-only'
});
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
// Since webpackDevMiddleware uses memory-fs internally to store build
// artifacts, we use it instead
const fs = middleware.fileSystem;
// if (pkg.dllPlugin) {
// app.get(/\.dll\.js$/, (req, res) => {
// const filename = req.path.replace(/^\//, '');
// res.sendFile(path.join(process.cwd(), pkg.dllPlugin.path, filename));
// });
// }
app.get(/(.json)$/, (req, res) => {
const filename = req.path.replace(/^\//, '');
res.sendFile(path.join(compiler.outputPath, filename));
});
app.get(/td-sdk.min.js/, (req, res) => {
const filename = req.path.replace(/^\//, '');
res.sendFile(path.join(process.cwd(), filename));
});
app.get('/td-sdk.js', (req, res) => {
fs.readFile(path.join(compiler.outputPath, 'td-sdk.js'), (err, file) => {
if (err) {
res.sendStatus(404);
} else {
res.send(file.toString());
}
});
});
app.get(/(.js|.css|.gif|.png)$/, (req, res) => {
const filename = req.path.replace(/^\//, '');
res.sendFile(path.join(process.cwd(), 'dev/playground', filename));
});
app.get('*', (req, res) => {
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
res.sendStatus(404);
} else {
res.send(file.toString());
}
});
});
};
// Production middlewares
// const addProdMiddlewares = (app, options) => {
// const publicPath = options.publicPath || '/';
// const outputPath = options.outputPath || path.resolve(process.cwd(), 'build');
//
// // compression middleware compresses your server responses which makes them
// // smaller (applies also to assets). You can read more about that technique
// // and other good practices on official Express.js docs http://mxs.is/googmy
// app.use(compression());
// app.use(publicPath, express.static(outputPath));
//
// app.get('*', (req, res) => res.sendFile(path.resolve(outputPath, 'index.html')));
// };
/**
* Front-end middleware
*/
module.exports = (app, options) => {
const isProd = process.env.NODE_ENV === 'production';
if (isProd) {
addProdMiddlewares(app, options);
} else {
var webpackConfig = require('./config/webpack.config');
//const webpackConfig = require('../../internals/webpack/webpack.dev.babel');
addDevMiddlewares(app, webpackConfig);
}
return app;
};