Skip to content

Commit

Permalink
Fix ES module property import (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Apr 5, 2024
1 parent acc2c57 commit dbdbf87
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/NodeApi/Interop/JSRuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ public JSValue Import(
}
else
{
// Getting a property on an imported module.
JSValue moduleValue = Import(module, null);
// Getting a property on a module - import the module first.
JSValue moduleValue = Import(module, property: null, esModule);
if (esModule)
{
return new JSReference(((JSPromise)moduleValue).Then(
Expand Down
10 changes: 8 additions & 2 deletions test/NodejsEmbeddingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,24 @@ public async Task ImportESPackage()
await nodejs.RunAsync(async () =>
{
JSValue testModule = await nodejs.ImportAsync(
"./test-esm-package", null, esModule: true);
"./test-esm-package", esModule: true);
Assert.Equal(JSValueType.Object, testModule.TypeOf());
Assert.Equal(JSValueType.Function, testModule["test"].TypeOf());
Assert.Equal("test", testModule.CallMethod("test"));

// Check that module resolution handles sub-paths from conditional exports.
// https://nodejs.org/api/packages.html#conditional-exports
JSValue testModuleFeature = await nodejs.ImportAsync(
"./test-esm-package/feature", null, esModule: true);
"./test-esm-package/feature", esModule: true);
Assert.Equal(JSValueType.Object, testModuleFeature.TypeOf());
Assert.Equal(JSValueType.Function, testModuleFeature["test2"].TypeOf());
Assert.Equal("test2", testModuleFeature.CallMethod("test2"));

// Directly import a property from the module
JSValue testModuleProperty = await nodejs.ImportAsync(
"./test-esm-package", "test", esModule: true);
Assert.Equal(JSValueType.Function, testModuleProperty.TypeOf());
Assert.Equal("test", testModuleProperty.Call());
});

nodejs.Dispose();
Expand Down

0 comments on commit dbdbf87

Please sign in to comment.