dev.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. setupLiveReloadTools() {
  23. // Webpack HMR settings
  24. const webpackConfig = require('../../webpack.config');
  25. var compiler = webpack(webpackConfig);
  26. this.app.use(webpackDevMiddleware(compiler, {
  27. noInfo: true, publicPath: webpackConfig.output.publicPath
  28. }));
  29. this.app.use(webpackHotMiddleware(compiler));
  30. // reload settings
  31. // see: https://github.com/glenjamin/webpack-hot-middleware
  32. // see: https://github.com/jprichardson/reload
  33. const reloadServer = reload(this.server, this.app);
  34. // fire reload() when views are modified
  35. const watcher = chokidar.watch(path.join(this.crowi.libDir, 'views'));
  36. watcher.on('all', (event, path) => {
  37. reloadServer.reload();
  38. });
  39. // debug(`watching for live-reloading -> ${this.crowi.libDir}`);
  40. }
  41. }
  42. module.exports = CrowiDev