Add a switch to compile dotnet.js as ES6 module (#41964)
authorAdeel Mujahid <3840695+am11@users.noreply.github.com>
Mon, 28 Sep 2020 20:51:02 +0000 (23:51 +0300)
committerGitHub <noreply@github.com>
Mon, 28 Sep 2020 20:51:02 +0000 (20:51 +0000)
* Add a switch to compile dotnet.js as ES6 module

With `./build.sh -os Browser /p:WasmEnableES6=True`, we can now compile
`dotnet.js` as an ES6 module, which can be easily be loaded in
environments like Deno. e.g.:

```js
// myApp.js

import jsdom from "https://dev.jspm.io/jsdom";
window.document = new jsdom.JSDOM(null, {url:'http://localhost'}).window.document;

const dotnetModule = await import('./artifacts//bin/native/net5.0-Browser-Debug-wasm/dotnet.js');
// this is needed since we cannot pass scriptDirectory to upstream module (yet: https://github.com/emscripten-core/emscripten/pull/12120)
const bytes = Deno.readFileSync('./artifacts//bin/native/net5.0-Browser-Debug-wasm/dotnet.wasm', 'utf8');
const dotnet = await dotnetModule.default({wasmBinary: bytes}); // currently it calls run() on load

// dotnet.run(); // we can _additionally_ invoke run() on demand
```

Also waiting on Deno PR to merge with utf-16le suppot, workaround is:
```sh
$ ./build.sh -os Browser /p:WasmEnableES6=True
$ sed -i '' 's/utf-16le/utf8/' artifacts/bin/native/net5.0-Browser-Debug-wasm/dotnet.js
```
and then
```sh
$ deno run --allow-read myApp.js
```

* Set default value to False explicitly

* Use lower-case false for check

Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
src/mono/wasm/Makefile
src/mono/wasm/wasm.proj

index bcf54e1..4fc42d4 100644 (file)
@@ -5,7 +5,7 @@ DOTNET=$(TOP)/dotnet.sh
 JSVU=$(HOME)/.jsvu
 
 #
-# These variables are set by wasm.targets
+# These variables are set by wasm.proj
 #
 EMSDK_PATH?=$(TOP)/src/mono/wasm/emsdk
 CONFIG?=Release
@@ -15,6 +15,7 @@ PINVOKE_TABLE?=$(TOP)/artifacts/obj/wasm/pinvoke-table.h
 MONO_BIN_DIR?=$(BINDIR)/mono/Browser.wasm.$(CONFIG)
 NATIVE_BIN_DIR?=$(BINDIR)/native/net5.0-Browser-$(CONFIG)-wasm
 ICU_LIBDIR?=
+ENABLE_ES6?=false
 
 all: build-native icu-data
 
@@ -59,6 +60,10 @@ MONO_LIBS = \
        $(ICU_LIBDIR)/libicudata.a
 
 EMCC_FLAGS=--profiling-funcs -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s BINARYEN=1 -s ALIASING_FUNCTION_POINTERS=0 -s NO_EXIT_RUNTIME=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'FS_createPath', 'FS_createDataFile', 'cwrap', 'setValue', 'getValue', 'UTF8ToString', 'UTF8ArrayToString', 'addFunction']" -s "EXPORTED_FUNCTIONS=['_putchar']" --source-map-base http://example.com  -emit-llvm -s FORCE_FILESYSTEM=1 -s USE_ZLIB=1
+ifneq ($(ENABLE_ES6),false)
+    EMCC_FLAGS+=-s MODULARIZE=1 -s EXPORT_ES6=1
+endif
+
 EMCC_DEBUG_FLAGS =-g -Os -s ASSERTIONS=1 -DENABLE_NETCORE=1 -DDEBUG=1
 EMCC_RELEASE_FLAGS=-Oz --llvm-opts 2 -DENABLE_NETCORE=1
 
index 8d6ec29..4be1bcd 100644 (file)
@@ -11,6 +11,7 @@
     <MicrosoftNetCoreAppRuntimePackDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'microsoft.netcore.app.runtime.$(PackageRID)', '$(Configuration)'))</MicrosoftNetCoreAppRuntimePackDir>
     <MicrosoftNetCoreAppRuntimePackRidDir>$([MSBuild]::NormalizeDirectory('$(MicrosoftNetCoreAppRuntimePackDir)', 'runtimes', '$(PackageRID)'))</MicrosoftNetCoreAppRuntimePackRidDir>
     <MicrosoftNetCoreAppRuntimePackNativeDir>$([MSBuild]::NormalizeDirectory('$(MicrosoftNetCoreAppRuntimePackRidDir)', 'native'))</MicrosoftNetCoreAppRuntimePackNativeDir>
+    <WasmEnableES6 Condition="'$(WasmEnableES6)' == ''">false</WasmEnableES6>
   </PropertyGroup>
 
   <Target Name="CheckEnv">
@@ -83,7 +84,7 @@
           AfterTargets="Build"
           DependsOnTargets="BuildPInvokeTable;BundleTimeZones">
 
-    <Exec Command="make -C $(MonoProjectRoot)wasm all SHELL=/bin/bash BINDIR=$(ArtifactsBinDir) MONO_BIN_DIR=$(MonoArtifactsPath) OBJDIR=$(ArtifactsObjDir) NATIVE_BIN_DIR=$(NativeBinDir) CONFIG=$(Configuration) PINVOKE_TABLE=$(WasmPInvokeTablePath) ICU_LIBDIR=$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)/runtimes/browser-wasm/native/lib"
+    <Exec Command="make -C $(MonoProjectRoot)wasm all SHELL=/bin/bash BINDIR=$(ArtifactsBinDir) MONO_BIN_DIR=$(MonoArtifactsPath) OBJDIR=$(ArtifactsObjDir) NATIVE_BIN_DIR=$(NativeBinDir) CONFIG=$(Configuration) PINVOKE_TABLE=$(WasmPInvokeTablePath) ICU_LIBDIR=$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)/runtimes/browser-wasm/native/lib ENABLE_ES6=$(WasmEnableES6)"
           IgnoreStandardErrorWarningFormat="true" />
 
     <Copy SourceFiles="$(NativeBinDir)dotnet.js;