2020-10-29

Docusaurus Alpha TypeScript with Husky

docusaurus, cheatsheet, typescript, husky

banner

Image by Lynn Coetzee from Pixabay

Introduction

I wanted to use Docusaurus v2, which supports MDX but is in Alpha.
This is just my cheatsheet on how to set it up with TypeScript and Commitizen support using Husky.

I will use yarn instead of npm.

Bootstrap Docusaurus

Create a new Docusaurus project.
https://v2.docusaurus.io/docs/installation

npx @docusaurus/init@next init [name] [template]

e.g. npx @docusaurus/init@next init doc-demo classic

Set up TypeScript

1. Install TypeScript and types

yarn add -D typescript @docusaurus/module-type-aliases @types/react @types/react-router-dom @types/react-helmet @tsconfig/docusaurus

Important: Make sure that the version of @docusaurus/module-type-aliases matches that of @docusaurus/core in package.json.

I had trouble as the bootstrapped project used @docusaurus/core version of alpha.66 while the latest @docusaurus/module-type-aliases was alpha.51.

npm module-type-aliases

I manually upgraded @docusaurus/module-type-aliases to @docusaurus/module-type-aliases@2.0.0-alpha.66 like

yarn add -D @docusaurus/module-type-aliases@2.0.0-alpha.66

2. Configure tsconfig.json

Extend as shown in the official documentation.
https://v2.docusaurus.io/docs/typescript-support

1{2  "extends": "@tsconfig/docusaurus/tsconfig.json",3  "include": ["src/"]4}

3. Rename the main page source extension

Change the ./src/pages/index.js to ./src/pages/index.tsx.

4. Add type-check to test TypeScript

Add type-check script in package.json.

1"scripts": {2    ...3    "type-check": "tsc"4}

Run the command to see if there is any error.

yarn type-check

If there is no error, you should be able to run it with yarn start.

Set up Commitizen with Husky

To enforce consistent commit message (I like Angular Commit Message Format), I will install Commitizen.

Checking it manually is taxing so I will use Husky to make it run during git commit.

I will also add TypeScript ESLint to see if there is any error (Is this needed when tsc is run with type-check???)

1. Install packages

To add commitizen, husky and eslint, run the following command

yarn add -D commitizen cz-conventional-changelog eslint eslint-config-prettier eslint-import-resolver-alias eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks husky lint-staged prettier @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser

2. Add scripts for husky and linting

In package.json,

1"scripts": {2    ...3    "cm": "cz",4    "lint": "eslint --ignore-path .gitignore . --ext ts --ext tsx --ext js --ext jsx",5    "lint:fix": "yarn lint --fix"6  },

3. Set up Commitizen for conventional log

In package.json, add the commitizen configuration

1"config": {2    "commitizen": {3      "path": "./node_modules/cz-conventional-changelog"4    }5  },

This will enforce the Angular commit message convention.

And add cz command.

4. Configure Lint-Staged

Create .lintstagedrc file in the project root and add following configuration

1{2  "*.{js,jsx,ts,tsx}": ["yarn lint:fix", "git add"],3  "*.scss": ["prettier --write", "stylelint --fix", "git add"],4  "{*.{json,md}}": ["prettier --write", "git add"]5}

5. Configure Husky

Create .huskyrc file in the project root.
The following configuration will let husky run when git commit is typed.

What it does is to

1{2  "hooks": {3    "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",4    "pre-commit": ["yarn type-check && lint-staged"]5  }6}

6. Configure ESLint

Create .eslintrc.js in the project root.
It is based on Setting up a GatsbyJS starter with TypeScript, ESLint, Prettier and pre-commit hooks.

1/**2 * based on Setting up the linters3 * https://www.arden.nl/setting-up-a-gatsby-js-starter-with-type-script-es-lint-prettier-and-pre-commit-hooks4 */5module.exports = {6  parser: "@typescript-eslint/parser", // Specifies the ESLint parser7  extends: [8    "eslint:recommended",9    "plugin:react/recommended",10    "plugin:@typescript-eslint/recommended",11    "prettier/@typescript-eslint",12    "plugin:prettier/recommended",13    "plugin:react-hooks/recommended",14  ],15  settings: {16    react: {17      version: "detect",18    },19    "import/resolver": {20      alias: [21        ["src", "./src"],22        ["components", "./src/components"],23        ["store", "./store"],24      ],25    },26  },27  env: {28    browser: true,29    node: true,30    es6: true,31  },32  plugins: ["@typescript-eslint", "react"],33  parserOptions: {34    ecmaFeatures: {35      jsx: true,36    },37    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features38    sourceType: "module", // Allows for the use of imports39  },40  rules: {41    "react/prop-types": "off", // Disable prop-types as we use TypeScript for type checking42    "@typescript-eslint/explicit-function-return-type": "off",43  },44  overrides: [45    // Override some TypeScript rules just for .js files46    {47      files: ["*.js"],48      rules: {49        "@typescript-eslint/no-var-requires": "off", //50      },51    },52  ],53};

When you stage your files and type git commit, you should see commitzen-conventional-log prompt.

husky result


Image by Lynn Coetzee from Pixabay