Repository navigation
feat(@schematics/angular): use library builder and add entry-point schematic #34645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alan-agius4
wants to merge
6
commits into
angular:main
Choose a base branch
from
alan-agius4:feat/library-builder-and-entry-point-schematic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+825
−153
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c033dbc
feat(@schematics/angular): use library builder and add entry-point sc…
alan-agius4 a32141c
fixup! feat(@schematics/angular): use library builder and add entry-p…
alan-agius4 6737a20
fixup! feat(@schematics/angular): use library builder and add entry-p…
alan-agius4 b4486a5
fixup! feat(@schematics/angular): use library builder and add entry-p…
alan-agius4 9636cf2
fixup! feat(@schematics/angular): use library builder and add entry-p…
alan-agius4 98c90c5
fixup! feat(@schematics/angular): use library builder and add entry-p…
alan-agius4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/schematics/angular/entry-point/files/__entryFile__.ts.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* | ||
| * Public API Surface of <%= dasherize(name) %> | ||
| */ | ||
|
|
||
| export * from './<%= isPrimary ? 'lib/' : '' %><%= dasherize(name) %>';<% if (!standalone) { %> | ||
| export * from './<%= isPrimary ? 'lib/' : '' %><%= dasherize(name) %>-module';<% } %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { type JsonObject, type JsonValue, isJsonObject } from '@angular-devkit/core'; | ||
| import { | ||
| type Rule, | ||
| SchematicsException, | ||
| type Tree, | ||
| apply, | ||
| applyTemplates, | ||
| chain, | ||
| mergeWith, | ||
| move, | ||
| noop, | ||
| schematic, | ||
| strings, | ||
| url, | ||
| } from '@angular-devkit/schematics'; | ||
| import { basename, join, relative } from 'node:path/posix'; | ||
| import { JSONFile } from '../utility/json-file'; | ||
| import { createProjectSchematic } from '../utility/project'; | ||
| import { Builders, ProjectType } from '../utility/workspace-models'; | ||
| import type { Schema as EntryPointOptions } from './schema'; | ||
|
|
||
| function updateTsConfig(packageName: string, ...paths: string[]): Rule { | ||
| return (host: Tree) => { | ||
| if (!host.exists('tsconfig.json')) { | ||
| return host; | ||
| } | ||
|
|
||
| const file = new JSONFile(host, 'tsconfig.json'); | ||
| const jsonPath = ['compilerOptions', 'paths', packageName]; | ||
| const value = file.get(jsonPath); | ||
| file.modify(jsonPath, Array.isArray(value) ? Array.from(new Set([...value, ...paths])) : paths); | ||
| }; | ||
| } | ||
|
|
||
| export default createProjectSchematic<EntryPointOptions>((options, { project, tree }) => { | ||
| if (project.extensions['projectType'] !== ProjectType.Library) { | ||
| throw new SchematicsException(`Project "${options.project}" is not a library project.`); | ||
| } | ||
|
|
||
| const buildTarget = project.targets.get('build'); | ||
| if (buildTarget?.builder !== Builders.BuildLibrary) { | ||
| throw new SchematicsException( | ||
| `Project "${options.project}" does not use the "${Builders.BuildLibrary}" builder.`, | ||
| ); | ||
| } | ||
|
|
||
| if (options.name === 'package.json') { | ||
| throw new SchematicsException('Entry point name "package.json" is reserved.'); | ||
| } | ||
|
|
||
| const libDir = project.root; | ||
| const packageJsonPath = join(libDir, 'package.json'); | ||
| if (!tree.exists(packageJsonPath)) { | ||
| throw new SchematicsException(`Could not find package.json at "${packageJsonPath}".`); | ||
| } | ||
|
|
||
| const packageJson = new JSONFile(tree, packageJsonPath); | ||
| const rawPackageName = packageJson.get(['name']); | ||
| const packageName = | ||
| typeof rawPackageName === 'string' && rawPackageName | ||
| ? rawPackageName | ||
| : strings.dasherize(options.project); | ||
|
|
||
| const isPrimary = options.name === '.'; | ||
| const entryFile = (options.entryFile ?? 'public-api').replace(/\.ts$/i, ''); | ||
| const entryPointDir = join(libDir, options.name, 'src'); | ||
| const exportKey = isPrimary ? '.' : `./${options.name}`; | ||
| const exportTarget = `./${relative(libDir, join(entryPointDir, `${entryFile}.ts`))}`; | ||
|
|
||
| const existingExports = (packageJson.get(['exports']) as JsonValue | undefined) ?? null; | ||
| let exportsObject: JsonObject = {}; | ||
| if (typeof existingExports === 'string' || Array.isArray(existingExports)) { | ||
| exportsObject = { '.': existingExports }; | ||
| } else if (isJsonObject(existingExports)) { | ||
| const keys = Object.keys(existingExports); | ||
| if (keys.some((k) => k.startsWith('.'))) { | ||
| exportsObject = { ...existingExports }; | ||
| } else if (keys.length > 0) { | ||
| exportsObject = { '.': existingExports }; | ||
| } | ||
| } | ||
|
|
||
| if (Object.prototype.hasOwnProperty.call(exportsObject, exportKey)) { | ||
| throw new SchematicsException( | ||
| `Entry point "${exportKey}" already exists in "${packageJsonPath}".`, | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| isJsonObject(existingExports) && | ||
| Object.keys(existingExports).some((k) => k.startsWith('.')) | ||
| ) { | ||
| packageJson.modify(['exports', exportKey], exportTarget, false); | ||
| } else { | ||
| packageJson.modify( | ||
| ['exports'], | ||
| { | ||
| ...exportsObject, | ||
| [exportKey]: exportTarget, | ||
| }, | ||
| false, | ||
| ); | ||
| } | ||
|
|
||
| const prefix = (options.prefix ?? project.prefix) || 'lib'; | ||
| const sourceDir = isPrimary ? join(entryPointDir, 'lib') : entryPointDir; | ||
| const artifactName = isPrimary | ||
| ? /^@.*\/.*/.test(options.project) | ||
| ? options.project.split('/')[1] | ||
| : options.project | ||
| : basename(options.name); | ||
|
|
||
| const buildOptions = buildTarget.options; | ||
| const distRoot = | ||
| typeof buildOptions?.['outputPath'] === 'string' | ||
| ? buildOptions['outputPath'] | ||
| : join('dist', options.project); | ||
|
|
||
| const tsConfigPathKey = isPrimary ? packageName : `${packageName}/*`; | ||
| const tsConfigPathTarget = `./${join(distRoot, isPrimary ? '.' : '*')}`; | ||
|
|
||
| const standalone = options.standalone ?? true; | ||
|
|
||
| const templateSource = apply(url('../entry-point/files'), [ | ||
| applyTemplates({ | ||
| ...strings, | ||
| ...options, | ||
| isPrimary, | ||
| standalone, | ||
| name: artifactName, | ||
| entryFile, | ||
| }), | ||
| move(entryPointDir), | ||
| ]); | ||
|
|
||
| return chain([ | ||
| mergeWith(templateSource), | ||
| options.skipTsConfig ? noop() : updateTsConfig(tsConfigPathKey, tsConfigPathTarget), | ||
| standalone | ||
| ? noop() | ||
| : schematic('module', { | ||
| name: artifactName, | ||
| commonModule: false, | ||
| flat: true, | ||
| path: sourceDir, | ||
| project: options.project, | ||
| // Explicitly set the `typeSeparator` this also ensures that the generated files are valid even if the `module` schematic | ||
| // inherits its `typeSeparator` from the workspace. | ||
| typeSeparator: '-', | ||
| }), | ||
| schematic('component', { | ||
| name: artifactName, | ||
| selector: `${prefix}-${strings.dasherize(artifactName)}`, | ||
| inlineStyle: true, | ||
| inlineTemplate: true, | ||
| flat: true, | ||
| path: sourceDir, | ||
| export: true, | ||
| standalone, | ||
| skipTests: options.skipTests, | ||
| project: options.project, | ||
| // Explicitly set an empty `type` and `exportDefault: false` since they don't necessarily make | ||
| // sense in a library and ensure that the generated files match the entry-point exports even | ||
| // if the `component` schematic inherits options from the workspace. | ||
| type: '', | ||
| exportDefault: false, | ||
| }), | ||
| ]); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.