fix(compartment-mapper): inferred exports are relative
MODIFY
packages/compartment-mapper/src/infer-exports.js
packages/compartment-mapper/src/infer-exports.js
@@ -12,7 +12,7 @@ const { isArray } = Array;
* @param {Object} exports - the `exports` field from a package.json
* @yields {[string, string]}
*/
-function* interpretBrowserExports(name, exports) {
+export function* interpretBrowserExports(name, exports) {
if (typeof exports === 'string') {
yield [name, relativize(exports)];
return;
@@ -55,7 +55,11 @@ function* interpretExports(name, exports, tags) {
}
for (const [key, value] of entries(exports)) {
if (key.startsWith('./') || key === '.') {
+ if (name === '.') {
+ yield* interpretExports(key, value, tags);
+ } else {
yield* interpretExports(join(name, key), value, tags);
+ }
} else if (tags.has(key)) {
yield* interpretExports(name, value, tags);
}
@@ -73,10 +77,8 @@ function* interpretExports(name, exports, tags) {
* ascending priority order, and the caller should use the last one that exists.
*
* @param {Object} packageDescriptor - the parsed body of a package.json file.
- * @param {string} packageDescriptor.name
* @param {string} packageDescriptor.main
* @param {string} [packageDescriptor.module]
- * @param {string} [packageDescriptor.browser]
* @param {Object} [packageDescriptor.exports]
* @param {Set<string>} tags - build tags about the target environment
* for selecting relevant exports, e.g., "browser" or "node".
@@ -85,7 +87,7 @@ function* interpretExports(name, exports, tags) {
* @yields {[string, string]}
*/
export const inferExportsEntries = function* inferExportsEntries(
- { name, main, module, browser, exports },
+ { main, module, exports },
tags,
types,
) {
@@ -98,14 +100,12 @@ export const inferExportsEntries = function* inferExportsEntries(
// specifier extension.
const spec = relativize(module);
types[spec] = 'mjs';
- yield [name, spec];
- } else if (browser !== undefined && tags.has('browser')) {
- yield* interpretBrowserExports(name, browser);
+ yield ['.', spec];
} else if (main !== undefined) {
- yield [name, relativize(main)];
+ yield ['.', relativize(main)];
}
if (exports !== undefined) {
- yield* interpretExports(name, exports, tags);
+ yield* interpretExports('.', exports, tags);
}
// TODO Otherwise, glob 'files' for all '.js', '.cjs', and '.mjs' entry
// modules, taking care to exclude node_modules.

MODIFY
packages/compartment-mapper/src/node-modules.js
packages/compartment-mapper/src/node-modules.js
@@ -39,6 +39,7 @@ import { searchDescriptor } from './search.js';
import { parseLocatedJson } from './json.js';
import { unpackReadPowers } from './powers.js';
import { pathCompare } from './compartment-map.js';
+import { join } from './node-module-specifier.js';
const { assign, create, keys, values } = Object;
@@ -518,11 +519,14 @@ const translateGraph = (
*/
const digest = (dependencyName, packageLocation) => {
const { exports, explicitExports } = graph[packageLocation];
- for (const exportName of keys(exports).sort()) {
- const module = exports[exportName];
- modules[exportName] = {
+ for (const exportPath of keys(exports).sort()) {
+ const targetPath = exports[exportPath];
+ // dependency name may be different from package's name,
+ // as in the case of browser field dependency replacements
+ const localPath = join(dependencyName, exportPath);
+ modules[localPath] = {
compartment: packageLocation,
- module,
+ module: targetPath,
};
}
// if the exports field is not present, then all modules must be accessible

MODIFY
packages/compartment-mapper/test/snapshots/test-infer-exports.js.md
packages/compartment-mapper/test/snapshots/test-infer-exports.js.md
@@ -10,8 +10,8 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- '@floating-ui/dom': './dist/floating-ui.dom.esm.js',
- '@floating-ui/dom/package.json': './package.json',
+ '.': './dist/floating-ui.dom.esm.js',
+ './package.json': './package.json',
},
types: {
'./dist/floating-ui.dom.esm.js': 'mjs',
@@ -24,12 +24,12 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- underscore: './underscore-umd.js',
- 'underscore/amd/*': './amd/*',
- 'underscore/cjs/*': './cjs/*',
- 'underscore/modules/*': './modules/*',
- 'underscore/package.json': './package.json',
- 'underscore/underscore*': './underscore*',
+ '.': './underscore-umd.js',
+ './amd/*': './amd/*',
+ './cjs/*': './cjs/*',
+ './modules/*': './modules/*',
+ './package.json': './package.json',
+ './underscore*': './underscore*',
},
types: {
'./modules/index-all.js': 'mjs',
@@ -42,14 +42,14 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- vue: './dist/vue.runtime.esm-bundler.js',
- 'vue/compiler-sfc': './compiler-sfc/index.mjs',
- 'vue/dist/*': './dist/*',
- 'vue/macros': './macros.d.ts',
- 'vue/macros-global': './macros-global.d.ts',
- 'vue/package.json': './package.json',
- 'vue/ref-macros': './ref-macros.d.ts',
- 'vue/server-renderer': './server-renderer/index.mjs',
+ '.': './dist/vue.runtime.esm-bundler.js',
+ './compiler-sfc': './compiler-sfc/index.mjs',
+ './dist/*': './dist/*',
+ './macros': './macros.d.ts',
+ './macros-global': './macros-global.d.ts',
+ './package.json': './package.json',
+ './ref-macros': './ref-macros.d.ts',
+ './server-renderer': './server-renderer/index.mjs',
},
types: {
'./dist/vue.runtime.esm-bundler.js': 'mjs',
@@ -62,10 +62,10 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- 'socket.io-client': './build/esm/index.js',
- 'socket.io-client/dist/socket.io.js': './dist/socket.io.js',
- 'socket.io-client/dist/socket.io.js.map': './dist/socket.io.js.map',
- 'socket.io-client/package.json': './package.json',
+ '.': './build/esm/index.js',
+ './dist/socket.io.js': './dist/socket.io.js',
+ './dist/socket.io.js.map': './dist/socket.io.js.map',
+ './package.json': './package.json',
},
types: {
'./build/esm/index.js': 'mjs',
@@ -78,8 +78,8 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- 'babel-plugin-polyfill-es-shims': './lib/index.js',
- 'babel-plugin-polyfill-es-shims/package.json': './package.json',
+ '.': './lib/index.js',
+ './package.json': './package.json',
},
types: {},
}
@@ -90,8 +90,8 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- color2k: './dist/index.exports.require.cjs.js',
- 'color2k/package.json': './package.json',
+ '.': './dist/index.exports.require.cjs.js',
+ './package.json': './package.json',
},
types: {
'./dist/index.module.es.js': 'mjs',
@@ -104,37 +104,37 @@ Generated by [AVA](https://avajs.dev).
{
exports: {
- '@noble/hashes/_sha2': './_sha2.js',
- '@noble/hashes/blake2b': './blake2b.js',
- '@noble/hashes/blake2b.d.ts': './blake2b.d.ts',
- '@noble/hashes/blake2s': './blake2s.js',
- '@noble/hashes/blake2s.d.ts': './blake2s.d.ts',
- '@noble/hashes/blake3': './blake3.js',
- '@noble/hashes/blake3.d.ts': './blake3.d.ts',
- '@noble/hashes/crypto': './crypto.js',
- '@noble/hashes/eskdf': './eskdf.js',
- '@noble/hashes/eskdf.d.ts': './eskdf.d.ts',
- '@noble/hashes/hkdf': './hkdf.js',
- '@noble/hashes/hkdf.d.ts': './hkdf.d.ts',
- '@noble/hashes/hmac': './hmac.js',
- '@noble/hashes/hmac.d.ts': './hmac.d.ts',
- '@noble/hashes/index': './index.js',
- '@noble/hashes/pbkdf2': './pbkdf2.js',
- '@noble/hashes/pbkdf2.d.ts': './pbkdf2.d.ts',
- '@noble/hashes/ripemd160': './ripemd160.js',
- '@noble/hashes/ripemd160.d.ts': './ripemd160.d.ts',
- '@noble/hashes/scrypt': './scrypt.js',
- '@noble/hashes/scrypt.d.ts': './scrypt.d.ts',
- '@noble/hashes/sha256': './sha256.js',
- '@noble/hashes/sha256.d.ts': './sha256.d.ts',
- '@noble/hashes/sha3': './sha3.js',
- '@noble/hashes/sha3-addons': './sha3-addons.js',
- '@noble/hashes/sha3-addons.d.ts': './sha3-addons.d.ts',
- '@noble/hashes/sha3.d.ts': './sha3.d.ts',
- '@noble/hashes/sha512': './sha512.js',
- '@noble/hashes/sha512.d.ts': './sha512.d.ts',
- '@noble/hashes/utils': './utils.js',
- '@noble/hashes/utils.d.ts': './utils.d.ts',
+ './_sha2': './_sha2.js',
+ './blake2b': './blake2b.js',
+ './blake2b.d.ts': './blake2b.d.ts',
+ './blake2s': './blake2s.js',
+ './blake2s.d.ts': './blake2s.d.ts',
+ './blake3': './blake3.js',
+ './blake3.d.ts': './blake3.d.ts',
+ './crypto': './crypto.js',
+ './eskdf': './eskdf.js',
+ './eskdf.d.ts': './eskdf.d.ts',
+ './hkdf': './hkdf.js',
+ './hkdf.d.ts': './hkdf.d.ts',
+ './hmac': './hmac.js',
+ './hmac.d.ts': './hmac.d.ts',
+ './index': './index.js',
+ './pbkdf2': './pbkdf2.js',
+ './pbkdf2.d.ts': './pbkdf2.d.ts',
+ './ripemd160': './ripemd160.js',
+ './ripemd160.d.ts': './ripemd160.d.ts',
+ './scrypt': './scrypt.js',
+ './scrypt.d.ts': './scrypt.d.ts',
+ './sha256': './sha256.js',
+ './sha256.d.ts': './sha256.d.ts',
+ './sha3': './sha3.js',
+ './sha3-addons': './sha3-addons.js',
+ './sha3-addons.d.ts': './sha3-addons.d.ts',
+ './sha3.d.ts': './sha3.d.ts',
+ './sha512': './sha512.js',
+ './sha512.d.ts': './sha512.d.ts',
+ './utils': './utils.js',
+ './utils.d.ts': './utils.d.ts',
},
types: {},
}

MODIFY
packages/compartment-mapper/test/snapshots/test-infer-exports.js.snap
packages/compartment-mapper/test/snapshots/test-infer-exports.js.snap
Binary files a/packages/compartment-mapper/test/snapshots/test-infer-exports.js.snap and b/packages/compartment-mapper/test/snapshots/test-infer-exports.js.snap differ

fix(compartment-mapper): inferred exports are relative