Skip to content

Commit

Permalink
fix(react): component generator should work without sourceRoot (#20094)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Nov 7, 2023
1 parent d4f4d73 commit 8a9f43f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/react/src/generators/component/component.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -62,6 +66,6 @@ export async function normalizeOptions(
className,
fileName,
filePath,
projectSourceRoot,
projectSourceRoot: projectSourceRoot ?? projectRoot,
};
}

0 comments on commit 8a9f43f

Please sign in to comment.