# library Creates a TypeScript library of the workspace: its `package.json` and `project.json` (with the typecheck, test and build targets), its TypeScript, lint and test configurations, an alias in `tsconfig.base.json`, a README and an entry module. With `--publishable`, the generator named make-publishable runs on it next. ```bash npx hf generate library my-utils \ --directory=libs --description="Shared helpers" --tags=type:util npx hf generate library my-utils --publishable --formats=esm,cjs ``` | Option | What it sets | | --------------- | ------------------------------------------------------------------------------------------------------------------- | | `--name` | The library's name, which names its directory and, with the scope, its package (also the first positional argument) | | `--directory` | The directory it is created under; defaults to libs | | `--scope` | The npm scope; defaults to the scope of the root manifest's name, or none | | `--projectName` | The project name tasks address; defaults to the library's name | | `--packageName` | The package name; defaults to the name under the scope | | `--description` | The description written to both manifests | | `--tags` | The tags the module boundaries read, comma-separated | | `--publishable` | Runs make-publishable on the new library | | `--keywords` | npm keywords, used with `--publishable` | | `--formats` | The bundle formats, used with `--publishable` | | `--skipFormat` | Leave the written files unformatted | The licence and the repository come from the root manifest, with the project's directory recorded, so a package published from the library carries them. ## API Reference ### Functions #### libraryGenerator ```typescript function libraryGenerator(tree: Tree, options: LibraryGeneratorOptions): Promise ``` Creates a library: its manifests, the files of its templates, and its alias in `tsconfig.base.json`; then, for a publishable one, runs the generator named `make-publishable` on it, the workspace's own where it provides one. **Parameters** | Name | Type | Description | | --- | --- | --- | | `tree` | `Tree` | The tree, rooted at the workspace | | `options` | `LibraryGeneratorOptions` | What to create | **Returns** `Promise`: Resolves when the library is written **Example: An internal library** ```bash npx hf generate library my-lib --tags=type:util ``` **Example: A publishable library under a chosen directory** ```bash npx hf generate library my-utils --directory=libs/utils --publishable --formats=esm,cjs,iife,umd ``` **Example: From a workspace's own generator, formatting once at the end** ```typescript import libraryGenerator from '@hyperfrontend/workspace/generators/library' import { formatFiles } from '@hyperfrontend/workspace/generators' await libraryGenerator(tree, { name: 'data-utils', tags: ['type:util'], skipFormat: true }) tree.write('libs/data-utils/src/index.ts', 'export * from "./data"\n') await formatFiles(tree) ``` #### planLibrary ```typescript function planLibrary(tree: Tree, options: LibraryGeneratorOptions): LibraryPlan ``` Settles the options: the names, the directory and the defaults the root manifest gives. **Parameters** | Name | Type | Description | | --- | --- | --- | | `tree` | `Tree` | The tree, whose root manifest gives the default scope | | `options` | `LibraryGeneratorOptions` | The options as given | **Returns** `LibraryPlan`: The names, the root and the description the generator writes from **Example: The names a spelling and the root manifest's scope settle to** ```typescript planLibrary(tree, { name: 'DataUtils', directory: 'libs/utils' }) // => { // name: 'data-utils', // projectName: 'data-utils', // packageName: '@acme/data-utils', // projectRoot: 'libs/utils/data-utils', // description: 'DataUtils library.', // tags: [], // offsetFromRoot: '../../../', // } when the root package.json is named @acme/workspace ``` ### Interfaces #### LibraryGeneratorOptions What the `library` generator takes. **Properties** | Name | Type | Description | | --- | --- | --- | | `name` | `string` | The name of the library, which names its directory and, with the scope, its package | | `directory?` | `string` | The directory the library is created under; `libs` when unset | | `scope?` | `string` | The npm scope of the package, with or without its `@`; the scope of the root manifest's name when unset | | `projectName?` | `string` | The project's name, as tasks address it; the library's name when unset | | `packageName?` | `string` | The package's name; the library's name under the scope when unset | | `description?` | `string` | A description of the library | | `tags?` | `string[]` | The tags the module boundaries read | | `publishable?` | `boolean` | Whether the generator named `make-publishable` runs on the library next | | `keywords?` | `string[]` | npm keywords for the package, handed to `make-publishable` | | `formats?` | `string[]` | The bundle formats the package builds, handed to `make-publishable` | | `skipFormat?` | `boolean` | Skip formatting the generated files | #### LibraryPlan The options settled. **Properties** | Name | Type | Description | | --- | --- | --- | | `name` | `string` | The library's name, as a file name | | `projectName` | `string` | The project's name | | `packageName` | `string` | The package's name | | `projectRoot` | `string` | The project's directory, relative to the root | | `description` | `string` | The description | | `tags` | `string[]` | The tags | | `offsetFromRoot` | `string` | The path from the project to the root | --- Canonical page: https://www.hyperfrontend.dev/docs/libraries/workspace/generators/library/ This file: https://www.hyperfrontend.dev/docs/libraries/workspace/generators/library.md Documentation index: https://www.hyperfrontend.dev/llms.txt