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