Skip to content

Commit

Permalink
[New] --multi: ensure all declared subpackages have proper exports
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 25, 2024
1 parent 906b618 commit 8bf6ae2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"rules": {
"array-bracket-newline": 0,
"global-require": 1,
"max-nested-callbacks": 0,
"no-console": 0,
"no-invalid-this": 1,
},
Expand Down
47 changes: 43 additions & 4 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,18 @@ var validateModule = function validateAPIModule(t, nameOrFilePaths) {
var expectedKeys = isMulti
? ['.', './auto', './shim', './package.json']
: ['.', './auto', './polyfill', './implementation', './shim', './package.json'];
var keysToCheck = keys(pkg.exports).filter(function (key) {

var exportsKeys = keys(pkg.exports);

var keysToCheck = exportsKeys.filter(function (key) {
return includes(expectedKeys, key);
});
st.deepEqual(keysToCheck, expectedKeys, 'expected entrypoints are present in the proper order');

keysToCheck.forEach(function (key) {
exportsKeys.forEach(function (key) {
var rhs = pkg.exports[key];
var exists = [].concat(rhs).some(fs.existsSync);
st.ok(exists, 'entrypoint "' + key + '" points to "' + inspect(rhs) + '" which exists (or is an array with one item that exists)');
var exists = [].concat(rhs).some(existsSync);
st.ok(exists, 'entrypoint `' + key + '` points to `' + inspect(rhs) + '` which exists (or is an array with one item that exists)');
});

st.equal(pkg.exports['./package.json'], './package.json', 'package.json is exposed');
Expand Down Expand Up @@ -216,6 +219,42 @@ var validateModule = function validateAPIModule(t, nameOrFilePaths) {
var subPackageDir = path.join(path.dirname(name), subPackage);
doValidation(t, subPackageDir, subPackageDir);
});

t.test('subpackages, `exports` field', { skip: !('exports' in pkg) }, function (st) {
subPackages.forEach(function (subPackage) {
var subPackageLHS = [
'./' + subPackage,
'./' + subPackage + '/auto',
'./' + subPackage + '/polyfill',
'./' + subPackage + '/implementation',
'./' + subPackage + '/shim',
];

subPackageLHS.forEach(function (lhs) {
st.ok(lhs in pkg.exports, '`' + lhs + '` is in `exports`');
if (lhs in pkg.exports) {
var rhs = pkg.exports[lhs];
var resolved = path.resolve(path.join(packageDir, rhs));
var lhsGuess = './' + path.relative(
packageDir,
path.join(
path.dirname(resolved),
path.basename(resolved, path.extname(resolved))
)
).replace(/\/index$/, '');
st.equal(lhs, lhsGuess, 'subpackage `' + subPackage + '` LHS + `' + lhs + '` resolves to `' + lhsGuess + '`');
}
});

st.deepEqual(
keys(pkg.exports).filter(function (lhs) { return subPackageLHS.indexOf(lhs) > -1; }),
subPackageLHS,
'subpackage `' + subPackage + '` exports the expected entries in the proper order'
);
});

st.end();
});
} else {
doValidation(t, packageDir, name);
}
Expand Down

0 comments on commit 8bf6ae2

Please sign in to comment.