You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

4 years ago
  1. 'use strict';
  2. /* eslint-disable
  3. no-shadow,
  4. no-undefined
  5. */
  6. const webpack = require('webpack');
  7. const addEntries = require('./addEntries');
  8. const getSocketClientPath = require('./getSocketClientPath');
  9. function updateCompiler(compiler, options) {
  10. if (options.inline !== false) {
  11. const findHMRPlugin = (config) => {
  12. if (!config.plugins) {
  13. return undefined;
  14. }
  15. return config.plugins.find(
  16. (plugin) => plugin.constructor === webpack.HotModuleReplacementPlugin
  17. );
  18. };
  19. const compilers = [];
  20. const compilersWithoutHMR = [];
  21. let webpackConfig;
  22. if (compiler.compilers) {
  23. webpackConfig = [];
  24. compiler.compilers.forEach((compiler) => {
  25. webpackConfig.push(compiler.options);
  26. compilers.push(compiler);
  27. if (!findHMRPlugin(compiler.options)) {
  28. compilersWithoutHMR.push(compiler);
  29. }
  30. });
  31. } else {
  32. webpackConfig = compiler.options;
  33. compilers.push(compiler);
  34. if (!findHMRPlugin(compiler.options)) {
  35. compilersWithoutHMR.push(compiler);
  36. }
  37. }
  38. // it's possible that we should clone the config before doing
  39. // this, but it seems safe not to since it actually reflects
  40. // the changes we are making to the compiler
  41. // important: this relies on the fact that addEntries now
  42. // prevents duplicate new entries.
  43. addEntries(webpackConfig, options);
  44. compilers.forEach((compiler) => {
  45. const config = compiler.options;
  46. compiler.hooks.entryOption.call(config.context, config.entry);
  47. const providePlugin = new webpack.ProvidePlugin({
  48. __webpack_dev_server_client__: getSocketClientPath(options),
  49. });
  50. providePlugin.apply(compiler);
  51. });
  52. // do not apply the plugin unless it didn't exist before.
  53. if (options.hot || options.hotOnly) {
  54. compilersWithoutHMR.forEach((compiler) => {
  55. // addDevServerEntrypoints above should have added the plugin
  56. // to the compiler options
  57. const plugin = findHMRPlugin(compiler.options);
  58. if (plugin) {
  59. plugin.apply(compiler);
  60. }
  61. });
  62. }
  63. }
  64. }
  65. module.exports = updateCompiler;