Skip to content

Commit

Permalink
chore: unify config files creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Dec 16, 2024
1 parent 6982c16 commit a485e9a
Showing 1 changed file with 38 additions and 119 deletions.
157 changes: 38 additions & 119 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ public override bool Execute()
BuildServiceWorkers();
GenerateEmbeddedJs();
GenerateIndexHtml();
GenerateConfig();
GenerateConfigScript();
GenerateConfigFiles();
RemoveDuplicateAssets();
}
finally
Expand Down Expand Up @@ -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<string, string>
{
["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))
{
Expand All @@ -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}\"")) : "";

Expand All @@ -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)
{
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a485e9a

Please sign in to comment.