dev.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 configPath = path.join(this.crowi.rootDir, 'webpack.config');
  29. const webpackConfig = require(configPath);
  30. var compiler = webpack(webpackConfig);
  31. this.app.use(webpackDevMiddleware(compiler, {
  32. noInfo: true, publicPath: webpackConfig.output.publicPath
  33. }));
  34. this.app.use(webpackHotMiddleware(compiler));
  35. }
  36. setupReloadServer() {
  37. // reload settings
  38. // see: https://github.com/glenjamin/webpack-hot-middleware
  39. // see: https://github.com/jprichardson/reload
  40. const reloadServer = reload(this.server, this.app);
  41. // fire reload() when views are modified
  42. const watcher = chokidar.watch(path.join(this.crowi.viewsDir));
  43. watcher.on('all', (event, path) => {
  44. reloadServer.reload();
  45. });
  46. debug(`watching for live-reloading -> ${this.crowi.libDir}`);
  47. }
  48. }
  49. module.exports = CrowiDev