updateCompiler.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. const providePlugin = new webpack.ProvidePlugin({
  47. // SockJSClient.getClientPath(options)
  48. __webpack_dev_server_client__: require.resolve(
  49. '../../client/clients/SockJSClient.js'
  50. ),
  51. });
  52. providePlugin.apply(compiler);
  53. });
  54. // do not apply the plugin unless it didn't exist before.
  55. if (options.hot || options.hotOnly) {
  56. compilersWithoutHMR.forEach((compiler) => {
  57. // addDevServerEntrypoints above should have added the plugin
  58. // to the compiler options
  59. const plugin = findHMRPlugin(compiler.options);
  60. if (plugin) {
  61. plugin.apply(compiler);
  62. }
  63. });
  64. }
  65. }
  66. }
  67. module.exports = updateCompiler;