Skip to content

Commit 6737a20

Browse files
committed
fixup! feat(@schematics/angular): use library builder and add entry-point schematic
1 parent a32141c commit 6737a20

7 files changed

Lines changed: 65 additions & 74 deletions

File tree

‎packages/schematics/angular/entry-point/files/__entryFile__.ts.template‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
* Public API Surface of <%= dasherize(name) %>
33
*/
44

5-
export * from './lib/<%= dasherize(name) %>';<% if (!standalone) { %>
6-
export * from './lib/<%= dasherize(name) %>-module';<% } %>
5+
export * from './<%= isPrimary ? 'lib/' : '' %><%= dasherize(name) %>';<% if (!standalone) { %>
6+
export * from './<%= isPrimary ? 'lib/' : '' %><%= dasherize(name) %>-module';<% } %>

‎packages/schematics/angular/entry-point/index.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
5656
throw new SchematicsException('Entry point name "package.json" is reserved.');
5757
}
5858

59-
if (options.name.split('/').includes('lib')) {
59+
const libDir = project.root;
60+
const sourceRoot = project.sourceRoot ?? join(libDir, 'src');
61+
const relativeSourceRoot = relative(libDir, sourceRoot);
62+
if (
63+
relativeSourceRoot &&
64+
!relativeSourceRoot.startsWith('..') &&
65+
(options.name === relativeSourceRoot || options.name.startsWith(`${relativeSourceRoot}/`))
66+
) {
6067
throw new SchematicsException(
61-
`Entry point name "${options.name}" cannot be "lib" or contain a "lib" segment ` +
62-
'as it conflicts with the entry point source directory.',
68+
`Entry point name "${options.name}" cannot be "${relativeSourceRoot}" or start with "${relativeSourceRoot}/" ` +
69+
'as it conflicts with the primary entry point source directory.',
6370
);
6471
}
6572

66-
const libDir = project.root;
6773
const packageJsonPath = join(libDir, 'package.json');
6874
if (!tree.exists(packageJsonPath)) {
6975
throw new SchematicsException(`Could not find package.json at "${packageJsonPath}".`);
@@ -78,8 +84,7 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
7884

7985
const isPrimary = options.name === '.';
8086
const entryFile = (options.entryFile ?? 'public-api').replace(/\.ts$/i, '');
81-
const sourceRoot = project.sourceRoot ?? join(libDir, 'src');
82-
const entryPointDir = join(sourceRoot, options.name);
87+
const entryPointDir = isPrimary ? sourceRoot : join(libDir, options.name, 'src');
8388
const exportKey = isPrimary ? '.' : `./${options.name}`;
8489
const exportTarget = `./${relative(libDir, join(entryPointDir, `${entryFile}.ts`))}`;
8590

@@ -119,7 +124,7 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
119124
}
120125

121126
const prefix = (options.prefix ?? project.prefix) || 'lib';
122-
const sourceDir = join(entryPointDir, 'lib');
127+
const sourceDir = isPrimary ? join(entryPointDir, 'lib') : entryPointDir;
123128
const artifactName = isPrimary
124129
? /^@.*\/.*/.test(options.project)
125130
? options.project.split('/')[1]
@@ -141,6 +146,7 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
141146
applyTemplates({
142147
...strings,
143148
...options,
149+
isPrimary,
144150
standalone,
145151
name: artifactName,
146152
entryFile,

‎packages/schematics/angular/entry-point/index_spec.ts‎

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ describe('EntryPoint Schematic', () => {
5656

5757
expect(tree.files).toEqual(
5858
jasmine.arrayContaining([
59-
'/projects/foo/src/secondary/public-api.ts',
60-
'/projects/foo/src/secondary/lib/secondary.ts',
61-
'/projects/foo/src/secondary/lib/secondary.spec.ts',
59+
'/projects/foo/secondary/src/public-api.ts',
60+
'/projects/foo/secondary/src/secondary.ts',
61+
'/projects/foo/secondary/src/secondary.spec.ts',
6262
]),
6363
);
6464

65-
const publicApiContent = tree.readContent('/projects/foo/src/secondary/public-api.ts');
66-
expect(publicApiContent).toContain("export * from './lib/secondary';");
65+
const publicApiContent = tree.readContent('/projects/foo/secondary/src/public-api.ts');
66+
expect(publicApiContent).toContain("export * from './secondary';");
6767
expect(publicApiContent).not.toContain('secondary-module');
6868
});
6969

@@ -79,14 +79,14 @@ describe('EntryPoint Schematic', () => {
7979

8080
expect(tree.files).toEqual(
8181
jasmine.arrayContaining([
82-
'/projects/foo/src/feature/sub/public-api.ts',
83-
'/projects/foo/src/feature/sub/lib/sub.ts',
84-
'/projects/foo/src/feature/sub/lib/sub.spec.ts',
82+
'/projects/foo/feature/sub/src/public-api.ts',
83+
'/projects/foo/feature/sub/src/sub.ts',
84+
'/projects/foo/feature/sub/src/sub.spec.ts',
8585
]),
8686
);
8787

8888
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
89-
expect(pkgJson.exports['./feature/sub']).toBe('./src/feature/sub/public-api.ts');
89+
expect(pkgJson.exports['./feature/sub']).toBe('./feature/sub/src/public-api.ts');
9090

9191
const tsConfigJson = getJsonFileContent(tree, '/tsconfig.json');
9292
expect(tsConfigJson.compilerOptions.paths['foo/*']).toEqual(['./dist/foo/*']);
@@ -102,9 +102,9 @@ describe('EntryPoint Schematic', () => {
102102
workspaceTree,
103103
);
104104

105-
expect(tree.files).toContain('/projects/foo/src/secondary/my-entry.ts');
105+
expect(tree.files).toContain('/projects/foo/secondary/src/my-entry.ts');
106106
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
107-
expect(pkgJson.exports['./secondary']).toBe('./src/secondary/my-entry.ts');
107+
expect(pkgJson.exports['./secondary']).toBe('./secondary/src/my-entry.ts');
108108
});
109109

110110
it('should strip .ts suffix from entryFile option', async () => {
@@ -117,10 +117,10 @@ describe('EntryPoint Schematic', () => {
117117
workspaceTree,
118118
);
119119

120-
expect(tree.files).toContain('/projects/foo/src/secondary/my-entry.ts');
121-
expect(tree.files).not.toContain('/projects/foo/src/secondary/my-entry.ts.ts');
120+
expect(tree.files).toContain('/projects/foo/secondary/src/my-entry.ts');
121+
expect(tree.files).not.toContain('/projects/foo/secondary/src/my-entry.ts.ts');
122122
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
123-
expect(pkgJson.exports['./secondary']).toBe('./src/secondary/my-entry.ts');
123+
expect(pkgJson.exports['./secondary']).toBe('./secondary/src/my-entry.ts');
124124
});
125125

126126
it('should update library package.json exports', async () => {
@@ -129,7 +129,7 @@ describe('EntryPoint Schematic', () => {
129129
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
130130
expect(pkgJson.exports).toEqual({
131131
'.': './src/public-api.ts',
132-
'./secondary': './src/secondary/public-api.ts',
132+
'./secondary': './secondary/src/public-api.ts',
133133
});
134134
expect(Object.keys(pkgJson)[0]).toBe('name');
135135
});
@@ -144,7 +144,7 @@ describe('EntryPoint Schematic', () => {
144144
const updatedPkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
145145
expect(updatedPkgJson.exports).toEqual({
146146
'.': './src/public-api.ts',
147-
'./secondary': './src/secondary/public-api.ts',
147+
'./secondary': './secondary/src/public-api.ts',
148148
});
149149
});
150150

@@ -158,7 +158,7 @@ describe('EntryPoint Schematic', () => {
158158
const updatedPkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
159159
expect(updatedPkgJson.exports).toEqual({
160160
'.': ['./src/public-api.ts'],
161-
'./secondary': './src/secondary/public-api.ts',
161+
'./secondary': './secondary/src/public-api.ts',
162162
});
163163
});
164164

@@ -172,7 +172,7 @@ describe('EntryPoint Schematic', () => {
172172
const updatedPkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
173173
expect(updatedPkgJson.exports).toEqual({
174174
'.': { default: './src/public-api.ts' },
175-
'./secondary': './src/secondary/public-api.ts',
175+
'./secondary': './secondary/src/public-api.ts',
176176
});
177177
});
178178

@@ -206,25 +206,6 @@ describe('EntryPoint Schematic', () => {
206206
expect(tsConfigJson.compilerOptions.paths['foo/*']).toEqual(['./custom-dist/foo/*']);
207207
});
208208

209-
it('should respect custom sourceRoot in angular.json', async () => {
210-
const angularJson = getJsonFileContent(workspaceTree, '/angular.json');
211-
angularJson.projects.foo.sourceRoot = 'projects/foo/custom-src';
212-
workspaceTree.overwrite('/angular.json', JSON.stringify(angularJson, null, 2));
213-
214-
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
215-
216-
expect(tree.files).toEqual(
217-
jasmine.arrayContaining([
218-
'/projects/foo/custom-src/secondary/public-api.ts',
219-
'/projects/foo/custom-src/secondary/lib/secondary.ts',
220-
'/projects/foo/custom-src/secondary/lib/secondary.spec.ts',
221-
]),
222-
);
223-
224-
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
225-
expect(pkgJson.exports['./secondary']).toBe('./custom-src/secondary/public-api.ts');
226-
});
227-
228209
it('should not modify root tsconfig.json when skipTsConfig is true', async () => {
229210
const tree = await schematicRunner.runSchematic(
230211
'entry-point',
@@ -259,9 +240,7 @@ describe('EntryPoint Schematic', () => {
259240
tree,
260241
);
261242

262-
const componentContent = tree.readContent(
263-
'/projects/prefixed-lib/src/secondary/lib/secondary.ts',
264-
);
243+
const componentContent = tree.readContent('/projects/prefixed-lib/secondary/src/secondary.ts');
265244
expect(componentContent).toContain("selector: 'custom-secondary'");
266245
});
267246

@@ -272,7 +251,7 @@ describe('EntryPoint Schematic', () => {
272251

273252
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
274253

275-
const componentContent = tree.readContent('/projects/foo/src/secondary/lib/secondary.ts');
254+
const componentContent = tree.readContent('/projects/foo/secondary/src/secondary.ts');
276255
expect(componentContent).toContain("selector: 'lib-secondary'");
277256
});
278257

@@ -286,7 +265,7 @@ describe('EntryPoint Schematic', () => {
286265
workspaceTree,
287266
);
288267

289-
const componentContent = tree.readContent('/projects/foo/src/secondary/lib/secondary.ts');
268+
const componentContent = tree.readContent('/projects/foo/secondary/src/secondary.ts');
290269
expect(componentContent).toContain("selector: 'override-secondary'");
291270
});
292271

@@ -311,16 +290,16 @@ describe('EntryPoint Schematic', () => {
311290

312291
expect(tree.files).toEqual(
313292
jasmine.arrayContaining([
314-
'/projects/my-scope/my-lib/src/secondary/public-api.ts',
315-
'/projects/my-scope/my-lib/src/secondary/lib/secondary.ts',
316-
'/projects/my-scope/my-lib/src/secondary/lib/secondary.spec.ts',
293+
'/projects/my-scope/my-lib/secondary/src/public-api.ts',
294+
'/projects/my-scope/my-lib/secondary/src/secondary.ts',
295+
'/projects/my-scope/my-lib/secondary/src/secondary.spec.ts',
317296
]),
318297
);
319298

320299
const pkgJson = getJsonFileContent(tree, '/projects/my-scope/my-lib/package.json');
321300
expect(pkgJson.exports).toEqual({
322301
'.': './src/public-api.ts',
323-
'./secondary': './src/secondary/public-api.ts',
302+
'./secondary': './secondary/src/public-api.ts',
324303
});
325304

326305
const tsConfigJson = getJsonFileContent(tree, '/tsconfig.json');
@@ -339,8 +318,8 @@ describe('EntryPoint Schematic', () => {
339318
workspaceTree,
340319
);
341320

342-
expect(tree.files).toContain('/projects/foo/src/secondary/lib/secondary.ts');
343-
expect(tree.files).not.toContain('/projects/foo/src/secondary/lib/secondary.spec.ts');
321+
expect(tree.files).toContain('/projects/foo/secondary/src/secondary.ts');
322+
expect(tree.files).not.toContain('/projects/foo/secondary/src/secondary.spec.ts');
344323
});
345324

346325
it('should not be affected by workspace component schematic type and exportDefault defaults', async () => {
@@ -355,8 +334,8 @@ describe('EntryPoint Schematic', () => {
355334

356335
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
357336

358-
expect(tree.files).toContain('/projects/foo/src/secondary/lib/secondary.ts');
359-
const componentContent = tree.readContent('/projects/foo/src/secondary/lib/secondary.ts');
337+
expect(tree.files).toContain('/projects/foo/secondary/src/secondary.ts');
338+
const componentContent = tree.readContent('/projects/foo/secondary/src/secondary.ts');
360339
expect(componentContent).toContain('export class Secondary');
361340
expect(componentContent).not.toContain('export default');
362341
});
@@ -374,16 +353,16 @@ describe('EntryPoint Schematic', () => {
374353

375354
expect(tree.files).toEqual(
376355
jasmine.arrayContaining([
377-
'/projects/foo/src/secondary/public-api.ts',
378-
'/projects/foo/src/secondary/lib/secondary-module.ts',
379-
'/projects/foo/src/secondary/lib/secondary.ts',
380-
'/projects/foo/src/secondary/lib/secondary.spec.ts',
356+
'/projects/foo/secondary/src/public-api.ts',
357+
'/projects/foo/secondary/src/secondary-module.ts',
358+
'/projects/foo/secondary/src/secondary.ts',
359+
'/projects/foo/secondary/src/secondary.spec.ts',
381360
]),
382361
);
383362

384-
const publicApiContent = tree.readContent('/projects/foo/src/secondary/public-api.ts');
385-
expect(publicApiContent).toContain("export * from './lib/secondary';");
386-
expect(publicApiContent).toContain("export * from './lib/secondary-module';");
363+
const publicApiContent = tree.readContent('/projects/foo/secondary/src/public-api.ts');
364+
expect(publicApiContent).toContain("export * from './secondary';");
365+
expect(publicApiContent).toContain("export * from './secondary-module';");
387366
});
388367
});
389368

@@ -451,8 +430,8 @@ describe('EntryPoint Schematic', () => {
451430
).toBeRejectedWithError('Entry point name "package.json" is reserved.');
452431
});
453432

454-
it('should throw if the entry-point name is "lib" or contains a "lib" segment', async () => {
455-
for (const invalidName of ['lib', 'lib/sub', 'feature/lib', 'feature/lib/sub']) {
433+
it('should throw if the entry-point name conflicts with the primary source directory', async () => {
434+
for (const invalidName of ['src', 'src/sub']) {
456435
await expectAsync(
457436
schematicRunner.runSchematic(
458437
'entry-point',
@@ -463,8 +442,8 @@ describe('EntryPoint Schematic', () => {
463442
workspaceTree,
464443
),
465444
).toBeRejectedWithError(
466-
`Entry point name "${invalidName}" cannot be "lib" or contain a "lib" segment ` +
467-
'as it conflicts with the entry point source directory.',
445+
`Entry point name "${invalidName}" cannot be "src" or start with "src/" ` +
446+
'as it conflicts with the primary entry point source directory.',
468447
);
469448
}
470449
});

‎packages/schematics/angular/library/files/tsconfig.lib.json.template‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"types": []
88
},
99
"include": [
10-
"src/**/*.ts"
10+
"**/*.ts"
1111
],
1212
"exclude": [
1313
"**/*.spec.ts"

‎packages/schematics/angular/library/files/tsconfig.spec.json.template‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"include": [
12-
"src/**/*.d.ts",
13-
"src/**/*<% if (standalone) { %>.spec<% } %>.ts"
12+
"**/*.d.ts",
13+
"**/*<% if (standalone) { %>.spec<% } %>.ts"
1414
]
1515
}

‎packages/schematics/angular/library/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ function addLibToWorkspaceFile(
111111
builder: Builders.BuildUnitTest,
112112
options: {
113113
tsConfig: `${projectRoot}/tsconfig.spec.json`,
114+
// Include spec files from secondary entry points located outside of the source root.
115+
include: ['../**/*.spec.ts'],
114116
},
115117
}
116118
: {

‎packages/schematics/angular/library/index_spec.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('Library Schematic', () => {
150150
expect(fileContent).toBeDefined();
151151
expect(fileContent.compilerOptions.declaration).toBe(true);
152152
expect(fileContent.compilerOptions.declarationMap).toBeUndefined();
153-
expect(fileContent.include).toEqual(['src/**/*.ts']);
153+
expect(fileContent.include).toEqual(['**/*.ts']);
154154
expect(fileContent.exclude).toEqual(['**/*.spec.ts']);
155155
});
156156

@@ -441,6 +441,10 @@ describe('Library Schematic', () => {
441441

442442
const workspace = JSON.parse(tree.readContent('/angular.json'));
443443
expect(workspace.projects.foo.architect.test.builder).toBe('@angular/build:unit-test');
444+
expect(workspace.projects.foo.architect.test.options.include).toEqual(['../**/*.spec.ts']);
445+
446+
const specTsConfig = getJsonFileContent(tree, '/projects/foo/tsconfig.spec.json');
447+
expect(specTsConfig.include).toEqual(['**/*.d.ts', '**/*.spec.ts']);
444448
});
445449

446450
it(`should add 'unit-test' test builder when vitest is available`, async () => {

0 commit comments

Comments
 (0)