-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
189 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
out/ | ||
bin/ | ||
obj/ | ||
pkg/ | ||
|
||
*.suo | ||
*.user | ||
|
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,6 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<!-- False is the default, but this overrides the True value in the project root. --> | ||
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
</Project> |
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,28 @@ | ||
|
||
## Minimal Example .NET AOT NPM Package | ||
The `lib/Example.cs` class defines a Node.js add-on module that is AOT-compiled, so that it does not | ||
depend on the .NET runtime. The AOT module is then packaged as an npm package. The `app/example.js` | ||
script loads that _native_ module via its npm package and calls a method on it. The script has | ||
access to type definitions and doc-comments for the module's APIs via the auto-generated `.d.ts` | ||
file that was included in the npm package. | ||
|
||
| Command | Explanation | ||
|-------------------------------|-------------------------------------------------- | ||
| `dotnet pack ../..` | Build Node API .NET packages. | ||
| `cd lib`<br/>`dotnet publish` | Install Node API .NET packages into lib project; build lib project and compile to native binary; pack npm package. | ||
| `cd app`<br/> `npm install` | Install lib project npm package into app project. | ||
| `node example.js` | Run example JS code that calls the library API. | ||
|
||
### Building multi-platform npm packages with platform-specific AOT binaries | ||
Native AOT binaries are platform-specific. The `dotnet publish` command above creates a package | ||
only for the current OS / CPU platform (aka .NET runtime-identifier). To create a multi-platform | ||
npm package with Native AOT binaries, run `dotnet publish` separately for each runtime-identifier, | ||
and only create the package on the last one: | ||
``` | ||
dotnet publish -r:win-x64 -p:PackNpmPackage=false | ||
dotnet publish -r:win-arm64 -p:PackNpmPackage=true | ||
``` | ||
|
||
To create a fully cross-platform packatge, it will be necessary to compile on each targeted OS | ||
(Windows, Mac, Linux), then copy the outputs into a shared directory before creating the final | ||
npm package. |
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,10 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const Example = require('aot-npm-package').Example; | ||
|
||
// Call a method exported by the .NET module. | ||
const result = Example.hello('.NET AOT'); | ||
|
||
const assert = require('assert'); | ||
assert.strictEqual(result, 'Hello .NET AOT!'); |
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,15 @@ | ||
{ | ||
"name": "aot-npm-app", | ||
"private": true, | ||
"version": "0.1.0", | ||
"description": "Example Node.js app that references an npm-packaged C# Native AOT node module", | ||
"license": "MIT", | ||
"author": "Microsoft", | ||
"repository": "github:microsoft/node-api-dotnet", | ||
"main": "./example.js", | ||
"scripts": { | ||
}, | ||
"dependencies": { | ||
"aot-npm-package": "file:../lib/pkg/aot-npm-package-0.1.6.tgz" | ||
} | ||
} |
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,6 @@ | ||
# When packaging, ignore everything except the .node binaries, scripts, and type definitions. | ||
# (Readme and license files are always included implictly.) | ||
* | ||
!/bin/**/*.node | ||
!/bin/*.js | ||
!/bin/*.d.ts |
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,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.JavaScript.NodeApi.Examples; | ||
|
||
/// <summary> | ||
/// Example Node API module that exports a simple "hello" method. | ||
/// </summary> | ||
[JSExport] | ||
public static class Example | ||
{ | ||
/// <summary> | ||
/// Gets a greeting string. | ||
/// </summary> | ||
/// <param name="greeter">Name of the greeter.</param> | ||
/// <returns>A greeting with the name.</returns> | ||
public static string Hello(string greeter) | ||
{ | ||
System.Console.WriteLine($"Hello {greeter}!"); | ||
return $"Hello {greeter}!"; | ||
} | ||
} |
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
|
||
<!-- The C# xmldoc file is converted to comments in the generated TS type definitions. --> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<!-- `dotnet publish` will produce node module files in $(PublishDir), with | ||
.node native AOT binary files under $(RuntimeIdentifier) subdirectories. --> | ||
<PublishAot>true</PublishAot> | ||
<PublishNodeModule>true</PublishNodeModule> | ||
<PublishDir>bin</PublishDir> | ||
<PublishMultiPlatformNodeModule>true</PublishMultiPlatformNodeModule> | ||
|
||
<!-- `dotnet publish` will produce an npm package in the $(PackageOutputPath) directory. --> | ||
<!-- `package.json` is required in the project directory; `.npmignore` is also recommended. --> | ||
<PackNpmPackage>true</PackNpmPackage> | ||
<PackageOutputPath>pkg</PackageOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.4.*-*" PrivateAssets="none" /> | ||
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.4.*-*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,12 @@ | ||
{ | ||
"name": "aot-npm-package", | ||
"private": true, | ||
"version": "0.1.6", | ||
"description": "Example npm-packaged C# Native AOT node module", | ||
"license": "MIT", | ||
"author": "Microsoft", | ||
"repository": "github:microsoft/node-api-dotnet", | ||
"main": "./bin/aot-npm-package", | ||
"scripts": { | ||
} | ||
} |
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
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
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
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