Skip to content

Commit 114a770

Browse files
committed
feat: add function to generate js version of config file
1 parent 6c5d2ed commit 114a770

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

src/Uno.Wasm.Bootstrap/ShellTask.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public override bool Execute()
154154
GenerateEmbeddedJs();
155155
GenerateIndexHtml();
156156
GenerateConfig();
157+
GenerateConfigScript();
157158
RemoveDuplicateAssets();
158159
}
159160
finally
@@ -622,6 +623,107 @@ private void GenerateConfig()
622623
}
623624
}
624625

626+
627+
628+
private void GenerateConfigScript()
629+
{
630+
var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, "uno-config-script.js");
631+
632+
using (var w = new StreamWriter(unoConfigJsPath, false, _utf8Encoding))
633+
{
634+
var baseLookup = _shellMode == ShellMode.Node ? "" : $"{WebAppBasePath}{PackageAssetsFolder}/";
635+
var dependencies = string.Join(", ", _dependencies
636+
.Where(d =>
637+
!d.EndsWith("require.js")
638+
&& !d.EndsWith("uno-bootstrap.js")
639+
&& !d.EndsWith("service-worker.js"))
640+
.Select(dep => BuildDependencyPath(dep, baseLookup)));
641+
642+
var config = new StringBuilder();
643+
644+
var enablePWA = !string.IsNullOrEmpty(PWAManifestFile);
645+
646+
var sanitizedOfflineFiles = StaticWebContent
647+
.Select(f => f.GetMetadata("Link")
648+
.Replace("\\", "/")
649+
.Replace("wwwroot/", ""))
650+
.Concat([$"uno-config-script.js", "_framework/blazor.boot.json", "."]);
651+
652+
var offlineFiles = enablePWA ? string.Join(", ", sanitizedOfflineFiles.Select(f => $"\"{WebAppBasePath}{f}\"")) : "";
653+
654+
var emccExportedRuntimeMethodsParams = string.Join(
655+
",",
656+
GetEmccExportedRuntimeMethods().Select(f => $"\'{f}\'"));
657+
658+
var runtimeOptionsSet = string.Join(",", (RuntimeOptions?.Split(' ') ?? []).Select(f => $"\'{f}\'"));
659+
660+
config.AppendLine($"self.config = {{}};");
661+
config.AppendLine($"self.config.uno_remote_managedpath = \"_framework\";");
662+
config.AppendLine($"self.config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";");
663+
config.AppendLine($"self.config.uno_dependencies = [{dependencies}];");
664+
config.AppendLine($"self.config.uno_runtime_options = [{runtimeOptionsSet}];");
665+
config.AppendLine($"self.config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};");
666+
config.AppendLine($"self.config.offline_files = ['{WebAppBasePath}', {offlineFiles}];");
667+
config.AppendLine($"self.config.uno_shell_mode = \"{_shellMode}\";");
668+
config.AppendLine($"self.config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};");
669+
config.AppendLine($"self.config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};");
670+
config.AppendLine($"self.config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};");
671+
config.AppendLine($"self.config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];");
672+
673+
if (GenerateAOTProfile)
674+
{
675+
config.AppendLine($"self.config.generate_aot_profile = true;");
676+
}
677+
678+
config.AppendLine($"self.config.environmentVariables = self.config.environmentVariables || {{}};");
679+
680+
void AddEnvironmentVariable(string name, string value) => config.AppendLine($"self.config.environmentVariables[\"{name}\"] = \"{value}\";");
681+
682+
if (MonoEnvironment != null)
683+
{
684+
foreach (var env in MonoEnvironment)
685+
{
686+
AddEnvironmentVariable(env.ItemSpec, env.GetMetadata("Value"));
687+
}
688+
}
689+
690+
var isProfiledAOT = UseAotProfile && _runtimeExecutionMode == RuntimeExecutionMode.InterpreterAndAOT;
691+
692+
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE", _runtimeExecutionMode.ToString());
693+
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_PROFILED_AOT", isProfiledAOT.ToString());
694+
AddEnvironmentVariable("UNO_BOOTSTRAP_LINKER_ENABLED", (PublishTrimmed && RunILLink).ToString());
695+
AddEnvironmentVariable("UNO_BOOTSTRAP_DEBUGGER_ENABLED", (!Optimize).ToString());
696+
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION", "Release");
697+
AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES", BuildRuntimeFeatures());
698+
AddEnvironmentVariable("UNO_BOOTSTRAP_APP_BASE", PackageAssetsFolder);
699+
AddEnvironmentVariable("UNO_BOOTSTRAP_WEBAPP_BASE_PATH", WebAppBasePath);
700+
701+
if (EmccFlags?.Any(f => f.ItemSpec?.Contains("MAXIMUM_MEMORY=4GB") ?? false) ?? false)
702+
{
703+
// Detects the use of the 4GB flag: https://v8.dev/blog/4gb-wasm-memory
704+
AddEnvironmentVariable("UNO_BOOTSTRAP_EMSCRIPTEN_MAXIMUM_MEMORY", "4GB");
705+
}
706+
707+
if (EnableLogProfiler)
708+
{
709+
AddEnvironmentVariable("UNO_BOOTSTRAP_LOG_PROFILER_OPTIONS", LogProfilerOptions);
710+
}
711+
712+
w.Write(config.ToString());
713+
714+
TaskItem indexMetadata = new(
715+
unoConfigJsPath, new Dictionary<string, string>
716+
{
717+
["CopyToOutputDirectory"] = "PreserveNewest",
718+
["ContentRoot"] = _intermediateAssetsPath,
719+
["Link"] = $"wwwroot/{PackageAssetsFolder}/" + Path.GetFileName(unoConfigJsPath),
720+
});
721+
722+
StaticWebContent = StaticWebContent.Concat([indexMetadata]).ToArray();
723+
}
724+
}
725+
726+
625727
private void GenerateIndexHtml()
626728
{
627729
if (_shellMode != ShellMode.Browser)

0 commit comments

Comments
 (0)