dev.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import webpack from 'webpack'
  15. import webpackConfig from '../webpack.config'
  16. module.exports = (app, PORT) => {
  17. let isBrowserOpen = false
  18. const compiler = webpack(webpackConfig)
  19. app.use(require('webpack-dev-middleware')(compiler, {
  20. noInfo: false,
  21. publicPath: webpackConfig.output.publicPath,
  22. stats: {
  23. colors: true,
  24. },
  25. log: text => {
  26. let statusIndex = text.indexOf('webpack: Compiled') > -1
  27. ? text.indexOf('webpack: Compiled') : text.indexOf('webpack: Failed')
  28. if (statusIndex > -1) {
  29. let left = text.substr(0, statusIndex)
  30. let isSuccesfull = text.indexOf('webpack: Compiled successfully.') > -1
  31. let color = text.indexOf('webpack: Compiled with warnings.') > -1 ? '\x1b[43m\x1b[30m' : ''
  32. color = isSuccesfull ? '\x1b[42m\x1b[30m' : color
  33. color = text.indexOf('webpack: Failed to compile.') > -1 ? '\x1b[41m' : color
  34. let end = `${color + text.substr(statusIndex)}\x1b[0m`
  35. console.log(left + end) // eslint-disable-line no-console
  36. if (!isBrowserOpen && isSuccesfull) {
  37. isBrowserOpen = true
  38. console.log(`\x1b[96mServer is available at http://localhost:${PORT}\x1b[0m`) // eslint-disable-line no-console
  39. }
  40. } else {
  41. console.log(text)// eslint-disable-line no-console
  42. }
  43. },
  44. }))
  45. app.use(require('webpack-hot-middleware')(compiler, {
  46. log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000, // eslint-disable-line no-console
  47. }))
  48. }