@nx/react:library - Add simpleName option to remove dir name from generated file names (#17226)
This commit is contained in:
parent
825190674c
commit
b0732e1554
@ -183,6 +183,11 @@
|
|||||||
"description": "Create a React library with a minimal setup, no separate test files.",
|
"description": "Create a React library with a minimal setup, no separate test files.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"simpleName": {
|
||||||
|
"description": "Don't include the directory in the name of the module of the library.",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["name"],
|
"required": ["name"],
|
||||||
|
|||||||
@ -21,6 +21,7 @@ export function createFiles(host: Tree, options: NormalizedSchema) {
|
|||||||
...names(options.name),
|
...names(options.name),
|
||||||
tmpl: '',
|
tmpl: '',
|
||||||
offsetFromRoot: offsetFromRoot(options.projectRoot),
|
offsetFromRoot: offsetFromRoot(options.projectRoot),
|
||||||
|
fileName: options.fileName,
|
||||||
};
|
};
|
||||||
|
|
||||||
generateFiles(
|
generateFiles(
|
||||||
|
|||||||
@ -26,7 +26,7 @@ export function normalizeOptions(
|
|||||||
: name;
|
: name;
|
||||||
|
|
||||||
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
|
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
|
||||||
const fileName = projectName;
|
const fileName = options.simpleName ? name : projectName;
|
||||||
const { libsDir: defaultLibsDir } = getWorkspaceLayout(host);
|
const { libsDir: defaultLibsDir } = getWorkspaceLayout(host);
|
||||||
const libsDir = layoutDirectory ?? defaultLibsDir;
|
const libsDir = layoutDirectory ?? defaultLibsDir;
|
||||||
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
|
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
|
||||||
|
|||||||
@ -29,6 +29,7 @@ describe('lib', () => {
|
|||||||
style: 'css',
|
style: 'css',
|
||||||
component: true,
|
component: true,
|
||||||
strict: true,
|
strict: true,
|
||||||
|
simpleName: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -736,6 +737,30 @@ describe('lib', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('--simpleName', () => {
|
||||||
|
it('should generate a library with a simple name', async () => {
|
||||||
|
await libraryGenerator(tree, {
|
||||||
|
...defaultSchema,
|
||||||
|
simpleName: true,
|
||||||
|
directory: 'myDir',
|
||||||
|
});
|
||||||
|
|
||||||
|
const indexFile = tree.read('libs/my-dir/my-lib/src/index.ts', 'utf-8');
|
||||||
|
|
||||||
|
expect(indexFile).toContain(`export * from './lib/my-lib';`);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
tree.exists('libs/my-dir/my-lib/src/lib/my-lib.module.css')
|
||||||
|
).toBeTruthy();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
tree.exists('libs/my-dir/my-lib/src/lib/my-lib.spec.tsx')
|
||||||
|
).toBeTruthy();
|
||||||
|
|
||||||
|
expect(tree.exists('libs/my-dir/my-lib/src/lib/my-lib.tsx')).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it.each`
|
it.each`
|
||||||
style
|
style
|
||||||
${'styled-components'}
|
${'styled-components'}
|
||||||
|
|||||||
@ -129,7 +129,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
|
|||||||
|
|
||||||
if (options.component) {
|
if (options.component) {
|
||||||
const componentTask = await componentGenerator(host, {
|
const componentTask = await componentGenerator(host, {
|
||||||
name: options.name,
|
name: options.fileName,
|
||||||
project: options.name,
|
project: options.name,
|
||||||
flat: true,
|
flat: true,
|
||||||
style: options.style,
|
style: options.style,
|
||||||
|
|||||||
@ -26,6 +26,7 @@ export interface Schema {
|
|||||||
tags?: string;
|
tags?: string;
|
||||||
unitTestRunner?: 'jest' | 'vitest' | 'none';
|
unitTestRunner?: 'jest' | 'vitest' | 'none';
|
||||||
minimal?: boolean;
|
minimal?: boolean;
|
||||||
|
simpleName?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NormalizedSchema extends Schema {
|
export interface NormalizedSchema extends Schema {
|
||||||
|
|||||||
@ -186,6 +186,11 @@
|
|||||||
"description": "Create a React library with a minimal setup, no separate test files.",
|
"description": "Create a React library with a minimal setup, no separate test files.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
|
},
|
||||||
|
"simpleName": {
|
||||||
|
"description": "Don't include the directory in the name of the module of the library.",
|
||||||
|
"type": "boolean",
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["name"]
|
"required": ["name"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user