refactor(v2): Convert docusaurus-core to TypeScript #2578
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
I believe this is the first step to support #2470. In order to use write TypeScript theme components, we should first have solid types for the core. Instead of writing
d.ts
files manually, it's better to write the core in TS and let TS generate correct type declaration files.Have you read the Contributing Guidelines on pull requests?
Yes
Implementation
It turns out that rewriting the code in TS is not as simple as changing the file extension and add a bunch of missing type annotations. It's complicated for the following reasons:
@docusaurus
,@generated
,@theme
. TypeScript doesn't understand these aliases natively.client/serverEntry.js
imports apackage.json
file outside of TypeScript 'rootDir' and TS is not happy about that.My solutions:
types.d.ts
that declares those modules. For@docusaurus/...
, I replaced that with relative imports. For@generated/...
, I looked at the generated files inwebsite/.docusaurus
and add the types. I left some types asany
since they appeared to be too dynamic. For@theme
, I added a blanket ruledeclare module '@theme/*' { const component: any; export default component }
, since I believe it's better to deal with this problem once we start to converting themes to TS.src/client
only and one for everything else but excluding client. The tsconfig forsrc/client
emits ESModule code and the other one emits commonjs code.client/serverEntry.js
to TS. I tried to replace that import with a file read, but it breaks the build.Test Plan
Check each page in the preview website. Nothing breaks.
Related PRs
(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)