forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wasm] Webpack & NextJS & TypeScript samples (dotnet#63335)
- nextJs sample - webpack sample - typescript sample Co-authored-by: campersau <buchholz.bastian@googlemail.com>
- Loading branch information
1 parent
ce2165d
commit 3591add
Showing
33 changed files
with
12,704 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Sample | ||
{ | ||
public class Test | ||
{ | ||
public static int Main() | ||
{ | ||
return 42; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Sample for React component in the NextJs app. | ||
|
||
Shows how to create react component and re-use runtime instance, when the component is instantiated multiple times. | ||
|
||
``` | ||
dotnet build /p:TargetOS=Browser /p:TargetArchitecture=wasm /p:Configuration=Debug /t:RunSample | ||
``` |
17 changes: 17 additions & 0 deletions
17
src/mono/sample/wasm/browser-nextjs/Wasm.Browser.NextJs.Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<WasmCopyAppZipToHelixTestDir>false</WasmCopyAppZipToHelixTestDir> | ||
<WasmMainJSPath>package.json</WasmMainJSPath> | ||
<WasmAppDir>public</WasmAppDir> | ||
</PropertyGroup> | ||
|
||
<Target Name="InstallNpmPackage" AfterTargets="WasmBuildApp" DependsOnTargets="Build"> | ||
<Exec Command="npm install" WorkingDirectory="$(MSBuildProjectDirectory)"/> | ||
<Exec Command="npm install $(MicrosoftNetCoreAppRuntimePackNativeDir) --no-save" WorkingDirectory="$(MSBuildProjectDirectory)"/> | ||
</Target> | ||
|
||
<Target Name="RunSample" DependsOnTargets="Build"> | ||
<Exec Command="npm run dev" WorkingDirectory="$(MSBuildProjectDirectory)"/> | ||
</Target> | ||
|
||
</Project> |
52 changes: 52 additions & 0 deletions
52
src/mono/sample/wasm/browser-nextjs/components/deepThought.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useState, useEffect } from 'react' | ||
import createDotnetRuntime from '@microsoft/dotnet-runtime' | ||
|
||
let dotnetRuntimePromise = undefined; | ||
let meaningFunction = undefined; | ||
|
||
async function createRuntime() { | ||
try { | ||
const response = await fetch('dotnet.wasm'); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
return createDotnetRuntime({ | ||
configSrc: "./mono-config.json", | ||
disableDotnet6Compatibility: true, | ||
scriptDirectory: "/", | ||
instantiateWasm: async (imports, successCallback) => { | ||
try { | ||
const arrayBufferResult = await WebAssembly.instantiate(arrayBuffer, imports); | ||
successCallback(arrayBufferResult.instance); | ||
} catch (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
} | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
throw err; | ||
} | ||
} | ||
|
||
async function dotnetMeaning() { | ||
if (!dotnetRuntimePromise) { | ||
dotnetRuntimePromise = createRuntime(); | ||
} | ||
const { BINDING } = await dotnetRuntimePromise; | ||
meaningFunction = BINDING.bind_static_method("[Wasm.Browser.NextJs.Sample] Sample.Test:Main"); | ||
return meaningFunction(); | ||
} | ||
|
||
export default function DeepThought() { | ||
const [meaning, setCount] = useState(undefined); | ||
|
||
useEffect(async () => { | ||
const meaning = await dotnetMeaning(); | ||
setCount(meaning); | ||
}, []); | ||
|
||
if (!meaning) { | ||
return (<div>DeepThought is thinking ....</div>); | ||
} | ||
return (<div>Answer to the Ultimate Question of Life, the Universe, and Everything is : {meaning}!</div>); | ||
}; |
Oops, something went wrong.