|
| 1 | +const NodePolyfillPlugin = require("node-polyfill-webpack-plugin"); |
| 2 | + |
| 3 | +module.exports = { |
| 4 | + webpack: function override(config) { |
| 5 | + config.plugins.push( |
| 6 | + new NodePolyfillPlugin({ |
| 7 | + excludeAliases: ["console"], |
| 8 | + }) |
| 9 | + ); |
| 10 | + return config; |
| 11 | + }, |
| 12 | + |
| 13 | + devServer: function overrideDevServer(configFn) { |
| 14 | + return function (proxy, allowedHost) { |
| 15 | + // Shim CRA's expected devServer.close() on webpack-dev-server v5 instances |
| 16 | + try { |
| 17 | + const WebpackDevServer = require("webpack-dev-server"); |
| 18 | + if ( |
| 19 | + WebpackDevServer && |
| 20 | + WebpackDevServer.prototype && |
| 21 | + typeof WebpackDevServer.prototype.stop === "function" && |
| 22 | + typeof WebpackDevServer.prototype.close !== "function" |
| 23 | + ) { |
| 24 | + WebpackDevServer.prototype.close = WebpackDevServer.prototype.stop; |
| 25 | + } |
| 26 | + } catch (e) { |
| 27 | + // no-op if require fails |
| 28 | + } |
| 29 | + |
| 30 | + const config = configFn(proxy, allowedHost); |
| 31 | + |
| 32 | + // Remove deprecated v4 hooks rejected by wds v5 schema |
| 33 | + if (config.onBeforeSetupMiddleware) { |
| 34 | + delete config.onBeforeSetupMiddleware; |
| 35 | + } |
| 36 | + if (config.onAfterSetupMiddleware) { |
| 37 | + delete config.onAfterSetupMiddleware; |
| 38 | + } |
| 39 | + |
| 40 | + // Migrate deprecated `https`/`http2` to webpack-dev-server v5 `server` option |
| 41 | + if (Object.prototype.hasOwnProperty.call(config, "https")) { |
| 42 | + const httpsOption = config.https; |
| 43 | + if (httpsOption) { |
| 44 | + if (typeof httpsOption === "object") { |
| 45 | + config.server = { type: "https", options: httpsOption }; |
| 46 | + } else { |
| 47 | + config.server = "https"; |
| 48 | + } |
| 49 | + } else { |
| 50 | + config.server = "http"; |
| 51 | + } |
| 52 | + delete config.https; |
| 53 | + } |
| 54 | + if (Object.prototype.hasOwnProperty.call(config, "http2")) { |
| 55 | + delete config.http2; |
| 56 | + } |
| 57 | + |
| 58 | + // Ensure setupMiddlewares exists for wds v5 |
| 59 | + if (!config.setupMiddlewares) { |
| 60 | + config.setupMiddlewares = (middlewares) => middlewares; |
| 61 | + } |
| 62 | + |
| 63 | + return config; |
| 64 | + }; |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
0 commit comments