@@ -40,6 +40,28 @@ function updateTsConfig(packageName: string, ...paths: string[]): Rule {
4040 } ;
4141}
4242
43+ /**
44+ * Creates a name usable for the generated class, selector, and file names from an
45+ * entry-point name. Entry-point names are npm export subpaths and may contain characters
46+ * (such as `~`) or digit-led words (such as `3d`) that cannot appear in a class name or
47+ * in a selector segment. Returns null when the result would not start with a letter.
48+ */
49+ function toArtifactName ( name : string ) : string | null {
50+ const artifactName = name
51+ // `~` and `/` cannot appear in a class name, selector, or file name.
52+ . replace ( / [ ~ / ] / g, '-' )
53+ // A word that starts with digits but contains more than digits cannot start a class
54+ // name or a selector segment (`lib-foo-3d` is an invalid selector); merging it into
55+ // the preceding word (`foo3d`) keeps both valid. All-digit words stay separate, as
56+ // trailing digit segments are valid in a selector (`lib-foo-2`).
57+ . replace ( / [ - _ ] + (? = \d + [ a - z A - Z . ] ) / g, '' )
58+ // Separator runs and leading or trailing separators produce empty selector segments.
59+ . replace ( / [ - _ ] { 2 , } / g, '-' )
60+ . replace ( / ^ [ - _ . ] + | [ - _ . ] + $ / g, '' ) ;
61+
62+ return / ^ [ a - z A - Z ] / . test ( artifactName ) ? artifactName : null ;
63+ }
64+
4365export default createProjectSchematic < EntryPointOptions > ( ( options , { project, tree } ) => {
4466 if ( project . extensions [ 'projectType' ] !== ProjectType . Library ) {
4567 throw new SchematicsException ( `Project "${ options . project } " is not a library project.` ) ;
@@ -58,20 +80,6 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
5880 throw new SchematicsException ( 'Entry point name "package.json" is reserved.' ) ;
5981 }
6082
61- // The final segment names the generated class, selector, and files. Without this check,
62- // a name that is a valid export subpath but not usable as an identifier fails later with
63- // an error about a class or selector the user never provided.
64- if (
65- options . name !== '.' &&
66- ! / ^ [ a - z A - Z ] [ a - z A - Z 0 - 9 _ - ] * (?: \. [ a - z A - Z 0 - 9 _ - ] + ) * $ / . test ( basename ( options . name ) )
67- ) {
68- throw new SchematicsException (
69- `Entry point name "${ options . name } " is invalid. The final path segment is used to name ` +
70- 'the generated source files and must start with a letter and contain only ' +
71- 'alphanumeric characters, dots, dashes, and underscores.' ,
72- ) ;
73- }
74-
7583 const libDir = project . root ;
7684 const packageJsonPath = join ( libDir , 'package.json' ) ;
7785 if ( ! tree . exists ( packageJsonPath ) ) {
@@ -128,11 +136,16 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
128136
129137 const prefix = ( options . prefix ?? project . prefix ) || 'lib' ;
130138 const sourceDir = isPrimary ? join ( entryPointDir , 'lib' ) : entryPointDir ;
131- const artifactName = isPrimary
139+ const baseName = isPrimary
132140 ? / ^ @ .* \/ .* / . test ( options . project )
133141 ? options . project . split ( '/' ) [ 1 ]
134142 : options . project
135- : basename ( options . name ) ;
143+ : options . name ;
144+ const artifactName =
145+ toArtifactName ( basename ( baseName ) ) ??
146+ toArtifactName ( baseName ) ??
147+ toArtifactName ( `${ prefix } -${ baseName } ` ) ??
148+ 'entry' ;
136149
137150 const buildOptions = project . targets . get ( 'build' ) ?. options ;
138151 const distRoot =
0 commit comments