Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/schematics/angular/entry-point/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ function updateTsConfig(packageName: string, ...paths: string[]): Rule {
};
}

/**
* Creates a name usable for the generated class, selector, and file names from an
* entry-point name. Entry-point names are npm export subpaths and may contain characters
* (such as `~`) or digit-led words (such as `3d`) that cannot appear in a class name or
* in a selector segment. Returns null when the result would not start with a letter.
*/
function toArtifactName(name: string): string | null {
const artifactName = name
// `~` and `/` cannot appear in a class name, selector, or file name.
.replace(/[~/]/g, '-')
// A word that starts with digits but contains more than digits cannot start a class
// name or a selector segment (`lib-foo-3d` is an invalid selector); merging it into
// the preceding word (`foo3d`) keeps both valid. All-digit words stay separate, as
// trailing digit segments are valid in a selector (`lib-foo-2`).
.replace(/[-_]+(?=\d+[a-zA-Z.])/g, '')
// Separator runs and leading or trailing separators produce empty selector segments.
.replace(/[-_]{2,}/g, '-')
.replace(/^[-_.]+|[-_.]+$/g, '');

return /^[a-zA-Z]/.test(artifactName) ? artifactName : null;
}

export default createProjectSchematic<EntryPointOptions>((options, { project, tree }) => {
if (project.extensions['projectType'] !== ProjectType.Library) {
throw new SchematicsException(`Project "${options.project}" is not a library project.`);
Expand Down Expand Up @@ -114,11 +136,16 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr

const prefix = (options.prefix ?? project.prefix) || 'lib';
const sourceDir = isPrimary ? join(entryPointDir, 'lib') : entryPointDir;
const artifactName = isPrimary
const baseName = isPrimary
? /^@.*\/.*/.test(options.project)
? options.project.split('/')[1]
: options.project
: basename(options.name);
: options.name;
const artifactName =
toArtifactName(basename(baseName)) ??
toArtifactName(baseName) ??
toArtifactName(`${prefix}-${baseName}`) ??
'entry';

const buildOptions = project.targets.get('build')?.options;
const distRoot =
Expand Down
89 changes: 89 additions & 0 deletions packages/schematics/angular/entry-point/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,95 @@ describe('EntryPoint Schematic', () => {
}
});

it('should create a usable class and selector when the name starts with digits', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: '3d',
project: 'foo',
},
workspaceTree,
);

const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(pkgJson.exports['./3d']).toBe('./3d/src/public-api.ts');

const componentContent = tree.readContent('/projects/foo/3d/src/lib3d.ts');
expect(componentContent).toContain('export class Lib3d');
expect(componentContent).toContain("selector: 'lib-lib3d'");
expect(tree.readContent('/projects/foo/3d/src/public-api.ts')).toContain(
"export * from './lib3d';",
);
});

it('should name artifacts from the parent segments when the final segment starts with digits', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: 'feature/3d',
project: 'foo',
},
workspaceTree,
);

const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(pkgJson.exports['./feature/3d']).toBe('./feature/3d/src/public-api.ts');

const componentContent = tree.readContent('/projects/foo/feature/3d/src/feature3d.ts');
expect(componentContent).toContain('export class Feature3d');
expect(componentContent).toContain("selector: 'lib-feature3d'");
});

it('should merge a digit-led word into the preceding word', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: 'my-3d',
project: 'foo',
},
workspaceTree,
);

const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(pkgJson.exports['./my-3d']).toBe('./my-3d/src/public-api.ts');

const componentContent = tree.readContent('/projects/foo/my-3d/src/my3d.ts');
expect(componentContent).toContain('export class My3d');
expect(componentContent).toContain("selector: 'lib-my3d'");
});

it('should replace characters that cannot appear in a class name or selector', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: 'svg~icons',
project: 'foo',
},
workspaceTree,
);

const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(pkgJson.exports['./svg~icons']).toBe('./svg~icons/src/public-api.ts');

const componentContent = tree.readContent('/projects/foo/svg~icons/src/svg-icons.ts');
expect(componentContent).toContain('export class SvgIcons');
expect(componentContent).toContain("selector: 'lib-svg-icons'");
});

it('should allow a final name segment containing dots', async () => {
const tree = await schematicRunner.runSchematic(
'entry-point',
{
name: 'testing.v2',
project: 'foo',
},
workspaceTree,
);

const packageJson = getJsonFileContent(tree, '/projects/foo/package.json');
expect(packageJson.exports['./testing.v2']).toBeDefined();
});

it('should throw if the entry point already exists in package.json exports', async () => {
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);

Expand Down
Loading