Skip to content

Commit

Permalink
docs(v2): Document TypeScript support (#2997)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamChou19815 authored Jun 26, 2020
1 parent 71b5c27 commit ec3c281
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
26 changes: 26 additions & 0 deletions website/docs/lifecycle-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions website/docs/typescript-support.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
type: 'category',
label: 'Getting Started',
collapsed: false,
items: ['installation', 'configuration'],
items: ['installation', 'configuration', 'typescript-support'],
},
{
type: 'category',
Expand Down
9 changes: 4 additions & 5 deletions website/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
// This file is not used in compilation. It is here just for a nice editor experience.
"extends": "../tsconfig.json",
"compilerOptions": {
"allowJs": true,
"esModuleInterop": true,
"jsx": "react",
"lib": ["DOM"],
"module": "esnext",
"noEmit": true,
"noImplicitAny": false,
"jsx": "react",
"baseUrl": "src"
"noImplicitAny": false
},
"include": ["src/"]
}

0 comments on commit ec3c281

Please sign in to comment.