addEntries.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. const webpack = require('webpack');
  3. const createDomain = require('./createDomain');
  4. function addEntries(config, options, server) {
  5. if (options.inline !== false) {
  6. // we're stubbing the app in this method as it's static and doesn't require
  7. // a server to be supplied. createDomain requires an app with the
  8. // address() signature.
  9. const app = server || {
  10. address() {
  11. return { port: options.port };
  12. },
  13. };
  14. const domain = createDomain(options, app);
  15. const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
  16. const entries = [
  17. `${require.resolve('../../client/')}?${domain}${sockPath}`,
  18. ];
  19. if (options.hotOnly) {
  20. entries.push(require.resolve('webpack/hot/only-dev-server'));
  21. } else if (options.hot) {
  22. entries.push(require.resolve('webpack/hot/dev-server'));
  23. }
  24. const prependEntry = (entry) => {
  25. if (typeof entry === 'function') {
  26. return () => Promise.resolve(entry()).then(prependEntry);
  27. }
  28. if (typeof entry === 'object' && !Array.isArray(entry)) {
  29. const clone = {};
  30. Object.keys(entry).forEach((key) => {
  31. // entry[key] should be a string here
  32. clone[key] = prependEntry(entry[key]);
  33. });
  34. return clone;
  35. }
  36. // in this case, entry is a string or an array.
  37. // make sure that we do not add duplicates.
  38. const entriesClone = entries.slice(0);
  39. [].concat(entry).forEach((newEntry) => {
  40. if (!entriesClone.includes(newEntry)) {
  41. entriesClone.push(newEntry);
  42. }
  43. });
  44. return entriesClone;
  45. };
  46. // eslint-disable-next-line no-shadow
  47. [].concat(config).forEach((config) => {
  48. config.entry = prependEntry(config.entry || './src');
  49. if (options.hot || options.hotOnly) {
  50. config.plugins = config.plugins || [];
  51. if (
  52. !config.plugins.find(
  53. (plugin) =>
  54. plugin.constructor === webpack.HotModuleReplacementPlugin
  55. )
  56. ) {
  57. config.plugins.push(new webpack.HotModuleReplacementPlugin());
  58. }
  59. }
  60. });
  61. }
  62. }
  63. module.exports = addEntries;