2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 12:28:12 +00:00
books/webpack/serve.js

76 lines
2.2 KiB
JavaScript
Raw Normal View History

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');
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',
'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
}