mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
docs(v2): Document TypeScript support (#2997)
This commit is contained in:
parent
71b5c2712b
commit
ec3c281952
4 changed files with 82 additions and 7 deletions
|
@ -414,6 +414,32 @@ module.exports = function (context, options) {
|
|||
};
|
||||
```
|
||||
|
||||
## `getTypeScriptThemePath()`
|
||||
|
||||
Similar to `getThemePath()`, it should return the path to the directory where the source code of TypeScript theme components can be found. Theme components under this path will **not** be resolved by Webpack. Therefore, it is not a replacement of `getThemePath()`. Instead, this path is purely for swizzling TypeScript theme components.
|
||||
|
||||
If you want to support TypeScript component swizzling for your theme, you can make the path returned by `getTypeScriptThemePath()` be your source directory, and make path returned by `getThemePath()` be the compiled JavaScript output.
|
||||
|
||||
Example:
|
||||
|
||||
```js {6-13} title="my-theme/src/index.js"
|
||||
const path = require('path');
|
||||
|
||||
module.exports = function (context, options) {
|
||||
return {
|
||||
name: 'name-of-my-theme',
|
||||
getThemePath() {
|
||||
// Where compiled JavaScript output lives
|
||||
return path.join(__dirname, '..', 'lib', 'theme');
|
||||
},
|
||||
getTypeScriptThemePath() {
|
||||
// Where TypeScript source code lives
|
||||
return path.resolve(__dirname, './theme');
|
||||
},
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
## `getClientModules()`
|
||||
|
||||
Returns an array of paths to the modules that are to be imported in the client bundle. These modules are imported globally before React even renders the initial UI.
|
||||
|
|
50
website/docs/typescript-support.md
Normal file
50
website/docs/typescript-support.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
---
|
||||
id: typescript-support
|
||||
title: TypeScript Support
|
||||
---
|
||||
|
||||
## Setup
|
||||
|
||||
Docusaurus supports writing and using TypeScript theme components. To start using TypeScript, add `@docusaurus/module-type-aliases` to your project:
|
||||
|
||||
```bash
|
||||
npm install --save-dev typescript @docusaurus/module-type-aliases
|
||||
```
|
||||
|
||||
Then add `tsconfig.json` to your project root with following content:
|
||||
|
||||
```json title="tsconfig.json"
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react",
|
||||
"lib": ["DOM"],
|
||||
"noEmit": true,
|
||||
"noImplicitAny": false
|
||||
},
|
||||
"include": ["src/"]
|
||||
}
|
||||
```
|
||||
|
||||
Docusaurus doesn't use this `tsconfig.json` to compile your TypeScript. It is added just for a nicer Editor experience, although you can choose to run `tsc --noEmit` to type check your code for yourself.
|
||||
|
||||
Then add `types.d.ts` in your `src` folder with the following content:
|
||||
|
||||
```ts title="src/types.d.ts"
|
||||
/// <reference types="@docusaurus/module-type-aliases" />
|
||||
```
|
||||
|
||||
This file makes TypeScript recognize various Docusaurus specific webpack aliases like `@theme`, `@docusaurus`, `@generated`.
|
||||
|
||||
Now you can start writing TypeScript theme components.
|
||||
|
||||
## Swizzling TypeScript theme components
|
||||
|
||||
For themes that supports TypeScript theme components, you can add the `--typescript` flag to the end of swizzling command to get TypeScript source code. For example, the following command will generate `index.tsx` and `styles.module.css` into `src/theme/Footer`.
|
||||
|
||||
```bash npm2yarn
|
||||
npm run swizzle @docusaurus/theme-classic Footer --typescript
|
||||
```
|
||||
|
||||
At this moment, the only official Docusaurus theme that supports TypeScript theme components is `@docusaurus/theme-classic`. If you are a Docusaurus theme package author who wants to add TypeScript support, see the [Lifecycle APIs docs](./lifecycle-apis#gettypescriptthemepath).
|
|
@ -16,7 +16,7 @@ module.exports = {
|
|||
type: 'category',
|
||||
label: 'Getting Started',
|
||||
collapsed: false,
|
||||
items: ['installation', 'configuration'],
|
||||
items: ['installation', 'configuration', 'typescript-support'],
|
||||
},
|
||||
{
|
||||
type: 'category',
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
{
|
||||
// This file is not used in compilation. It is here just for a nice editor experience.
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["DOM"],
|
||||
"module": "esnext",
|
||||
"noEmit": true,
|
||||
"noImplicitAny": false,
|
||||
"allowJs": true,
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react",
|
||||
"baseUrl": "src"
|
||||
"lib": ["DOM"],
|
||||
"noEmit": true,
|
||||
"noImplicitAny": false
|
||||
},
|
||||
"include": ["src/"]
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue