Skip to content

Commit a485e9a

Browse files
committed
chore: unify config files creation
1 parent 6982c16 commit a485e9a

File tree

1 file changed

+38
-119
lines changed

1 file changed

+38
-119
lines changed

src/Uno.Wasm.Bootstrap/ShellTask.cs

Lines changed: 38 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ public override bool Execute()
153153
BuildServiceWorkers();
154154
GenerateEmbeddedJs();
155155
GenerateIndexHtml();
156-
GenerateConfig();
157-
GenerateConfigScript();
156+
GenerateConfigFiles();
158157
RemoveDuplicateAssets();
159158
}
160159
finally
@@ -531,112 +530,18 @@ static string BuildDependencyPath(string dep, string baseLookup)
531530
? $"\"{baseLookup}{Path.GetFileName(dep)}\""
532531
: $"\"{baseLookup}{Path.GetFileNameWithoutExtension(dep)}\"";
533532

534-
private void GenerateConfig()
533+
private void GenerateConfigFiles()
535534
{
536-
var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, "uno-config.js");
535+
GenerateConfigFile("uno-config.js", isModule: true);
537536

538-
using (var w = new StreamWriter(unoConfigJsPath, false, _utf8Encoding))
539-
{
540-
var baseLookup = _shellMode == ShellMode.Node ? "" : $"{WebAppBasePath}{PackageAssetsFolder}/";
541-
var dependencies = string.Join(", ", _dependencies
542-
.Where(d =>
543-
!d.EndsWith("require.js")
544-
&& !d.EndsWith("uno-bootstrap.js")
545-
&& !d.EndsWith("service-worker.js")
546-
&& !d.EndsWith("service-worker-classic.js"))
547-
.Select(dep => BuildDependencyPath(dep, baseLookup)));
548-
549-
var config = new StringBuilder();
550-
551-
var enablePWA = !string.IsNullOrEmpty(PWAManifestFile);
552-
553-
var sanitizedOfflineFiles = StaticWebContent
554-
.Select(f => f.GetMetadata("Link")
555-
.Replace("\\", "/")
556-
.Replace("wwwroot/", ""))
557-
.Concat([$"uno-config.js", "_framework/blazor.boot.json", "."]);
558-
559-
var offlineFiles = enablePWA ? string.Join(", ", sanitizedOfflineFiles.Select(f => $"\"{WebAppBasePath}{f}\"")) : "";
560-
561-
var emccExportedRuntimeMethodsParams = string.Join(
562-
",",
563-
GetEmccExportedRuntimeMethods().Select(f => $"\'{f}\'"));
564-
565-
var runtimeOptionsSet = string.Join(",", (RuntimeOptions?.Split(' ') ?? []).Select(f => $"\'{f}\'"));
566-
567-
config.AppendLine($"let config = {{}};");
568-
config.AppendLine($"config.uno_remote_managedpath = \"_framework\";");
569-
config.AppendLine($"config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";");
570-
config.AppendLine($"config.uno_dependencies = [{dependencies}];");
571-
config.AppendLine($"config.uno_runtime_options = [{runtimeOptionsSet}];");
572-
config.AppendLine($"config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};");
573-
config.AppendLine($"config.offline_files = ['{WebAppBasePath}', {offlineFiles}];");
574-
config.AppendLine($"config.uno_shell_mode = \"{_shellMode}\";");
575-
config.AppendLine($"config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};");
576-
config.AppendLine($"config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};");
577-
config.AppendLine($"config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};");
578-
config.AppendLine($"config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];");
579-
580-
if (GenerateAOTProfile)
581-
{
582-
config.AppendLine($"config.generate_aot_profile = true;");
583-
}
584-
585-
config.AppendLine($"config.environmentVariables = config.environmentVariables || {{}};");
586-
587-
void AddEnvironmentVariable(string name, string value) => config.AppendLine($"config.environmentVariables[\"{name}\"] = \"{value}\";");
588-
589-
if (MonoEnvironment != null)
590-
{
591-
foreach (var env in MonoEnvironment)
592-
{
593-
AddEnvironmentVariable(env.ItemSpec, env.GetMetadata("Value"));
594-
}
595-
}
596-
597-
var isProfiledAOT = UseAotProfile && _runtimeExecutionMode == RuntimeExecutionMode.InterpreterAndAOT;
598-
599-
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE", _runtimeExecutionMode.ToString());
600-
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_PROFILED_AOT", isProfiledAOT.ToString());
601-
AddEnvironmentVariable("UNO_BOOTSTRAP_LINKER_ENABLED", (PublishTrimmed && RunILLink).ToString());
602-
AddEnvironmentVariable("UNO_BOOTSTRAP_DEBUGGER_ENABLED", (!Optimize).ToString());
603-
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION", "Release");
604-
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES", BuildRuntimeFeatures());
605-
AddEnvironmentVariable("UNO_BOOTSTRAP_APP_BASE", PackageAssetsFolder);
606-
AddEnvironmentVariable("UNO_BOOTSTRAP_WEBAPP_BASE_PATH", WebAppBasePath);
607-
608-
if (EmccFlags?.Any(f => f.ItemSpec?.Contains("MAXIMUM_MEMORY=4GB") ?? false) ?? false)
609-
{
610-
// Detects the use of the 4GB flag: https://v8.dev/blog/4gb-wasm-memory
611-
AddEnvironmentVariable("UNO_BOOTSTRAP_EMSCRIPTEN_MAXIMUM_MEMORY", "4GB");
612-
}
613-
614-
if (EnableLogProfiler)
615-
{
616-
AddEnvironmentVariable("UNO_BOOTSTRAP_LOG_PROFILER_OPTIONS", LogProfilerOptions);
617-
}
618-
619-
config.AppendLine("export { config };");
620-
621-
w.Write(config.ToString());
622-
623-
TaskItem indexMetadata = new(
624-
unoConfigJsPath, new Dictionary<string, string>
625-
{
626-
["CopyToOutputDirectory"] = "PreserveNewest",
627-
["ContentRoot"] = _intermediateAssetsPath,
628-
["Link"] = $"wwwroot/{PackageAssetsFolder}/" + Path.GetFileName(unoConfigJsPath),
629-
});
630-
631-
StaticWebContent = StaticWebContent.Concat([indexMetadata]).ToArray();
632-
}
537+
GenerateConfigFile("uno-config-script.js", isModule: false);
633538
}
634539

635-
636-
637-
private void GenerateConfigScript()
540+
private void GenerateConfigFile(string fileName, bool isModule)
638541
{
639-
var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, "uno-config-script.js");
542+
var self = isModule ? "" : "self.";
543+
544+
var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, fileName);
640545

641546
using (var w = new StreamWriter(unoConfigJsPath, false, _utf8Encoding))
642547
{
@@ -657,7 +562,7 @@ private void GenerateConfigScript()
657562
.Select(f => f.GetMetadata("Link")
658563
.Replace("\\", "/")
659564
.Replace("wwwroot/", ""))
660-
.Concat([$"uno-config-script.js", "_framework/blazor.boot.json", "."]);
565+
.Concat([fileName, "_framework/blazor.boot.json", "."]);
661566

662567
var offlineFiles = enablePWA ? string.Join(", ", sanitizedOfflineFiles.Select(f => $"\"{WebAppBasePath}{f}\"")) : "";
663568

@@ -667,27 +572,36 @@ private void GenerateConfigScript()
667572

668573
var runtimeOptionsSet = string.Join(",", (RuntimeOptions?.Split(' ') ?? []).Select(f => $"\'{f}\'"));
669574

670-
config.AppendLine($"self.config = {{}};");
671-
config.AppendLine($"self.config.uno_remote_managedpath = \"_framework\";");
672-
config.AppendLine($"self.config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";");
673-
config.AppendLine($"self.config.uno_dependencies = [{dependencies}];");
674-
config.AppendLine($"self.config.uno_runtime_options = [{runtimeOptionsSet}];");
675-
config.AppendLine($"self.config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};");
676-
config.AppendLine($"self.config.offline_files = ['{WebAppBasePath}', {offlineFiles}];");
677-
config.AppendLine($"self.config.uno_shell_mode = \"{_shellMode}\";");
678-
config.AppendLine($"self.config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};");
679-
config.AppendLine($"self.config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};");
680-
config.AppendLine($"self.config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};");
681-
config.AppendLine($"self.config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];");
575+
576+
if (isModule)
577+
{
578+
config.AppendLine($"let config = {{}};");
579+
}
580+
else
581+
{
582+
config.AppendLine($"{self}config = {{}};");
583+
}
584+
585+
config.AppendLine($"{self}config.uno_remote_managedpath = \"_framework\";");
586+
config.AppendLine($"{self}config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";");
587+
config.AppendLine($"{self}config.uno_dependencies = [{dependencies}];");
588+
config.AppendLine($"{self}config.uno_runtime_options = [{runtimeOptionsSet}];");
589+
config.AppendLine($"{self}config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};");
590+
config.AppendLine($"{self}config.offline_files = ['{WebAppBasePath}', {offlineFiles}];");
591+
config.AppendLine($"{self}config.uno_shell_mode = \"{_shellMode}\";");
592+
config.AppendLine($"{self}config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};");
593+
config.AppendLine($"{self}config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};");
594+
config.AppendLine($"{self}config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};");
595+
config.AppendLine($"{self}config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];");
682596

683597
if (GenerateAOTProfile)
684598
{
685-
config.AppendLine($"self.config.generate_aot_profile = true;");
599+
config.AppendLine($"{self}config.generate_aot_profile = true;");
686600
}
687601

688-
config.AppendLine($"self.config.environmentVariables = self.config.environmentVariables || {{}};");
602+
config.AppendLine($"{self}config.environmentVariables = {self}config.environmentVariables || {{}};");
689603

690-
void AddEnvironmentVariable(string name, string value) => config.AppendLine($"self.config.environmentVariables[\"{name}\"] = \"{value}\";");
604+
void AddEnvironmentVariable(string name, string value) => config.AppendLine($"{self}config.environmentVariables[\"{name}\"] = \"{value}\";");
691605

692606
if (MonoEnvironment != null)
693607
{
@@ -719,6 +633,11 @@ private void GenerateConfigScript()
719633
AddEnvironmentVariable("UNO_BOOTSTRAP_LOG_PROFILER_OPTIONS", LogProfilerOptions);
720634
}
721635

636+
if (isModule)
637+
{
638+
config.AppendLine("export { config };");
639+
}
640+
722641
w.Write(config.ToString());
723642

724643
TaskItem indexMetadata = new(

0 commit comments

Comments
 (0)