[release/8.0] [nodejs] Remove experimental wasm arguments from template (#91401)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fri, 1 Sep 2023 18:31:41 +0000 (12:31 -0600)
committerGitHub <noreply@github.com>
Fri, 1 Sep 2023 18:31:41 +0000 (12:31 -0600)
* Remove experimental wasm arguments from template and add them to features.md

* Fix WBT

* Use lowercase host in runtimeconfig.template.json

* Override runtimeconfig only for wasmconsole

---------

Co-authored-by: Marek Fišera <mara@neptuo.com>
Co-authored-by: Marek Safar <marek.safar@gmail.com>
src/mono/wasm/Wasm.Build.Tests/WasmTemplateTestBase.cs
src/mono/wasm/features.md
src/mono/wasm/templates/templates/console/runtimeconfig.template.json

index 6280a02..a7ae3fb 100644 (file)
@@ -3,7 +3,9 @@
 
 #nullable enable
 
+using System;
 using System.IO;
+using System.Text.Json.Nodes;
 using Xunit.Abstractions;
 
 namespace Wasm.Build.Tests;
@@ -45,18 +47,38 @@ public abstract class WasmTemplateTestBase : BuildTestBase
         if (runAnalyzers)
             extraProperties += "<RunAnalyzers>true</RunAnalyzers>";
 
-        // TODO: Can be removed after updated templates propagate in.
-        string extraItems = string.Empty;
-        if (template == "wasmbrowser")
-            extraItems += "<WasmExtraFilesToDeploy Include=\"main.js\" />";
-        else
-            extraItems += "<WasmExtraFilesToDeploy Include=\"main.mjs\" />";
+        if (template == "wasmconsole")
+        {
+            UpdateRuntimeconfigTemplateForNode(_projectDir);
+        }
 
-        AddItemsPropertiesToProject(projectfile, extraProperties, extraItems);
+        AddItemsPropertiesToProject(projectfile, extraProperties);
 
         return projectfile;
     }
 
+    private static void UpdateRuntimeconfigTemplateForNode(string projectDir)
+    {
+        // TODO: Can be removed once Node >= 20
+
+        string runtimeconfigTemplatePath = Path.Combine(projectDir, "runtimeconfig.template.json");
+        string runtimeconfigTemplateContent = File.ReadAllText(runtimeconfigTemplatePath);
+        var runtimeconfigTemplate = JsonObject.Parse(runtimeconfigTemplateContent);
+        if (runtimeconfigTemplate == null)
+            throw new Exception($"Unable to parse runtimeconfigtemplate at '{runtimeconfigTemplatePath}'");
+
+        var perHostConfigs = runtimeconfigTemplate?["wasmHostProperties"]?["perHostConfig"]?.AsArray();
+        if (perHostConfigs == null || perHostConfigs.Count == 0 || perHostConfigs[0] == null)
+            throw new Exception($"Unable to find perHostConfig in runtimeconfigtemplate at '{runtimeconfigTemplatePath}'");
+
+        perHostConfigs[0]!["host-args"] = new JsonArray(
+            "--experimental-wasm-simd",
+            "--experimental-wasm-eh"
+        );
+
+        File.WriteAllText(runtimeconfigTemplatePath, runtimeconfigTemplate!.ToString());
+    }
+
     public (string projectDir, string buildOutput) BuildTemplateProject(BuildArgs buildArgs,
         string id,
         BuildProjectOptions buildProjectOptions)
index c131002..d3d85f2 100644 (file)
@@ -292,6 +292,24 @@ A WebAssembly application that works well on desktop PCs browser may take minute
 ### Shell environments - NodeJS & V8
 While our primary target is web browsers, we have partial support for Node.JS v14 sufficient to pass most of our automated tests. We also have partial support for the D8 command-line shell, version 11 or higher, sufficient to pass most of our automated tests. Both of these environments may lack support for features that are available in the browser.
 
+#### NodeJS < 20
+Until node version 20, you may need to pass these arguments when running the application `--experimental-wasm-simd --experimental-wasm-eh`. When you run the application using `dotnet run`, you can add these to the runtimeconfig template
+
+```json
+"wasmHostProperties": {
+    "perHostConfig": [
+        {
+            "name": "node",
+            ...
+            "host-args": [
+                "--experimental-wasm-simd", // 👈 Enable SIMD support
+                "--experimental-wasm-eh" // 👈 Enable exception handling support
+            ]
+        }
+    ]
+}
+```
+
 ## Choosing the right platform target
 Every end user has different needs, so the right platform for every application may differ.
 
index 49721fa..a056e67 100644 (file)
@@ -4,12 +4,8 @@
             {
                 "name": "node",
                 "js-path": "main.mjs",
-                "Host": "nodejs",
-                "host-args": [
-                    "--experimental-wasm-simd",
-                    "--experimental-wasm-eh"
-                ]
+                "host": "nodejs"
             }
         ]
     }
-}
+}
\ No newline at end of file