const debug = require('debug')('crowi:crowi:dev'); const path = require('path'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const webpackHotMiddleware = require('webpack-hot-middleware'); const reload = require('reload'); const chokidar = require('chokidar'); class CrowiDev { /** * Creates an instance of CrowiDev. * @param {Crowi} crowi * @param {any} server http server * @param {any} app express * * @memberOf CrowiDev */ constructor(crowi, server, app) { this.crowi = crowi; this.server = server; this.app = app; } setupTools() { this.setupWebpackHMR(); this.setupReloadServer(); } setupWebpackHMR() { // Webpack HMR settings const configPath = path.join(this.crowi.rootDir, 'webpack.config'); const webpackConfig = require(configPath); var compiler = webpack(webpackConfig); this.app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: webpackConfig.output.publicPath })); this.app.use(webpackHotMiddleware(compiler)); } setupReloadServer() { // reload settings // see: https://github.com/glenjamin/webpack-hot-middleware // see: https://github.com/jprichardson/reload const reloadServer = reload(this.server, this.app); // fire reload() when views are modified const watcher = chokidar.watch(path.join(this.crowi.viewsDir)); watcher.on('all', (event, path) => { reloadServer.reload(); }); debug(`watching for live-reloading -> ${this.crowi.libDir}`); } } module.exports = CrowiDev