fix(react): component generator should work without sourceRoot (#20094)

This commit is contained in:
Jack Hsu 2023-11-07 13:10:40 -05:00 committed by GitHub
parent d4f4d73129
commit 8a9f43f074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -1,5 +1,11 @@
import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version';
import { logger, readJson, Tree } from '@nx/devkit';
import {
logger,
readJson,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { createApp, createLib } from '../../utils/testing-generators';
import { componentGenerator } from './component';
@ -140,6 +146,22 @@ describe('component', () => {
expect(indexContent).not.toMatch(/lib\/hello/);
});
it('should work for projects without sourceRoot', async () => {
const projectConfig = readProjectConfiguration(appTree, 'my-lib');
delete projectConfig.sourceRoot;
updateProjectConfiguration(appTree, 'my-lib', projectConfig);
await componentGenerator(appTree, {
name: 'my-lib/src/lib/hello',
style: 'css',
export: true,
});
const indexContent = appTree.read('my-lib/src/index.ts', 'utf-8');
expect(indexContent).not.toMatch(/lib\/hello/);
});
});
describe('--pascalCaseFiles', () => {

View File

@ -36,7 +36,11 @@ export async function normalizeOptions(
const { className } = names(name);
const { sourceRoot: projectSourceRoot, projectType } = project;
const {
sourceRoot: projectSourceRoot,
root: projectRoot,
projectType,
} = project;
const styledModule = /^(css|scss|less|none)$/.test(options.style)
? null
@ -62,6 +66,6 @@ export async function normalizeOptions(
className,
fileName,
filePath,
projectSourceRoot,
projectSourceRoot: projectSourceRoot ?? projectRoot,
};
}