Skip to content

Eliminate .NET version .js suffix in module import #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Docs/dynamic-invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ For examples of this scenario, see
```

To load a specific version of .NET, append the target framework moniker to the module name.
A `.js` suffix is required when using ES modules, optional with CommonJS.
```JavaScript
import dotnet from 'node-api-dotnet/net6.0.js'
import dotnet from 'node-api-dotnet/net6.0'
```
Currently the supported target frameworks are `net472`, `net6.0`, and `net8.0`.

Expand Down
3 changes: 1 addition & 2 deletions Docs/node-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ For a minimal example of this scenario, see
```

To load a specific version of .NET, append the target framework moniker to the module name.
A `.js` suffix is required when using ES modules, optional with CommonJS.
```JavaScript
import dotnet from 'node-api-dotnet/net6.0.js'
import dotnet from 'node-api-dotnet/net6.0'
```
Currently the supported target frameworks are `net472`, `net6.0`, and `net8.0`.

Expand Down
13 changes: 13 additions & 0 deletions src/node-api-dotnet/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,17 @@ function writePackageJson(packageStageDir, packageJson) {
if (packageJson.dependencies && packageJson.dependencies['node-api-dotnet']) {
packageJson.dependencies['node-api-dotnet'] = buildVersion;
}

delete packageJson.scripts;

// Generate package entry-points for each of the supported target framework monikers.
// https://nodejs.org/api/packages.html#package-entry-points
if (packageJson.exports) {
for (let tfm of targetFrameworks) {
packageJson.exports[`./${tfm}`] = `./${tfm}.js`;
}
}

const stagedPackageJsonPath = path.join(packageStageDir, 'package.json');
fs.writeFileSync(stagedPackageJsonPath, JSON.stringify(packageJson, undefined, ' '));
console.log(stagedPackageJsonPath);
Expand Down Expand Up @@ -205,6 +215,9 @@ function generateTargetFrameworkScriptFiles(packageStageDir) {

function generateTargetFrameworkScriptFile(filePath, tfm) {
// Each generated entrypoint script uses `init.js` to request a specific target framework version.
// The exported module will be augmented with .NET namespaces from loaded assemblies.
// The module augmentation only works with CommonJS exports, because ESM exports are immutable.
// (ES modules can still import this CommonJS module.)
const js = `const initialize = require('./init');
module.exports = initialize(${tfm ? `'${tfm}'` : ''});
`;
Expand Down
5 changes: 4 additions & 1 deletion src/node-api-dotnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"name": "node-api-dotnet",
"version": "0.1.0",
"description": "Node-API bindings for .Net",
"main": "index.js",
"license": "MIT",
"author": "Microsoft",
"scripts": {
},
"type": "commonjs",
"exports": {
".": "./index.js"
},
"types": "./index.d.ts",
"keywords": [
"Node-API",
Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/napi-dotnet/ComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,6 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
public class Equatable : IEquatable<Equatable>
{
public bool Equals(Equatable? other) => other != null;
public override bool Equals(object obj) => obj is Equatable;
public override bool Equals(object? obj) => obj is Equatable;
public override int GetHashCode() => 1;
}
2 changes: 1 addition & 1 deletion test/TestCases/projects/js-esm/net472.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import assert from 'assert';
import dotnet from 'node-api-dotnet/net472.js';
import dotnet from 'node-api-dotnet/net472';

import './bin/mscorlib.js';

Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/projects/js-esm/net6.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import assert from 'assert';
import dotnet from 'node-api-dotnet/net6.0.js';
import dotnet from 'node-api-dotnet/net6.0';

import './bin/System.Runtime.js';
import './bin/System.Console.js';
Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/projects/js-esm/net8.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import assert from 'assert';
import dotnet from 'node-api-dotnet/net8.0.js';
import dotnet from 'node-api-dotnet/net8.0';

import './bin/System.Runtime.js';
import './bin/System.Console.js';
Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/projects/ts-esm/net472.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import * as assert from 'assert';
import dotnet from 'node-api-dotnet/net472.js';
import dotnet from 'node-api-dotnet/net472';

import './bin/mscorlib.js';

Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/projects/ts-esm/net6.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import * as assert from 'assert';
import dotnet from 'node-api-dotnet/net6.0.js';
import dotnet from 'node-api-dotnet/net6.0';

import './bin/System.Runtime.js';
import './bin/System.Console.js';
Expand Down
2 changes: 1 addition & 1 deletion test/TestCases/projects/ts-esm/net8.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import * as assert from 'assert';
import dotnet from 'node-api-dotnet/net8.0.js';
import dotnet from 'node-api-dotnet/net8.0';

import './bin/System.Runtime.js';
import './bin/System.Console.js';
Expand Down