Skip to content

Commit 389b5c5

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

File tree

12 files changed

+54
-16
lines changed

12 files changed

+54
-16
lines changed

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Flags: --experimental-addon-modules
2+
'use strict';
3+
const common = require('../../common');
4+
const assert = require('node:assert');
5+
6+
import(`esm-package/${common.buildType}`)
7+
.then((mod) => {
8+
assert.strictEqual(mod.default.hello(), 'world');
9+
})
10+
.then(common.mustCall());
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Flags: --experimental-addon-modules
2+
'use strict';
3+
const common = require('../../common');
4+
const assert = require('node:assert');
5+
6+
require(`esm-package/${common.buildType}`)
7+
.then((mod) => {
8+
assert.strictEqual(mod.hello(), 'world');
9+
})
10+
.then(common.mustCall());
File renamed without changes.
File renamed without changes.

test/addons/esm-package/package.json

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+
}

test/addons/esm/test-esm.mjs renamed to test/addons/esm-package/test-esm.mjs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* This file is supposed to be loaded by `test-import.js` and `test-require.js`
3+
* to verify that `import('*.node')` is working properly either been loaded with
4+
* the ESM loader or the CJS loader.
5+
*/
6+
17
// eslint-disable-next-line node-core/require-common-first
28
import { buildType } from '../../common/index.mjs';
39
import assert from 'node:assert';
@@ -9,6 +15,7 @@ export async function run() {
915
// binding.node
1016
{
1117
const bindingPath = require.resolve(`./build/${buildType}/binding.node`);
18+
// Test with order of import+require
1219
const { default: binding, 'module.exports': exports } = await import(bindingPath);
1320
assert.strictEqual(binding, exports);
1421
assert.strictEqual(binding.hello(), 'world');
@@ -28,14 +35,14 @@ export async function run() {
2835
// binding-export-default.node
2936
{
3037
const bindingPath = require.resolve(`./build/${buildType}/binding-export-default.node`);
38+
// Test with order of require+import
39+
const bindingRequire = require(bindingPath);
3140
const ns = await import(bindingPath);
41+
assert.strictEqual(ns.default, bindingRequire);
3242

3343
// As same as ESM-import-CJS, the default export is the value of `module.exports`.
3444
assert.strictEqual(ns.default, ns['module.exports']);
3545
assert.strictEqual(ns.default.default(), 'hello world');
36-
37-
const bindingRequire = require(bindingPath);
38-
assert.strictEqual(ns.default, bindingRequire);
3946
}
4047

4148
// binding-export-primitive.node

0 commit comments

Comments
 (0)