From a485e9aa2d40f82bbc4bc543d16f4e82327c55d5 Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Mon, 16 Dec 2024 07:57:21 -0500 Subject: [PATCH] chore: unify config files creation --- src/Uno.Wasm.Bootstrap/ShellTask.cs | 157 +++++++--------------------- 1 file changed, 38 insertions(+), 119 deletions(-) diff --git a/src/Uno.Wasm.Bootstrap/ShellTask.cs b/src/Uno.Wasm.Bootstrap/ShellTask.cs index eb01b39c..4251ae12 100644 --- a/src/Uno.Wasm.Bootstrap/ShellTask.cs +++ b/src/Uno.Wasm.Bootstrap/ShellTask.cs @@ -153,8 +153,7 @@ public override bool Execute() BuildServiceWorkers(); GenerateEmbeddedJs(); GenerateIndexHtml(); - GenerateConfig(); - GenerateConfigScript(); + GenerateConfigFiles(); RemoveDuplicateAssets(); } finally @@ -531,112 +530,18 @@ static string BuildDependencyPath(string dep, string baseLookup) ? $"\"{baseLookup}{Path.GetFileName(dep)}\"" : $"\"{baseLookup}{Path.GetFileNameWithoutExtension(dep)}\""; - private void GenerateConfig() + private void GenerateConfigFiles() { - var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, "uno-config.js"); + GenerateConfigFile("uno-config.js", isModule: true); - using (var w = new StreamWriter(unoConfigJsPath, false, _utf8Encoding)) - { - var baseLookup = _shellMode == ShellMode.Node ? "" : $"{WebAppBasePath}{PackageAssetsFolder}/"; - var dependencies = string.Join(", ", _dependencies - .Where(d => - !d.EndsWith("require.js") - && !d.EndsWith("uno-bootstrap.js") - && !d.EndsWith("service-worker.js") - && !d.EndsWith("service-worker-classic.js")) - .Select(dep => BuildDependencyPath(dep, baseLookup))); - - var config = new StringBuilder(); - - var enablePWA = !string.IsNullOrEmpty(PWAManifestFile); - - var sanitizedOfflineFiles = StaticWebContent - .Select(f => f.GetMetadata("Link") - .Replace("\\", "/") - .Replace("wwwroot/", "")) - .Concat([$"uno-config.js", "_framework/blazor.boot.json", "."]); - - var offlineFiles = enablePWA ? string.Join(", ", sanitizedOfflineFiles.Select(f => $"\"{WebAppBasePath}{f}\"")) : ""; - - var emccExportedRuntimeMethodsParams = string.Join( - ",", - GetEmccExportedRuntimeMethods().Select(f => $"\'{f}\'")); - - var runtimeOptionsSet = string.Join(",", (RuntimeOptions?.Split(' ') ?? []).Select(f => $"\'{f}\'")); - - config.AppendLine($"let config = {{}};"); - config.AppendLine($"config.uno_remote_managedpath = \"_framework\";"); - config.AppendLine($"config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";"); - config.AppendLine($"config.uno_dependencies = [{dependencies}];"); - config.AppendLine($"config.uno_runtime_options = [{runtimeOptionsSet}];"); - config.AppendLine($"config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};"); - config.AppendLine($"config.offline_files = ['{WebAppBasePath}', {offlineFiles}];"); - config.AppendLine($"config.uno_shell_mode = \"{_shellMode}\";"); - config.AppendLine($"config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};"); - config.AppendLine($"config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};"); - config.AppendLine($"config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};"); - config.AppendLine($"config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];"); - - if (GenerateAOTProfile) - { - config.AppendLine($"config.generate_aot_profile = true;"); - } - - config.AppendLine($"config.environmentVariables = config.environmentVariables || {{}};"); - - void AddEnvironmentVariable(string name, string value) => config.AppendLine($"config.environmentVariables[\"{name}\"] = \"{value}\";"); - - if (MonoEnvironment != null) - { - foreach (var env in MonoEnvironment) - { - AddEnvironmentVariable(env.ItemSpec, env.GetMetadata("Value")); - } - } - - var isProfiledAOT = UseAotProfile && _runtimeExecutionMode == RuntimeExecutionMode.InterpreterAndAOT; - - AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_MODE", _runtimeExecutionMode.ToString()); - AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_PROFILED_AOT", isProfiledAOT.ToString()); - AddEnvironmentVariable("UNO_BOOTSTRAP_LINKER_ENABLED", (PublishTrimmed && RunILLink).ToString()); - AddEnvironmentVariable("UNO_BOOTSTRAP_DEBUGGER_ENABLED", (!Optimize).ToString()); - AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_CONFIGURATION", "Release"); - AddEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES", BuildRuntimeFeatures()); - AddEnvironmentVariable("UNO_BOOTSTRAP_APP_BASE", PackageAssetsFolder); - AddEnvironmentVariable("UNO_BOOTSTRAP_WEBAPP_BASE_PATH", WebAppBasePath); - - if (EmccFlags?.Any(f => f.ItemSpec?.Contains("MAXIMUM_MEMORY=4GB") ?? false) ?? false) - { - // Detects the use of the 4GB flag: https://v8.dev/blog/4gb-wasm-memory - AddEnvironmentVariable("UNO_BOOTSTRAP_EMSCRIPTEN_MAXIMUM_MEMORY", "4GB"); - } - - if (EnableLogProfiler) - { - AddEnvironmentVariable("UNO_BOOTSTRAP_LOG_PROFILER_OPTIONS", LogProfilerOptions); - } - - config.AppendLine("export { config };"); - - w.Write(config.ToString()); - - TaskItem indexMetadata = new( - unoConfigJsPath, new Dictionary - { - ["CopyToOutputDirectory"] = "PreserveNewest", - ["ContentRoot"] = _intermediateAssetsPath, - ["Link"] = $"wwwroot/{PackageAssetsFolder}/" + Path.GetFileName(unoConfigJsPath), - }); - - StaticWebContent = StaticWebContent.Concat([indexMetadata]).ToArray(); - } + GenerateConfigFile("uno-config-script.js", isModule: false); } - - - private void GenerateConfigScript() + private void GenerateConfigFile(string fileName, bool isModule) { - var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, "uno-config-script.js"); + var self = isModule ? "" : "self."; + + var unoConfigJsPath = Path.Combine(_intermediateAssetsPath, fileName); using (var w = new StreamWriter(unoConfigJsPath, false, _utf8Encoding)) { @@ -657,7 +562,7 @@ private void GenerateConfigScript() .Select(f => f.GetMetadata("Link") .Replace("\\", "/") .Replace("wwwroot/", "")) - .Concat([$"uno-config-script.js", "_framework/blazor.boot.json", "."]); + .Concat([fileName, "_framework/blazor.boot.json", "."]); var offlineFiles = enablePWA ? string.Join(", ", sanitizedOfflineFiles.Select(f => $"\"{WebAppBasePath}{f}\"")) : ""; @@ -667,27 +572,36 @@ private void GenerateConfigScript() var runtimeOptionsSet = string.Join(",", (RuntimeOptions?.Split(' ') ?? []).Select(f => $"\'{f}\'")); - config.AppendLine($"self.config = {{}};"); - config.AppendLine($"self.config.uno_remote_managedpath = \"_framework\";"); - config.AppendLine($"self.config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";"); - config.AppendLine($"self.config.uno_dependencies = [{dependencies}];"); - config.AppendLine($"self.config.uno_runtime_options = [{runtimeOptionsSet}];"); - config.AppendLine($"self.config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};"); - config.AppendLine($"self.config.offline_files = ['{WebAppBasePath}', {offlineFiles}];"); - config.AppendLine($"self.config.uno_shell_mode = \"{_shellMode}\";"); - config.AppendLine($"self.config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};"); - config.AppendLine($"self.config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};"); - config.AppendLine($"self.config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};"); - config.AppendLine($"self.config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];"); + + if (isModule) + { + config.AppendLine($"let config = {{}};"); + } + else + { + config.AppendLine($"{self}config = {{}};"); + } + + config.AppendLine($"{self}config.uno_remote_managedpath = \"_framework\";"); + config.AppendLine($"{self}config.uno_app_base = \"{WebAppBasePath}{PackageAssetsFolder}\";"); + config.AppendLine($"{self}config.uno_dependencies = [{dependencies}];"); + config.AppendLine($"{self}config.uno_runtime_options = [{runtimeOptionsSet}];"); + config.AppendLine($"{self}config.enable_pwa = {enablePWA.ToString().ToLowerInvariant()};"); + config.AppendLine($"{self}config.offline_files = ['{WebAppBasePath}', {offlineFiles}];"); + config.AppendLine($"{self}config.uno_shell_mode = \"{_shellMode}\";"); + config.AppendLine($"{self}config.uno_debugging_enabled = {(!Optimize).ToString().ToLowerInvariant()};"); + config.AppendLine($"{self}config.uno_enable_tracing = {EnableTracing.ToString().ToLowerInvariant()};"); + config.AppendLine($"{self}config.uno_load_all_satellite_resources = {LoadAllSatelliteResources.ToString().ToLowerInvariant()};"); + config.AppendLine($"{self}config.emcc_exported_runtime_methods = [{emccExportedRuntimeMethodsParams}];"); if (GenerateAOTProfile) { - config.AppendLine($"self.config.generate_aot_profile = true;"); + config.AppendLine($"{self}config.generate_aot_profile = true;"); } - config.AppendLine($"self.config.environmentVariables = self.config.environmentVariables || {{}};"); + config.AppendLine($"{self}config.environmentVariables = {self}config.environmentVariables || {{}};"); - void AddEnvironmentVariable(string name, string value) => config.AppendLine($"self.config.environmentVariables[\"{name}\"] = \"{value}\";"); + void AddEnvironmentVariable(string name, string value) => config.AppendLine($"{self}config.environmentVariables[\"{name}\"] = \"{value}\";"); if (MonoEnvironment != null) { @@ -719,6 +633,11 @@ private void GenerateConfigScript() AddEnvironmentVariable("UNO_BOOTSTRAP_LOG_PROFILER_OPTIONS", LogProfilerOptions); } + if (isModule) + { + config.AppendLine("export { config };"); + } + w.Write(config.ToString()); TaskItem indexMetadata = new(