Skip to content

Commit

Permalink
chore: unify service workers file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Dec 15, 2024
1 parent efba02c commit 6982c16
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ public override bool Execute()
ExtractAdditionalCSS();
RemoveDuplicateAssets();
GeneratePackageFolder();
BuildServiceWorker();
BuildServiceWorkerClassic();
BuildServiceWorkers();
GenerateEmbeddedJs();
GenerateIndexHtml();
GenerateConfig();
Expand Down Expand Up @@ -295,27 +294,17 @@ private void CopyContent()
}
}

private void BuildServiceWorker()
private void BuildServiceWorkers()
{
using var resourceStream = GetType().Assembly.GetManifestResourceStream("Uno.Wasm.Bootstrap.v0.Embedded.service-worker.js");
using var reader = new StreamReader(resourceStream);

var worker = TouchServiceWorker(reader.ReadToEnd());
var memoryStream = new MemoryStream();

using var writer = new StreamWriter(memoryStream, Encoding.UTF8);
writer.Write(worker);
writer.Flush();

memoryStream.Position = 0;
BuildServiceWorker(resource: "Uno.Wasm.Bootstrap.v0.Embedded.service-worker.js", outputFile: "service-worker.js");

CopyStreamToOutput("service-worker.js", memoryStream, DeployMode.Root);
// Case for browsers that do not support modules for service workers: Firefox for example
BuildServiceWorker(resource: "Uno.Wasm.Bootstrap.v0.Embedded.service-worker-classic.js", outputFile: "service-worker-classic.js");
}

// Case for browsers that do not support modules for service workers: Firefox for example
private void BuildServiceWorkerClassic()
private void BuildServiceWorker(string resource, string outputFile)
{
using var resourceStream = GetType().Assembly.GetManifestResourceStream("Uno.Wasm.Bootstrap.v0.Embedded.service-worker-classic.js");
using var resourceStream = GetType().Assembly.GetManifestResourceStream(resource);
using var reader = new StreamReader(resourceStream);

var worker = TouchServiceWorker(reader.ReadToEnd());
Expand All @@ -327,7 +316,7 @@ private void BuildServiceWorkerClassic()

memoryStream.Position = 0;

CopyStreamToOutput("service-worker-classic.js", memoryStream, DeployMode.Root);
CopyStreamToOutput(outputFile, memoryStream, DeployMode.Root);
}

private void ExtractAdditionalJS()
Expand Down

0 comments on commit 6982c16

Please sign in to comment.