refactor(v2): Expose docusaurus module type aliases to end-users #2608
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
In #2578, I migrated all
@docusaurus/core
to TypeScript. During the migration, I have to declare modules to make TypeScript understand docusaurus-specific webpack aliases like@generated
@theme
. I put those declarations in atypes.d.ts
file inside@docusaurus/core
.In order to provide better TypeScript support for end-users, these aliases information also has to be available externally. TypeScript won't scan all
node_modules
to find thistypes.d.ts
. Therefore, we need a way to tell to TypeScript to find the module declaration file.My solution is to use the
<reference types=... />
triple-slash directive. I moved the module declaration to a new package@docusaurus/module-type-aliases
, and put/// <reference types="@docusaurus/module-type-aliases" />
in place of the original
types.d.ts
.Then once we published
@docusaurus/module-type-aliases
, An end-user can install it and add a filedocusaurus.d.ts
to their root with content/// <reference types="@docusaurus/module-type-aliases" />
, then all the red-squiggly line under@theme
,@generated
will be gone! User doesn't need to go through the hack mentioned in #2578 (comment).(This is the same approach taken by
create-teact-app
. It declares the css modules here, and link it in package.json here, and it will generate areact-app-env.d.ts
with/// <reference types="react-scripts" />
in user's project folder).Note
You might wonder why I didn't put the declarations in
@docusaurus/types
. In fact, this is the first thing I tried. However, it doesn't work well to mix module declaration and type definition together in one file, so I have to separate these two packages.Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
Everything still compiles and the preview site still works, which shows that this diff doesn't break anything.
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.)