2018-07-29 11:21:03 +00:00
|
|
|
const webpack = require('webpack');
|
|
|
|
const webpackDevServer = require('webpack-dev-server');
|
|
|
|
const webpackDevMiddleware = require('webpack-dev-middleware');
|
|
|
|
const webpackHotMiddleware = require('webpack-hot-middleware');
|
|
|
|
|
|
|
|
const logger = require('./logger');
|
|
|
|
const { getAppConfig, resolveAppDir } = require('./utils');
|
2018-10-21 09:06:53 +00:00
|
|
|
const { getConfig: getWebpackConfig } = require('./config');
|
2018-07-29 11:21:03 +00:00
|
|
|
|
|
|
|
const log = logger('serve');
|
|
|
|
const warn = logger('serve', 'red');
|
|
|
|
|
|
|
|
const appConfig = getAppConfig();
|
|
|
|
const webpackConfig = getWebpackConfig();
|
|
|
|
|
|
|
|
function addWebpackMiddleware(app) {
|
|
|
|
log();
|
|
|
|
log('Starting dev server...');
|
|
|
|
|
|
|
|
addWebpackEntryPoints(webpackConfig);
|
|
|
|
const compiler = webpack(webpackConfig);
|
|
|
|
|
|
|
|
app.use(webpackDevMiddleware(compiler, {
|
|
|
|
logLevel: 'silent',
|
|
|
|
publicPath: webpackConfig.output.publicPath
|
|
|
|
}));
|
|
|
|
|
|
|
|
app.use(webpackHotMiddleware(compiler, {
|
|
|
|
path: '/__webpack_hmr'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function startWebpackDevServer() {
|
|
|
|
log();
|
|
|
|
log('Starting dev server...');
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
addWebpackEntryPoints(webpackConfig, true);
|
|
|
|
const compiler = webpack(webpackConfig);
|
|
|
|
const server = new webpackDevServer(compiler, webpackConfig.devServer);
|
|
|
|
|
|
|
|
const { devServerHost, devServerPort } = appConfig.dev;
|
|
|
|
server.listen(devServerPort, devServerHost, () => {
|
|
|
|
// listening on devServerPort
|
|
|
|
|
|
|
|
compiler.hooks.done.tap('webpack done compiling', function() {
|
|
|
|
resolve(server);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function addWebpackEntryPoints(webpackConfig, forDevServer) {
|
|
|
|
const devServerEntryPoints = [
|
2018-10-20 12:30:58 +00:00
|
|
|
// resolveAppDir('node_modules/webpack-dev-server/client/index.js') + '?http://localhost',
|
|
|
|
'webpack-dev-server/client/index.js?http://localhost',
|
2018-07-29 11:21:03 +00:00
|
|
|
'webpack/hot/dev-server'
|
|
|
|
];
|
|
|
|
const middlewareEntryPoints = [
|
|
|
|
'webpack-hot-middleware/client?path=/__webpack_hmr'
|
|
|
|
];
|
|
|
|
const entryPoints = forDevServer ? devServerEntryPoints : middlewareEntryPoints;
|
|
|
|
const entry = webpackConfig.entry;
|
|
|
|
|
|
|
|
Object.keys(entry).forEach(key => {
|
|
|
|
entry[key] = [...entryPoints, entry[key]];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addWebpackMiddleware,
|
|
|
|
startWebpackDevServer
|
|
|
|
}
|