Skip to content

Commit bc2de2a

Browse files
In aspnet-webpack HMR, don't rely on assumption that entry point is called 'main'. Fixes aspnet#289.
1 parent 605090e commit bc2de2a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aspnet-webpack",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.",
55
"main": "index.js",
66
"scripts": {

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,26 @@ export function createWebpackDevServer(callback: CreateDevServerCallback, option
4343
const listener = app.listen(suggestedHMRPortOrZero, () => {
4444
// Build the final Webpack config based on supplied options
4545
if (enableHotModuleReplacement) {
46-
// TODO: Stop assuming there's an entry point called 'main'
47-
if (typeof webpackConfig.entry['main'] === 'string') {
48-
webpackConfig.entry['main'] = ['webpack-hot-middleware/client', webpackConfig.entry['main']];
49-
} else {
50-
webpackConfig.entry['main'].unshift('webpack-hot-middleware/client');
46+
// For this, we only support the key/value config format, not string or string[], since
47+
// those ones don't clearly indicate what the resulting bundle name will be
48+
const entryPoints = webpackConfig.entry;
49+
const isObjectStyleConfig = entryPoints
50+
&& typeof entryPoints === 'object'
51+
&& !(entryPoints instanceof Array);
52+
if (!isObjectStyleConfig) {
53+
callback('To use HotModuleReplacement, your webpack config must specify an \'entry\' value as a key-value object (e.g., "entry: { main: \'ClientApp/boot-client.ts\' }")', null);
54+
return;
5155
}
56+
57+
// Augment all entry points so they support HMR
58+
Object.getOwnPropertyNames(entryPoints).forEach(entryPointName => {
59+
if (typeof entryPoints[entryPointName] === 'string') {
60+
entryPoints[entryPointName] = ['webpack-hot-middleware/client', entryPoints[entryPointName]];
61+
} else {
62+
entryPoints[entryPointName].unshift('webpack-hot-middleware/client');
63+
}
64+
});
65+
5266
webpackConfig.plugins.push(
5367
new webpack.HotModuleReplacementPlugin()
5468
);

0 commit comments

Comments
 (0)