feat(compartment-mapper): for commonjs alias package root to default module
MODIFY
packages/compartment-mapper/src/infer-exports.js
packages/compartment-mapper/src/infer-exports.js
@@ -157,7 +157,7 @@ export const inferExportsAndAliases = (
tags,
types,
) => {
- const { name, main, module, exports, browser } = descriptor;
+ const { name, type, main, module, exports, browser } = descriptor;
// collect externalAliases from exports and main/module
assign(
@@ -171,6 +171,10 @@ export const inferExportsAndAliases = (
if (module === undefined && exports === undefined) {
const defaultModule = main !== undefined ? relativize(main) : './index.js';
externalAliases['.'] = defaultModule;
+ // in commonjs, expose package root as default module
+ if (type !== 'module') {
+ internalAliases['.'] = defaultModule;
+ }
}
// if present, allow "browser" field to populate moduleMap

ADD
packages/compartment-mapper/test/fixtures-0/node_modules/excalibur/README.md
+This package covers the case where a commonjs package attempts to import the
+default module by referencing the package root.
+see https://github.com/endojs/endo/issues/1363

ADD
packages/compartment-mapper/test/fixtures-0/node_modules/excalibur/package.json
+{
+ "name": "excalibur",
+ "version": "1.0.0",
+ "main": "./src/stone.js",
+ "scripts": {
+ "preinstall": "echo DO NOT INSTALL TEST FIXTURES; exit -1"
+ }
+}

ADD
packages/compartment-mapper/test/fixtures-0/node_modules/excalibur/src/stone.js
+exports.sword = (
+`/
+O===[====================-
+\\`
+);
\ No newline at end of file

ADD
packages/compartment-mapper/test/fixtures-0/node_modules/excalibur/src/sword.js
+const { sword } = require('..');
+exports.sword = sword;
\ No newline at end of file

MODIFY
packages/compartment-mapper/test/test-main.js
packages/compartment-mapper/test/test-main.js
@@ -225,3 +225,17 @@ scaffold(
},
1,
);
+
+scaffold(
+ 'package root as main via internalAlias',
+ test,
+ new URL(
+ 'fixtures-0/node_modules/excalibur/src/sword.js',
+ import.meta.url,
+ ).toString(),
+ (t, { namespace: { sword } }) => {
+ t.is(typeof sword, 'string');
+ t.is(sword.length, 30);
+ },
+ 2,
+);

feat(compartment-mapper): for commonjs alias package root to default module