2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 11:55:46 +00:00
books/webpack/serve.js
Faris Ansari ddd9a786d2 Frappe CLI for development
- frappe start command
  - starts frappejs server
  - sets up webpack middleware to bundle files
- introduce frappe.conf.js for configuration
2018-07-29 16:51:03 +05:30

75 lines
2.1 KiB
JavaScript

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');
const 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 = [
resolveAppDir('node_modules/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
}