dev.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const debug = require('debug')('crowi:crowi:dev');
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const webpackDevMiddleware = require('webpack-dev-middleware');
  5. const webpackHotMiddleware = require('webpack-hot-middleware');
  6. const reload = require('reload');
  7. const chokidar = require('chokidar');
  8. class CrowiDev {
  9. /**
  10. * Creates an instance of CrowiDev.
  11. * @param {Crowi} crowi
  12. * @param {any} server http server
  13. * @param {any} app express
  14. *
  15. * @memberOf CrowiDev
  16. */
  17. constructor(crowi, server, app) {
  18. this.crowi = crowi;
  19. this.server = server;
  20. this.app = app;
  21. }
  22. setupTools() {
  23. this.setupWebpackHMR();
  24. this.setupReloadServer();
  25. }
  26. setupWebpackHMR() {
  27. // Webpack HMR settings
  28. const webpackConfig = require('../../webpack.config');
  29. var compiler = webpack(webpackConfig);
  30. this.app.use(webpackDevMiddleware(compiler, {
  31. noInfo: true, publicPath: webpackConfig.output.publicPath
  32. }));
  33. this.app.use(webpackHotMiddleware(compiler));
  34. }
  35. setupReloadServer() {
  36. // reload settings
  37. // see: https://github.com/glenjamin/webpack-hot-middleware
  38. // see: https://github.com/jprichardson/reload
  39. const reloadServer = reload(this.server, this.app);
  40. // fire reload() when views are modified
  41. const watcher = chokidar.watch(path.join(this.crowi.libDir, 'views'));
  42. watcher.on('all', (event, path) => {
  43. reloadServer.reload();
  44. });
  45. debug(`watching for live-reloading -> ${this.crowi.libDir}`);
  46. }
  47. }
  48. module.exports = CrowiDev