Skip to content

Commit 348c8fd

Browse files
authored
Improve module typedefs and loader generation (#265)
1 parent 53328f5 commit 348c8fd

File tree

98 files changed

+831
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+831
-189
lines changed

Docs/dynamic-invoke.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ For examples of this scenario, see
1111
<PropertyGroup>
1212
<TargetFramework>net6.0</TargetFramework>
1313
<OutDir>bin</OutDir>
14-
<NodeApiAssemblyJSModuleType>commonjs</NodeApiAssemblyJSModuleType>
1514
<GenerateNodeApiTypeDefinitionsForReferences>true</GenerateNodeApiTypeDefinitionsForReferences>
1615
</PropertyGroup>
1716
<ItemGroup>
18-
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.5.*" />
17+
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.7.*" />
1918
<PackageReference Include="Example.Package" Version="1.2.3" />
2019
<PackageReference Include="Example.Package.Two" Version="2.3.4" />
2120
</ItemGroup>
@@ -27,15 +26,15 @@ For examples of this scenario, see
2726
will be placed there.
2827
- The `Microsoft.JavaScript.NodeApi.Generator` package reference enables automatic generation
2928
of TS type-definitions for the referenced assemblies.
30-
- Change `NodeApiAssemblyJSModuleType` to `esm` if using ES modules.
3129

3230
Build the project to restore the packages, place assemblies in the `bin` directory, and generate
3331
type definitions:
3432
```
3533
dotnet build
3634
```
3735

38-
2. Add a dependency on the `node-api-dotnet` npm package to your JavaScript project:
36+
2. Create a `package.json` file for the project, if you haven't already. Then add a dependency on
37+
the `node-api-dotnet` npm package to your JavaScript project:
3938
```
4039
npm install node-api-dotnet
4140
```

Docs/node-module.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ For a minimal example of this scenario, see
2323
Afterward you should have the two references in your project file:
2424
```xml
2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.4.*-*" />
27-
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.4.*-*" />
26+
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.7.*-*" />
27+
<PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.7.*-*" />
2828
</ItemGroup>
2929
```
3030

examples/aot-module/example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const Example = require('./bin/aot-module').Example;
4+
import { Example } from './bin/aot-module.js';
55

66
// Call a method exported by the .NET module.
77
const result = Example.hello('.NET AOT');
88

9-
const assert = require('assert');
9+
import assert from 'assert';
1010
assert.strictEqual(result, 'Hello .NET AOT!');

examples/aot-module/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "node-api-dotnet-examples-aot-module",
3+
"type": "module"
4+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const Example = require('aot-npm-package').Example;
4+
import { Example } from 'aot-npm-package';
55

66
// Call a method exported by the .NET module.
77
const result = Example.hello('.NET AOT');
88

9-
const assert = require('assert');
9+
import assert from 'node:assert';
1010
assert.strictEqual(result, 'Hello .NET AOT!');

examples/aot-npm-package/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"author": "Microsoft",
88
"repository": "github:microsoft/node-api-dotnet",
99
"main": "./example.js",
10+
"type": "module",
1011
"scripts": {
1112
},
1213
"dependencies": {
13-
"aot-npm-package": "file:../lib/pkg/aot-npm-package-0.1.6.tgz"
14+
"aot-npm-package": "file:../lib"
1415
}
1516
}

examples/aot-npm-package/lib/aot-npm-package.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
<!-- The C# xmldoc file is converted to comments in the generated TS type definitions. -->
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88

9-
<!-- `dotnet publish` will produce node module files in $(PublishDir), with
10-
.node native AOT binary files under $(RuntimeIdentifier) subdirectories. -->
9+
<!-- `dotnet publish` will produce node module files in $(PublishDir) -->
1110
<PublishAot>true</PublishAot>
1211
<PublishNodeModule>true</PublishNodeModule>
1312
<PublishDir>bin</PublishDir>
13+
14+
<!-- Place .node native AOT binary files under $(RuntimeIdentifier) subdirectories.
15+
This could support packaging multiple platform-specific binaries in a single npm package
16+
(though that would require building them each separately then merging the results). -->
1417
<PublishMultiPlatformNodeModule>true</PublishMultiPlatformNodeModule>
1518

1619
<!-- `dotnet publish` will produce an npm package in the $(PackageOutputPath) directory. -->
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "aot-npm-package",
33
"private": true,
4-
"version": "0.1.6",
4+
"version": "0.2",
55
"description": "Example npm-packaged C# Native AOT node module",
66
"license": "MIT",
77
"author": "Microsoft",
88
"repository": "github:microsoft/node-api-dotnet",
9-
"main": "./bin/aot-npm-package",
9+
"type": "module",
10+
"main": "./bin/aot-npm-package.js",
1011
"scripts": {
1112
}
1213
}

examples/dotnet-module/example.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const dotnet = require('node-api-dotnet');
5-
6-
/** @type {import('./bin/dotnet-module').Example} */
7-
const Example = dotnet.require('./bin/dotnet-module').Example;
4+
const Example = require('./bin/dotnet-module').Example;
85

96
// Call a method exported by the .NET module.
107
const result = Example.hello('.NET');

examples/dotnet-module/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "node-api-dotnet-examples-dotnet-module",
3+
"type": "commonjs",
34
"dependencies": {
45
"node-api-dotnet": "file:../../out/pkg/node-api-dotnet"
56
}

0 commit comments

Comments
 (0)