addDevServerEntrypoints.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. /* eslint no-param-reassign: 'off' */
  3. const createDomain = require('./createDomain');
  4. module.exports = function addDevServerEntrypoints(webpackOptions, devServerOptions, listeningApp) {
  5. if (devServerOptions.inline !== false) {
  6. // we're stubbing the app in this method as it's static and doesn't require
  7. // a listeningApp to be supplied. createDomain requires an app with the
  8. // address() signature.
  9. const app = listeningApp || {
  10. address() {
  11. return { port: devServerOptions.port };
  12. }
  13. };
  14. const domain = createDomain(devServerOptions, app);
  15. const devClient = [`${require.resolve('../../client/')}?${domain}`];
  16. if (devServerOptions.hotOnly) { devClient.push('webpack/hot/only-dev-server'); } else if (devServerOptions.hot) { devClient.push('webpack/hot/dev-server'); }
  17. const prependDevClient = (entry) => {
  18. if (typeof entry === 'function') {
  19. return () => Promise.resolve(entry()).then(prependDevClient);
  20. }
  21. if (typeof entry === 'object' && !Array.isArray(entry)) {
  22. const entryClone = {};
  23. Object.keys(entry).forEach((key) => {
  24. entryClone[key] = devClient.concat(entry[key]);
  25. });
  26. return entryClone;
  27. }
  28. return devClient.concat(entry);
  29. };
  30. [].concat(webpackOptions).forEach((wpOpt) => {
  31. wpOpt.entry = prependDevClient(wpOpt.entry);
  32. });
  33. }
  34. };