diff --git a/docs/local-dev/cli-build-system.md b/docs/local-dev/cli-build-system.md index cf8ac626b987a..a784e0eff9164 100644 --- a/docs/local-dev/cli-build-system.md +++ b/docs/local-dev/cli-build-system.md @@ -650,8 +650,34 @@ The following is an excerpt of a typical setup of an isomorphic library package: "files": ["dist"], ``` +## Sub-path Exports + +The Backstage CLI supports implementation of sub-path exports through the `"exports"` field in `package.json`. It might for example look like this: + +```json + "name": "@backstage/plugin-foo", + "exports": { + ".": "./src/index.ts", + "./components": "./src/components.ts", + }, +``` + +This in turn would allow you to import anything exported in `src/index.ts` via `@backstage/plugins-foo`, and `src/components.ts` via `@backstage/plugins-foo/components`. Note that patterns are not supported, meaning the exports may not contain `*` wildcards. + +As with the rest of the Backstage CLI build system, the setup is optimized for local development, which is why the `"exports"` targets point directly to source files. The `package build` command will detect the `"exports"` field and automatically generate the corresponding `dist` files, and the `prepublish` command will rewrite the `"exports"` field to point to the `dist` files, as well as generating folder-based entry points for backwards compatibility. + +TypeScript support is currently handled though the `typesVersions` field, as there is not yet a module resolution mode that works well with `"exports"`. You can craft the `typesVersions` yourself, but it will also be automatically generated by the `migrate package-exports` command. + +To add sub-path exports to an existing package, simply add the desired `"exports"` fields and then run the following command: + +```bash +yarn backstage-cli package migrate package-exports +``` + ## Experimental Type Build +> Note: Experimental type builds are deprecated and will be removed in the future. They have been replaced by [sub-path exports](#sub-path-exports). + The Backstage CLI has an experimental feature where multiple different type definition files can be generated for different release stages. The release stages are marked in the [TSDoc](https://tsdoc.org/) for each individual export, using either `@public`, `@alpha`, or `@beta`. Rather than just building a single `index.d.ts` file, the build process will instead output `index.d.ts`, `index.beta.d.ts`, and `index.alpha.d.ts`. Each of these files will have exports from more unstable release stages stripped, meaning that `index.d.ts` will omit all exports marked with `@alpha` or `@beta`, while `index.beta.d.ts` will omit all exports marked with `@alpha`. This feature is aimed at projects that publish to package registries and wish to maintain different levels of API stability within each package. There is no need to use this within a single monorepo, as it has no effect due to only applying to built and published packages.