@hyperfrontend/workspace/generators/library

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.

npx hf generate library my-utils \
  --directory=libs --description="Shared helpers" --tags=type:util
npx hf generate library my-utils --publishable --formats=esm,cjs
OptionWhat it sets
--nameThe library's name, which names its directory and, with the scope, its package (also the first positional argument)
--directoryThe directory it is created under; defaults to libs
--scopeThe npm scope; defaults to the scope of the root manifest's name, or none
--projectNameThe project name tasks address; defaults to the library's name
--packageNameThe package name; defaults to the name under the scope
--descriptionThe description written to both manifests
--tagsThe tags the module boundaries read, comma-separated
--publishableRuns make-publishable on the new library
--keywordsnpm keywords, used with --publishable
--formatsThe bundle formats, used with --publishable
--skipFormatLeave 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

§function

libraryGenerator(tree: Tree, options: LibraryGeneratorOptions): Promise<void>

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
NameTypeDescription
§tree
TreeThe tree, rooted at the workspace
§options
LibraryGeneratorOptionsWhat to create
Returns
Promise<void>
Resolves when the library is written
Examples

An internal library

npx hf generate library my-lib --tags=type:util

A publishable library under a chosen directory

npx hf generate library my-utils --directory=libs/utils --publishable --formats=esm,cjs,iife,umd

From a workspace's own generator, formatting once at the end

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)
§function

planLibrary(tree: Tree, options: LibraryGeneratorOptions): LibraryPlan

Settles the options: the names, the directory and the defaults the root manifest gives.
Parameters
NameTypeDescription
§tree
TreeThe tree, whose root manifest gives the default scope
§options
LibraryGeneratorOptionsThe 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

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

§interface

LibraryGeneratorOptions

What the library generator takes.
Properties
§name:stringThe name of the library, which names its directory and, with the scope, its package
§directory?:stringThe directory the library is created under; libs when unset
§scope?:stringThe npm scope of the package, with or without its @; the scope of the root manifest's name when unset
§projectName?:stringThe project's name, as tasks address it; the library's name when unset
§packageName?:stringThe package's name; the library's name under the scope when unset
§description?:stringA description of the library
§tags?:string[]The tags the module boundaries read
§publishable?:booleanWhether 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?:booleanSkip formatting the generated files
§interface

LibraryPlan

The options settled.
Properties
§name:stringThe library's name, as a file name
§projectName:stringThe project's name
§packageName:stringThe package's name
§projectRoot:stringThe project's directory, relative to the root
§description:stringThe description
§tags:string[]The tags
§offsetFromRoot:stringThe path from the project to the root