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

Commit 3568476

Browse files
aspnet-webpack auto-loads 'event-source-polyfill' on client when HMR is enabled. This requires 'event-source-polyfill' to be included in the client-side bundle, so it's also now added to all the templates' vendor bundles. Fixes #365.
1 parent 93779a5 commit 3568476

File tree

11 files changed

+36
-9
lines changed

11 files changed

+36
-9
lines changed

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

+26-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ interface DevServerOptions {
2626
ReactHotModuleReplacement: boolean;
2727
}
2828

29-
function arrayContainsStringStartingWith(array: string[], prefixToFind: string) {
30-
return array.some(item => item.substring(0, prefixToFind.length) === prefixToFind);
31-
}
32-
3329
function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configuration, enableHotModuleReplacement: boolean, enableReactHotModuleReplacement: boolean, hmrEndpoint: string) {
3430
// Build the final Webpack config based on supplied options
3531
if (enableHotModuleReplacement) {
@@ -49,9 +45,23 @@ function attachWebpackDevMiddleware(app: any, webpackConfig: webpack.Configurati
4945
const webpackHotMiddlewareOptions = `?path=` + encodeURIComponent(hmrEndpoint);
5046
if (typeof entryPoints[entryPointName] === 'string') {
5147
entryPoints[entryPointName] = [webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions, entryPoints[entryPointName]];
52-
} else if (!arrayContainsStringStartingWith(entryPoints[entryPointName], webpackHotMiddlewareEntryPoint)) {
48+
} else if (firstIndexOfStringStartingWith(entryPoints[entryPointName], webpackHotMiddlewareEntryPoint) < 0) {
5349
entryPoints[entryPointName].unshift(webpackHotMiddlewareEntryPoint + webpackHotMiddlewareOptions);
5450
}
51+
52+
// Now also inject eventsource polyfill so this can work on IE/Edge (unless it's already there)
53+
const eventSourcePolyfillEntryPoint = 'event-source-polyfill';
54+
const entryPointsArray: string[] = entryPoints[entryPointName]; // We know by now that it's an array, because if it wasn't, we already wrapped it in one
55+
if (entryPointsArray.indexOf(eventSourcePolyfillEntryPoint) < 0) {
56+
const webpackHmrIndex = firstIndexOfStringStartingWith(entryPointsArray, webpackHotMiddlewareEntryPoint);
57+
if (webpackHmrIndex < 0) {
58+
// This should not be possible, since we just added it if it was missing
59+
throw new Error('Cannot find ' + webpackHotMiddlewareEntryPoint + ' in entry points array: ' + entryPointsArray);
60+
}
61+
62+
// Insert the polyfill just before the HMR entrypoint
63+
entryPointsArray.splice(webpackHmrIndex, 0, eventSourcePolyfillEntryPoint);
64+
}
5565
});
5666

5767
webpackConfig.plugins = [].concat(webpackConfig.plugins || []); // Be sure not to mutate the original array, as it might be shared
@@ -168,3 +178,14 @@ function removeTrailingSlash(str: string) {
168178
function getPath(publicPath: string) {
169179
return url.parse(publicPath).path;
170180
}
181+
182+
function firstIndexOfStringStartingWith(array: string[], prefixToFind: string) {
183+
for (let index = 0; index < array.length; index++) {
184+
const candidate = array[index];
185+
if (candidate.substring(0, prefixToFind.length) === prefixToFind) {
186+
return index;
187+
}
188+
}
189+
190+
return -1; // Not found
191+
}

templates/Angular2Spa/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"css": "^2.2.1",
2222
"css-loader": "^0.25.0",
2323
"es6-shim": "^0.35.1",
24+
"event-source-polyfill": "^0.0.7",
2425
"expose-loader": "^0.7.1",
2526
"extract-text-webpack-plugin": "^1.0.1",
2627
"file-loader": "^0.9.0",

templates/Angular2Spa/webpack.config.vendor.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
'bootstrap/dist/css/bootstrap.css',
3131
'es6-shim',
3232
'es6-promise',
33+
'event-source-polyfill',
3334
'jquery',
3435
'zone.js',
3536
]

templates/KnockoutSpa/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"bundle-loader": "^0.5.4",
88
"crossroads": "^0.12.2",
99
"css-loader": "^0.23.1",
10+
"event-source-polyfill": "^0.0.7",
1011
"extract-text-webpack-plugin": "^1.0.1",
1112
"file-loader": "^0.8.5",
1213
"history": "^2.0.1",

templates/KnockoutSpa/webpack.config.vendor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
]
1616
},
1717
entry: {
18-
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'knockout', 'crossroads', 'history', 'isomorphic-fetch', 'style-loader', 'jquery'],
18+
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'knockout', 'crossroads', 'event-source-polyfill', 'history', 'isomorphic-fetch', 'style-loader', 'jquery'],
1919
},
2020
output: {
2121
path: path.join(__dirname, 'wwwroot', 'dist'),

templates/ReactReduxSpa/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"bootstrap": "^3.3.6",
1313
"css-loader": "^0.23.1",
1414
"domain-task": "^2.0.0",
15+
"event-source-polyfill": "^0.0.7",
1516
"extract-text-webpack-plugin": "^1.0.1",
1617
"file-loader": "^0.8.5",
1718
"jquery": "^2.2.1",

templates/ReactReduxSpa/webpack.config.vendor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
]
1616
},
1717
entry: {
18-
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'domain-task', 'react', 'react-dom', 'react-router', 'redux', 'redux-thunk', 'react-router-redux', 'redux-typed', 'style-loader', 'jquery'],
18+
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'domain-task', 'event-source-polyfill', 'react', 'react-dom', 'react-router', 'redux', 'redux-thunk', 'react-router-redux', 'redux-typed', 'style-loader', 'jquery'],
1919
},
2020
output: {
2121
path: path.join(__dirname, 'wwwroot', 'dist'),

templates/ReactSpa/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"babel-preset-react": "^6.5.0",
1111
"bootstrap": "^3.3.6",
1212
"css-loader": "^0.23.1",
13+
"event-source-polyfill": "^0.0.7",
1314
"extract-text-webpack-plugin": "^1.0.1",
1415
"file-loader": "^0.8.5",
1516
"isomorphic-fetch": "^2.2.1",

templates/ReactSpa/webpack.config.vendor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
]
1616
},
1717
entry: {
18-
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'isomorphic-fetch', 'react', 'react-dom', 'react-router', 'style-loader', 'jquery'],
18+
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'event-source-polyfill', 'isomorphic-fetch', 'react', 'react-dom', 'react-router', 'style-loader', 'jquery'],
1919
},
2020
output: {
2121
path: path.join(__dirname, 'wwwroot', 'dist'),

templates/WebApplicationBasic/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"devDependencies": {
55
"bootstrap": "^3.3.6",
66
"css-loader": "^0.23.1",
7+
"event-source-polyfill": "^0.0.7",
78
"extendify": "^1.0.0",
89
"extract-text-webpack-plugin": "^1.0.1",
910
"file-loader": "^0.8.5",

templates/WebApplicationBasic/webpack.config.vendor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
]
1616
},
1717
entry: {
18-
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'style-loader', 'jquery']
18+
vendor: ['bootstrap', 'bootstrap/dist/css/bootstrap.css', 'event-source-polyfill', 'style-loader', 'jquery']
1919
},
2020
output: {
2121
path: path.join(__dirname, 'wwwroot', 'dist'),

0 commit comments

Comments
 (0)