yacd/server.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-10-20 20:32:02 +08:00
'use strict';
const path = require('path');
const config = require('./webpack.config');
const webpack = require('webpack');
const express = require('express');
const app = express();
const devMiddleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const { PORT } = process.env;
const port = PORT ? Number(PORT) : 3000;
config.entry.app.unshift('webpack-hot-middleware/client');
2018-10-20 20:32:02 +08:00
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
// prints more readable module names in the browser console on HMR updates
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin()
2018-10-20 20:32:02 +08:00
);
const compiler = webpack(config);
// webpack-dev-server config
const publicPath = config.output.publicPath;
const stats = {
colors: true,
version: false,
modulesSort: 'issuer',
assets: false,
2018-10-20 20:32:02 +08:00
cached: false,
cachedAssets: false,
chunks: false,
chunkModules: false
};
const options = { publicPath, stats };
const wdm = devMiddleware(compiler, options);
const whm = hotMiddleware(compiler);
app.use(wdm);
app.use(whm);
2018-10-20 20:32:02 +08:00
2019-04-17 00:20:42 +08:00
app.use('*', (_req, res, next) => {
2018-10-20 20:32:02 +08:00
const filename = path.join(compiler.outputPath, 'index.html');
compiler.outputFileSystem.readFile(filename, (err, result) => {
if (err) return next(err);
res.set('content-type', 'text/html');
res.send(result);
res.end();
});
});
2019-04-17 00:20:42 +08:00
const host = '0.0.0.0';
app.listen(port, host, () => {
console.log(`>> Listening at http://${host}:${port}`);
2018-10-20 20:32:02 +08:00
});
wdm.waitUntilValid(() => {
2019-04-17 00:20:42 +08:00
console.log(
`
>> Build ready at:
http://${host}:${port}
http://127.0.0.1:${port}
http://localhost:${port}
`
);
});