nx/packages/jest/plugins/resolver.ts
Leosvel Pérez Espinosa 48cd50a550
feat(core): use custom resolution to resolve from source local plugins with artifacts pointing to the outputs (#29222)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

Local Nx plugins in the new TS setup can't be resolved properly if they
aren't built first. Graph plugins can't be built either because the
graph is needed to run a task, but the plugin must be built to construct
the graph.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Local Nx plugins should work in the new TS setup. A custom resolution is
added to resolve the local plugin artifacts from the source.

It will try to use a `development` condition from the `exports` entry in
`package.json` if it exists. If it doesn't, it will fall back to guess
the source based on the artifact path and some commonly known/used
source dirs: `.`, `./src`, `./src/lib`.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-12-13 10:49:54 -05:00

81 lines
2.4 KiB
TypeScript

import { dirname, extname, join, resolve } from 'path';
import { resolve as resolveExports } from 'resolve.exports';
import type { ResolverOptions } from 'jest-resolve';
let compilerSetup;
let ts;
function getCompilerSetup(rootDir: string) {
const tsConfigPath =
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.spec.json') ||
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.test.json') ||
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.jest.json');
if (!tsConfigPath) {
console.error(
`Cannot locate a tsconfig.spec.json. Please create one at ${rootDir}/tsconfig.spec.json`
);
}
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
const config = ts.parseJsonConfigFileContent(
readResult.config,
ts.sys,
dirname(tsConfigPath)
);
const compilerOptions = config.options;
const host = ts.createCompilerHost(compilerOptions, true);
return { compilerOptions, host };
}
module.exports = function (path: string, options: ResolverOptions) {
const ext = extname(path);
if (ext === '.css' || ext === '.scss' || ext === '.sass' || ext === '.less') {
return require.resolve('identity-obj-proxy');
}
try {
try {
// Try to use the defaultResolver with default options
return options.defaultResolver(path, options);
} catch {
// Try to use the defaultResolver with a packageFilter
return options.defaultResolver(path, {
...options,
packageFilter: (pkg) => ({
...pkg,
main: pkg.main || pkg.es2015 || pkg.module,
}),
pathFilter: (pkg) => {
if (!pkg.exports) {
return path;
}
return resolveExports(pkg, path)?.[0] || path;
},
});
}
} catch (e) {
if (
path === 'jest-sequencer-@jest/test-sequencer' ||
path === '@jest/test-sequencer' ||
path.startsWith('jest-sequencer-')
) {
return;
}
// Fallback to using typescript
ts = ts || require('typescript');
compilerSetup = compilerSetup || getCompilerSetup(options.rootDir);
const { compilerOptions, host } = compilerSetup;
const resolvedFileName = ts.resolveModuleName(
path,
join(options.basedir, 'fake-placeholder.ts'),
compilerOptions,
host
).resolvedModule?.resolvedFileName;
if (!resolvedFileName) {
throw new Error(`Could not resolve ${path}`);
}
return resolve(options.rootDir, resolvedFileName);
}
};