2018-10-20 20:32:02 +08:00
|
|
|
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;
|
2020-11-07 23:42:41 +08:00
|
|
|
const publicPath = config.output.publicPath;
|
2018-10-20 20:32:02 +08:00
|
|
|
|
2020-10-11 19:31:32 +08:00
|
|
|
config.entry.app.import.unshift('webpack-hot-middleware/client');
|
2018-10-20 20:32:02 +08:00
|
|
|
config.plugins.push(
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
2019-01-05 00:05:35 +08:00
|
|
|
new webpack.NoEmitOnErrorsPlugin()
|
2018-10-20 20:32:02 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const compiler = webpack(config);
|
|
|
|
|
2020-11-07 23:42:41 +08:00
|
|
|
const wdm = devMiddleware(compiler, { publicPath });
|
2019-01-18 23:51:30 +08:00
|
|
|
const whm = hotMiddleware(compiler);
|
2020-11-07 23:42:41 +08:00
|
|
|
|
2019-01-18 23:51:30 +08:00
|
|
|
app.use(wdm);
|
|
|
|
app.use(whm);
|
2018-10-20 20:32:02 +08:00
|
|
|
|
2019-11-19 23:53:25 +08:00
|
|
|
app.get('/_dev', (_req, res) => {
|
2020-11-07 23:42:41 +08:00
|
|
|
const outputPath = wdm.getFilenameFromUrl(publicPath || '/');
|
2019-11-19 23:53:25 +08:00
|
|
|
const filesystem = wdm.fileSystem;
|
|
|
|
const content = filesystem.readdirSync(outputPath);
|
|
|
|
res.end(content.join('\n'));
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
2019-01-18 23:51:30 +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}
|
|
|
|
`
|
|
|
|
);
|
2019-01-18 23:51:30 +08:00
|
|
|
});
|