Skip to content

Commit 7375fa0

Browse files
committed
fixup! esm: add experimental support for addon modules
1 parent 2ea455d commit 7375fa0

File tree

13 files changed

+65
-16
lines changed

13 files changed

+65
-16
lines changed

β€Ždoc/api/module.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ The final value of `format` must be one of the following:
957957
958958
| `format` | Description | Acceptable types for `source` returned by `load` |
959959
| ------------ | ------------------------------ | -------------------------------------------------------------------------- |
960+
| `'addon'` | Load a Node.js addon | { `null` } |
960961
| `'builtin'` | Load a Node.js builtin module | Not applicable |
961962
| `'commonjs'` | Load a Node.js CommonJS module | { [`string`][], [`ArrayBuffer`][], [`TypedArray`][], `null`, `undefined` } |
962963
| `'json'` | Load a JSON file | { [`string`][], [`ArrayBuffer`][], [`TypedArray`][] } |

β€Žlib/internal/modules/esm/translators.jsβ€Ž

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,14 @@ function createCJSNoSourceModuleWrap(url, isMain) {
245245

246246
// Addon export names are not known until the addon is loaded.
247247
const exportNames = ['default', 'module.exports'];
248-
return new ModuleWrap(url, undefined, exportNames, function() {
248+
return new ModuleWrap(url, undefined, exportNames, function evaluationCallback() {
249249
debug(`Loading CJSModule ${url}`);
250250

251251
if (!module.loaded) {
252252
wrapModuleLoad(filename, null, isMain);
253253
}
254254

255+
/** @type {import('./loader').ModuleExports} */
255256
let exports;
256257
if (module[kModuleExport] !== undefined) {
257258
exports = module[kModuleExport];
@@ -323,18 +324,18 @@ translators.set('commonjs', function commonjsStrategy(url, source, isMain) {
323324
*/
324325
function cjsEmplaceModuleCacheEntry(filename, exportNames) {
325326
// TODO: Do we want to keep hitting the user mutable CJS loader here?
326-
let module = CJSModule._cache[filename];
327-
if (module) {
328-
return module;
327+
let cjsMod = CJSModule._cache[filename];
328+
if (cjsMod) {
329+
return cjsMod;
329330
}
330331

331-
module = new CJSModule(filename);
332-
module.filename = filename;
333-
module.paths = CJSModule._nodeModulePaths(module.path);
334-
module[kIsCachedByESMLoader] = true;
335-
CJSModule._cache[filename] = module;
332+
cjsMod = new CJSModule(filename);
333+
cjsMod.filename = filename;
334+
cjsMod.paths = CJSModule._nodeModulePaths(cjsMod.path);
335+
cjsMod[kIsCachedByESMLoader] = true;
336+
CJSModule._cache[filename] = cjsMod;
336337

337-
return module;
338+
return cjsMod;
338339
}
339340

340341
/**
@@ -510,7 +511,7 @@ translators.set('wasm', async function(url, source) {
510511
});
511512

512513
// Strategy for loading a addon
513-
translators.set('addon', function(url, source, isMain) {
514+
translators.set('addon', function translateAddon(url, source, isMain) {
514515
emitExperimentalWarning('Importing addons');
515516

516517
// The addon must be loaded from file system with dlopen. Assert

β€Žtest/addons/esm-package-dependent/node_modules/esm-packageβ€Ž

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Flags: --experimental-addon-modules
2+
'use strict';
3+
const common = require('../../common');
4+
const assert = require('node:assert');
5+
6+
/**
7+
* Test that the export condition `node-addons` can be used
8+
* with `*.node` files with the ESM loader.
9+
*/
10+
11+
import(`esm-package/${common.buildType}`)
12+
.then((mod) => {
13+
assert.strictEqual(mod.default.hello(), 'world');
14+
})
15+
.then(common.mustCall());
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Flags: --experimental-addon-modules
2+
'use strict';
3+
const common = require('../../common');
4+
const assert = require('node:assert');
5+
6+
/**
7+
* Test that the export condition `node-addons` can be used
8+
* with `*.node` files with the CJS loader.
9+
*/
10+
11+
require(`esm-package/${common.buildType}`)
12+
.then((mod) => {
13+
assert.strictEqual(mod.hello(), 'world');
14+
})
15+
.then(common.mustCall());
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "esm-package",
3+
"exports": {
4+
"./Debug": {
5+
"node-addons": "./build/Debug/binding.node"
6+
},
7+
"./Release": {
8+
"node-addons": "./build/Release/binding.node"
9+
}
10+
}
11+
}

0 commit comments

Comments
Β (0)