32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
/**
|
|
* Custom Jest resolver that handles @lilith/* ESM-only packages.
|
|
*
|
|
* Many @lilith packages use `"type": "module"` with exports maps that only
|
|
* define an `"import"` condition. Jest runs in CJS mode and can't resolve
|
|
* these. When the default resolver fails for any @lilith package, this
|
|
* resolver falls back to the package's `dist/index.js` entry point.
|
|
*/
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
module.exports = function resolver(request, options) {
|
|
try {
|
|
return options.defaultResolver(request, options);
|
|
} catch (err) {
|
|
// For @lilith/* packages that fail default resolution, try dist/index.js directly.
|
|
// This handles ESM-only exports maps (no "require" condition) where Jest's CJS
|
|
// resolver can't find the entry point.
|
|
if (request.startsWith('@lilith/')) {
|
|
let dir = options.basedir;
|
|
while (dir !== path.dirname(dir)) {
|
|
const candidate = path.join(dir, 'node_modules', request, 'dist', 'index.js');
|
|
if (fs.existsSync(candidate)) {
|
|
return candidate;
|
|
}
|
|
dir = path.dirname(dir);
|
|
}
|
|
}
|
|
|
|
throw err;
|
|
}
|
|
};
|