private static FileContentReference GetPatchedBlazorServerFile() { if (_patchedBlazorServerFile == null) { var assembly = GetAspNetCoreComponentsServerAssembly(); var resources = assembly.GetManifestResourceNames(); var resourceName = resources.Single(str => str.EndsWith("blazor.server.js")); using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string js = reader.ReadToEnd(); //Patch Descriptor Regex as it make Babel crash during transform js = js.Replace("/\\W*Blazor:[^{]*(?<descriptor>.*)$/;", @"/[\0-\/:-@\[-\^`\{-\uFFFF]*Blazor:(?:(?!\{)[\s\S])*(.*)$/;"); //Transpile code to ES5 for IE11 before manual patching js = Transform(js, "blazor.server.js", "{\"plugins\":[\"proposal-class-properties\",\"proposal-object-rest-spread\"],\"presets\":[[\"env\",{\"targets\":{\"browsers\":[\"ie 11\"]}}], \"es2015\",\"es2016\",\"es2017\",\"stage-3\"], \"sourceType\": \"script\"}"); //At this point, Babel has unminified the code, and fixed IE11 issues, like 'import' method calls. //We still need to fix 'descriptor' regex evaluation code, as it was expecting a named capture group. js = Regex.Replace(js, "([a-zA-Z]+)(.groups[ ]*&&[ ]*[a-zA-Z]+.groups.descriptor)", "$1[1]"); //Minify with AjaxMin (we don't want an additional external tool with NPM or else for managing this //kind of thing here... js = Uglify.Js(js).Code; //Computing ETag. Should be computed last ! string Etag = EtagGenerator.GenerateEtagFromString(js); //Computing Build time for the Last-Modified Http Header //We should rely on the creation date of the Microsoft API //not the Blazor.Polyfill.Server one as the Microsoft.AspNetCore.Components.Server //assembly may be updated in time. We will rely on the current creation/modification date on disk DateTime buildTime = GetAssemblyCreationDate(assembly); _patchedBlazorServerFile = new FileContentReference() { Value = js, ETag = Etag, LastModified = buildTime, ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(js).ToString(CultureInfo.InvariantCulture) }; } } return(_patchedBlazorServerFile); }
private static FileContentReference GetIE11BlazorPolyfill(bool isIE11, bool isMinified) { if (!isIE11) { if (_fakeie11Polyfill == null) { string fakeContent = "var _fakeBlazorPolyfill = { };"; //Computing ETag. Should be computed last ! string Etag = EtagGenerator.GenerateEtagFromString(fakeContent); //Computing Build time for the Last-Modified Http Header DateTime buildTime = GetBlazorPolyfillServerBuildDate(); _fakeie11Polyfill = new FileContentReference() { Value = fakeContent, ETag = Etag, LastModified = buildTime, ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(fakeContent).ToString(CultureInfo.InvariantCulture) }; } return(_fakeie11Polyfill); } else { if (isMinified) { if (_ie11PolyfillMin == null) { var assembly = GetBlazorPolyfillAssembly(); var resources = assembly.GetManifestResourceNames(); var resourceName = resources.Single(str => str.EndsWith("blazor.polyfill.min.js")); using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string js = reader.ReadToEnd(); //Computing ETag. Should be computed last ! string Etag = EtagGenerator.GenerateEtagFromString(js); //Computing Build time for the Last-Modified Http Header DateTime buildTime = GetBlazorPolyfillServerBuildDate(); _ie11PolyfillMin = new FileContentReference() { Value = js, ETag = Etag, LastModified = buildTime, ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(js).ToString(CultureInfo.InvariantCulture) }; } } return(_ie11PolyfillMin); } else { if (_ie11Polyfill == null) { var assembly = GetBlazorPolyfillAssembly(); var resources = assembly.GetManifestResourceNames(); var resourceName = resources.Single(str => str.EndsWith("blazor.polyfill.js")); using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { string js = reader.ReadToEnd(); //Computing ETag. Should be computed last ! string Etag = EtagGenerator.GenerateEtagFromString(js); //Computing Build time for the Last-Modified Http Header DateTime buildTime = GetBlazorPolyfillServerBuildDate(); _ie11Polyfill = new FileContentReference() { Value = js, ETag = Etag, LastModified = buildTime, ContentLength = System.Text.UTF8Encoding.UTF8.GetByteCount(js).ToString(CultureInfo.InvariantCulture) }; } } return(_ie11Polyfill); } } }