Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit adf4732

Browse files
Make aspnet-webpack and SpaServices both back-compatible with older versions of the other, in case people don't upgrade both at the same time
1 parent 7b22722 commit adf4732

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public static void UseWebpackDevMiddleware(
6565
nodeServices.InvokeExportAsync<WebpackDevServerInfo>(nodeScript.FileName, "createWebpackDevServer",
6666
JsonConvert.SerializeObject(devServerOptions)).Result;
6767

68-
// Older versions of aspnet-webpack just returned a single 'publicPath', but now we support multiple
68+
// If we're talking to an older version of aspnet-webpack, it will return only a single PublicPath,
69+
// not an array of PublicPaths. Handle that scenario.
6970
if (devServerInfo.PublicPaths == null)
7071
{
71-
throw new InvalidOperationException(
72-
"To enable Webpack dev middleware, you must update to a newer version of the aspnet-webpack NPM package.");
72+
devServerInfo.PublicPaths = new[] { devServerInfo.PublicPath };
7373
}
7474

7575
// Proxy the corresponding requests through ASP.NET and into the Node listener
@@ -107,6 +107,10 @@ class WebpackDevServerInfo
107107
{
108108
public int Port { get; set; }
109109
public string[] PublicPaths { get; set; }
110+
111+
// For back-compatibility with older versions of aspnet-webpack, in the case where your webpack
112+
// configuration contains exactly one config entry. This will be removed soon.
113+
public string PublicPath { get; set; }
110114
}
111115
}
112116
#pragma warning restore CS0649

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/WebpackDevMiddleware.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import * as webpack from 'webpack';
33
import * as url from 'url';
44
import { requireNewCopy } from './RequireNewCopy';
55

6+
export type CreateDevServerResult = {
7+
Port: number,
8+
PublicPaths: string[],
9+
PublicPath: string // For backward compatibility with older verions of Microsoft.AspNetCore.SpaServices. Will be removed soon.
10+
};
11+
612
export interface CreateDevServerCallback {
7-
(error: any, result: { Port: number, PublicPaths: string[] }): void;
13+
(error: any, result: CreateDevServerResult): void;
814
}
915

1016
// These are the options passed by WebpackDevMiddleware.cs
1117
interface CreateDevServerOptions {
12-
understandsMultiplePublicPaths: boolean; // For checking that the NuGet package is recent enough. Can be removed when we no longer need back-compatibility.
1318
webpackConfigPath: string;
1419
suppliedOptions: DevServerOptions;
1520
}
@@ -89,11 +94,6 @@ function beginWebpackWatcher(webpackConfig: webpack.Configuration) {
8994
export function createWebpackDevServer(callback: CreateDevServerCallback, optionsJson: string) {
9095
const options: CreateDevServerOptions = JSON.parse(optionsJson);
9196

92-
if (!options.understandsMultiplePublicPaths) {
93-
callback('To use Webpack dev server, you must update to a newer version of the Microsoft.AspNetCore.SpaServices package', null);
94-
return;
95-
}
96-
9797
// Read the webpack config's export, and normalize it into the more general 'array of configs' format
9898
let webpackConfigArray: webpack.Configuration[] = requireNewCopy(options.webpackConfigPath);
9999
if (!(webpackConfigArray instanceof Array)) {
@@ -137,7 +137,11 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
137137
// Tell the ASP.NET app what addresses we're listening on, so that it can proxy requests here
138138
callback(null, {
139139
Port: listener.address().port,
140-
PublicPaths: normalizedPublicPaths
140+
PublicPaths: normalizedPublicPaths,
141+
142+
// For back-compatibility with older versions of Microsoft.AspNetCore.SpaServices, in the case where
143+
// you have exactly one webpackConfigArray entry. This will be removed soon.
144+
PublicPath: normalizedPublicPaths[0]
141145
});
142146
} catch (ex) {
143147
callback(ex.stack, null);

0 commit comments

Comments
 (0)