Recommend exporting `$lib` when writing a library (#2525)

fix #2519
This commit is contained in:
Timothee Guerin 2023-10-02 14:42:58 -07:00 коммит произвёл GitHub
Родитель 003d0662aa
Коммит 54b6010ebe
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 12 добавлений и 6 удалений

Просмотреть файл

@ -85,17 +85,23 @@ This will create `tsconfig.json`. But we need to make a couple changes to this.
### 4. Create `lib.ts`
Open `./src/lib.ts` and create your library definition that registers your library with the TypeSpec compiler and defines any diagnostics your library will emit. The following shows an example:
Open `./src/lib.ts` and create your library definition that registers your library with the TypeSpec compiler and defines any diagnostics your library will emit. Make sure to export the library definition as `$lib`.
:::warn
If `$lib` is not accessible from your library package (`import {$lib} from "my-library";`) some functionality will be unavailable like validation of emitter options, linter rules, etc.
:::
The following shows an example:
```typescript
import { createTypeSpecLibrary } from "@typespec/compiler";
export const myLibrary = createTypeSpecLibrary({
export const $lib = createTypeSpecLibrary({
name: "myLibrary",
diagnostics: {},
});
} as const);
// optional but convenient
// Optional but convenient, those are meant to be used locally in your library.
export const { reportDiagnostic, createDiagnostic, createStateSymbol } = myLibrary;
```
@ -105,9 +111,9 @@ Diagnostics are used for linters and decorators which are covered in subsequent
Open `./src/index.ts` and import your library definition:
<!-- prettier-ignore -->
```typescript
import { myLibrary } from "./lib.js";
// Re-export $lib to the compiler can get access to it and register your library correctly.
export { $lib } from "./lib.js";
```
### 6. Build TypeScript