-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new Rsdoctor official plugin (#10588)
- Loading branch information
Showing
12 changed files
with
331 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.tsbuildinfo* | ||
tsconfig* | ||
__tests__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `@docusaurus/plugin-rsdoctor` | ||
|
||
[Rsdoctor](https://rsdoctor.dev/) plugin for Docusaurus. | ||
|
||
## Usage | ||
|
||
See [plugin-rsdoctor documentation](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-rsdoctor). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@docusaurus/plugin-rsdoctor", | ||
"version": "3.5.2", | ||
"description": "Rsdoctor plugin for Docusaurus.", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"watch": "tsc --build --watch" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/docusaurus.git", | ||
"directory": "packages/docusaurus-plugin-rsdoctor" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@docusaurus/core": "3.5.2", | ||
"@docusaurus/types": "3.5.2", | ||
"@docusaurus/utils-validation": "3.5.2", | ||
"@rsdoctor/webpack-plugin": "^0.4.6", | ||
"@rsdoctor/rspack-plugin": "^0.4.6", | ||
"tslib": "^2.6.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=18.0" | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
packages/docusaurus-plugin-rsdoctor/src/__tests__/options.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {normalizePluginOptions} from '@docusaurus/utils-validation'; | ||
import { | ||
validateOptions, | ||
type PluginOptions, | ||
type Options, | ||
DEFAULT_OPTIONS, | ||
} from '../options'; | ||
import type {Validate} from '@docusaurus/types'; | ||
|
||
function validate(options?: Options) { | ||
return validateOptions({ | ||
validate: normalizePluginOptions as Validate< | ||
Options | undefined, | ||
PluginOptions | ||
>, | ||
options, | ||
}); | ||
} | ||
|
||
function result(options?: Options) { | ||
return { | ||
id: 'default', | ||
...DEFAULT_OPTIONS, | ||
...options, | ||
}; | ||
} | ||
|
||
describe('validateOptions', () => { | ||
it('accepts undefined', () => { | ||
expect(validate(undefined)).toEqual(result(DEFAULT_OPTIONS)); | ||
}); | ||
|
||
it('accepts empty object', () => { | ||
expect(validate({})).toEqual(result(DEFAULT_OPTIONS)); | ||
}); | ||
|
||
it('accepts defaults', () => { | ||
expect(validate(DEFAULT_OPTIONS)).toEqual(result(DEFAULT_OPTIONS)); | ||
}); | ||
|
||
it('rejects null', () => { | ||
expect( | ||
// @ts-expect-error: TS should error | ||
() => validate(null), | ||
).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); | ||
}); | ||
|
||
it('rejects number', () => { | ||
expect( | ||
// @ts-expect-error: TS should error | ||
() => validate(42), | ||
).toThrowErrorMatchingInlineSnapshot(`""value" must be of type object"`); | ||
}); | ||
|
||
describe('rsdoctorOptions', () => { | ||
it('accepts undefined', () => { | ||
expect(validate({rsdoctorOptions: undefined})).toEqual( | ||
result(DEFAULT_OPTIONS), | ||
); | ||
}); | ||
|
||
it('accepts empty', () => { | ||
expect(validate({rsdoctorOptions: {}})).toEqual(result(DEFAULT_OPTIONS)); | ||
}); | ||
|
||
it('accepts any record', () => { | ||
expect( | ||
validate({rsdoctorOptions: {any: 'value', evenNumbers: 42}}), | ||
).toEqual( | ||
result({ | ||
...DEFAULT_OPTIONS, | ||
rsdoctorOptions: { | ||
any: 'value', | ||
evenNumbers: 42, | ||
}, | ||
}), | ||
); | ||
}); | ||
|
||
it('accepts default', () => { | ||
expect( | ||
validate({rsdoctorOptions: DEFAULT_OPTIONS.rsdoctorOptions}), | ||
).toEqual(result(DEFAULT_OPTIONS)); | ||
}); | ||
|
||
it('rejects number values', () => { | ||
expect(() => | ||
// @ts-expect-error: invalid type | ||
validate({rsdoctorOptions: 42}), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`""rsdoctorOptions" must be of type object"`, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {RsdoctorRspackMultiplePlugin} from '@rsdoctor/rspack-plugin'; | ||
import {RsdoctorWebpackMultiplePlugin} from '@rsdoctor/webpack-plugin'; | ||
import type {CurrentBundler, LoadContext, Plugin} from '@docusaurus/types'; | ||
import type {PluginOptions, Options} from './options'; | ||
|
||
function createRsdoctorBundlerPlugin({ | ||
isServer, | ||
currentBundler, | ||
options, | ||
}: { | ||
isServer: boolean; | ||
currentBundler: CurrentBundler; | ||
options: PluginOptions; | ||
}) { | ||
const RsdoctorPlugin = | ||
currentBundler.name === 'rspack' | ||
? RsdoctorRspackMultiplePlugin | ||
: RsdoctorWebpackMultiplePlugin; | ||
|
||
return new RsdoctorPlugin({ | ||
name: isServer ? 'server' : 'client', | ||
...options.rsdoctorOptions, | ||
}); | ||
} | ||
|
||
export default (async function pluginRsdoctor( | ||
context: LoadContext, | ||
options: PluginOptions, | ||
): Promise<Plugin | null> { | ||
return { | ||
name: 'docusaurus-plugin-rsdoctor', | ||
configureWebpack: (__config, isServer) => { | ||
return { | ||
plugins: [ | ||
createRsdoctorBundlerPlugin({ | ||
isServer, | ||
currentBundler: context.currentBundler, | ||
options, | ||
}), | ||
], | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
export {validateOptions} from './options'; | ||
|
||
export type {PluginOptions, Options}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import {Joi} from '@docusaurus/utils-validation'; | ||
import type {OptionValidationContext} from '@docusaurus/types'; | ||
|
||
export type PluginOptions = { | ||
rsdoctorOptions: Record<string, unknown>; | ||
}; | ||
|
||
export type Options = { | ||
rsdoctorOptions?: Record<string, unknown>; | ||
}; | ||
|
||
export const DEFAULT_OPTIONS: Partial<PluginOptions> = { | ||
rsdoctorOptions: {}, | ||
}; | ||
|
||
const pluginOptionsSchema = Joi.object<PluginOptions>({ | ||
rsdoctorOptions: Joi.object() | ||
.pattern(Joi.string(), Joi.any()) | ||
.optional() | ||
.default(DEFAULT_OPTIONS.rsdoctorOptions), | ||
}).default(DEFAULT_OPTIONS); | ||
|
||
export function validateOptions({ | ||
validate, | ||
options, | ||
}: OptionValidationContext<Options | undefined, PluginOptions>): PluginOptions { | ||
return validate(pluginOptionsSchema, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/// <reference types="@docusaurus/module-type-aliases" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"noEmit": false | ||
}, | ||
"include": ["src"], | ||
"exclude": ["**/__tests__/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
sidebar_position: 7 | ||
slug: /api/plugins/@docusaurus/plugin-rsdoctor | ||
--- | ||
|
||
# 📦 plugin-rsdoctor | ||
|
||
import APITable from '@site/src/components/APITable'; | ||
|
||
A [Rsdoctor](https://rsdoctor.dev/) plugin can help you troubleshoot the bundling phase of your Docusaurus site, supporting both Webpack and Rspack. | ||
|
||
:::tip | ||
|
||
Use it to figure out which plugin or loader is slowing down the bundler, and focus your efforts on optimizing the bottleneck. | ||
|
||
::: | ||
|
||
## Installation {#installation} | ||
|
||
```bash npm2yarn | ||
npm install --save @docusaurus/plugin-rsdoctor | ||
``` | ||
|
||
## Configuration {#configuration} | ||
|
||
Accepted fields: | ||
|
||
```mdx-code-block | ||
<APITable> | ||
``` | ||
|
||
| Name | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| `rsdoctorOptions` | `object` | `{}` | The [Rsdoctor bundler plugin options](https://rsdoctor.dev/config/options/options), forwarded as is | | ||
|
||
```mdx-code-block | ||
</APITable> | ||
``` | ||
|
||
### Example configuration {#ex-config} | ||
|
||
You can configure this plugin through plugin options. | ||
|
||
```js title="docusaurus.config.js" | ||
export default { | ||
plugins: [ | ||
[ | ||
'rsdoctor', | ||
{ | ||
rsdoctorOptions: { | ||
mode: 'lite', | ||
}, | ||
}, | ||
], | ||
], | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.