fix(nx-plugin): fix plugin generation in ts solution setup (#29730)

<!-- 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 -->

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

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

Fixes #
This commit is contained in:
Leosvel Pérez Espinosa 2025-01-23 17:43:29 +01:00 committed by GitHub
parent bd35ab2892
commit 9d1c55b80e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 91 additions and 11 deletions

View File

@ -23,6 +23,62 @@ describe('Nx Plugin (TS solution)', () => {
afterAll(() => cleanupProject());
it('should be able to generate a Nx Plugin with generators, executors and migrations', async () => {
const plugin = uniq('plugin');
const generator = uniq('generator');
const executor = uniq('executor');
const migrationVersion = '1.0.0';
runCLI(
`generate @nx/plugin:plugin packages/${plugin} --linter=eslint --unitTestRunner=jest --e2eTestRunner=jest --publishable`
);
runCLI(
`generate @nx/plugin:generator packages/${plugin}/src/generators/${generator}/generator --name ${generator}`
);
runCLI(
`generate @nx/plugin:executor packages/${plugin}/src/executors/${executor}/executor --name ${executor} --includeHasher`
);
runCLI(
`generate @nx/plugin:migration packages/${plugin}/src/migrations/update-${migrationVersion}/update-${migrationVersion} --packageVersion=${migrationVersion} --packageJsonUpdates=false`
);
expect(runCLI(`lint ${plugin}`)).toContain(
`Successfully ran target lint for project ${plugin}`
);
expect(runCLI(`typecheck ${plugin}`)).toContain(
`Successfully ran target typecheck for project ${plugin}`
);
expect(runCLI(`build ${plugin}`)).toContain(
`Successfully ran target build for project ${plugin}`
);
checkFilesExist(
// entry point
`packages/${plugin}/dist/index.js`,
`packages/${plugin}/dist/index.d.ts`,
// generator
`packages/${plugin}/dist/generators/${generator}/schema.json`,
`packages/${plugin}/dist/generators/${generator}/schema.d.ts`,
`packages/${plugin}/dist/generators/${generator}/generator.js`,
`packages/${plugin}/dist/generators/${generator}/generator.d.ts`,
// executor
`packages/${plugin}/dist/executors/${executor}/schema.json`,
`packages/${plugin}/dist/executors/${executor}/schema.d.ts`,
`packages/${plugin}/dist/executors/${executor}/executor.js`,
`packages/${plugin}/dist/executors/${executor}/executor.d.ts`,
`packages/${plugin}/dist/executors/${executor}/hasher.js`,
`packages/${plugin}/dist/executors/${executor}/hasher.d.ts`,
// migration
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.js`,
`packages/${plugin}/dist/migrations/update-${migrationVersion}/update-${migrationVersion}.d.ts`
);
expect(runCLI(`test ${plugin}`)).toContain(
`Successfully ran target test for project ${plugin}`
);
expect(runCLI(`e2e ${plugin}-e2e`)).toContain(
`Successfully ran target e2e for project ${plugin}-e2e`
);
}, 90000);
it('should be able to infer projects and targets', async () => {
const plugin = uniq('plugin');
runCLI(`generate @nx/plugin:plugin packages/${plugin}`);

View File

@ -191,15 +191,27 @@ function normalizeOptions(
let outDir: string;
const base = { ...DEFAULT_OPTIONS, ...options };
let runtimeTsConfig: string;
try {
runtimeTsConfig = require.resolve(
path.join(sourceProject.data.root, base.tsConfig)
);
const tsConfig = readJsonFile(runtimeTsConfig);
rootDir = tsConfig.compilerOptions?.rootDir;
outDir = tsConfig.compilerOptions?.outDir;
} catch {
// nothing
if (sourceProject.data.targets?.build?.executor === '@nx/js:tsc') {
rootDir = sourceProject.data.targets.build.options.rootDir;
outDir = sourceProject.data.targets.build.options.outputPath;
}
if (!rootDir && !outDir) {
try {
runtimeTsConfig = require.resolve(
path.join(workspaceRoot, sourceProject.data.root, base.tsConfig)
);
const tsConfig = readJsonFile(runtimeTsConfig);
rootDir ??= tsConfig.compilerOptions?.rootDir
? path.join(sourceProject.data.root, tsConfig.compilerOptions.rootDir)
: undefined;
outDir ??= tsConfig.compilerOptions?.outDir
? path.join(sourceProject.data.root, tsConfig.compilerOptions.outDir)
: undefined;
} catch {
// nothing
}
}
const pathPrefix =
sourceProject.data.root !== '.' ? `${sourceProject.data.root}/` : '';
@ -217,8 +229,8 @@ function normalizeOptions(
packageJson: base.packageJson
? `${pathPrefix}${base.packageJson}`
: undefined,
rootDir: rootDir ? path.join(sourceProject.data.root, rootDir) : undefined,
outDir: outDir ? path.join(sourceProject.data.root, outDir) : undefined,
rootDir,
outDir,
};
}

View File

@ -8,6 +8,7 @@ import {
readProjectConfiguration,
runTasksInSerial,
Tree,
updateJson,
updateProjectConfiguration,
} from '@nx/devkit';
import { Linter } from '@nx/eslint';
@ -102,6 +103,17 @@ export async function pluginGeneratorInternal(host: Tree, schema: Schema) {
})
);
if (options.isTsSolutionSetup) {
updateJson(
host,
joinPathFragments(options.projectRoot, 'package.json'),
(json) => {
delete json.type;
return json;
}
);
}
if (options.bundler === 'tsc') {
tasks.push(addTsLibDependencies(host));
}