updateCompiler.js 1.9 KB

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