Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,3 @@ updates:
svgr:
patterns:
- '@svgr/*'
# Rsdoctor plugins pin exact @rsdoctor/core and @rsdoctor/sdk versions
rsdoctor:
patterns:
- '@rsdoctor/*'
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-rsdoctor/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@docusaurus/plugin-rsdoctor`

[Rsdoctor](https://rsdoctor.dev/) plugin for Docusaurus.
[Rsdoctor](https://rsdoctor.rs/) plugin for Docusaurus.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-rsdoctor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@docusaurus/core": "workspace:*",
"@docusaurus/types": "workspace:*",
"@docusaurus/utils-validation": "workspace:*",
"@rsdoctor/rspack-plugin": "catalog:",
"@rsdoctor/core": "catalog:",
"@rsdoctor/webpack-plugin": "catalog:",
"tslib": "catalog:"
},
Expand Down
83 changes: 47 additions & 36 deletions packages/docusaurus-plugin-rsdoctor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,71 @@
* LICENSE file in the root directory of this source tree.
*/

import {RsdoctorRspackMultiplePlugin} from '@rsdoctor/rspack-plugin';
import {RsdoctorWebpackMultiplePlugin} from '@rsdoctor/webpack-plugin';
import type {ConfigureWebpackResult} from '@docusaurus/types/src/plugin';
import type {CurrentBundler, LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Options} from './options';

type RsdoctorPluginType = typeof RsdoctorRspackMultiplePlugin;
type BundlerConfig = ConfigureWebpackResult;

function getRsdoctorPlugin(
currentBundlerName: CurrentBundler['name'],
): RsdoctorPluginType {
return currentBundlerName === 'rspack'
? RsdoctorRspackMultiplePlugin
: // Cast fixes "error TS2321: Excessive stack depth"
// This may be a temporary TS 7.0 issue?
(RsdoctorWebpackMultiplePlugin as unknown as RsdoctorPluginType);
}
type RsdoctorOptions = PluginOptions['rsdoctorOptions'];

type CreateBundlerConfig = (params: {name: string}) => BundlerConfig;

function createRsdoctorBundlerPlugin({
isServer,
currentBundler,
options,
// Rsdoctor v2 only supports Rspack, Webpack sites keep using Rsdoctor v1
// Each Rsdoctor package is imported lazily, only for the current bundler
async function getCreateBundlerConfig({
currentBundlerName,
rsdoctorOptions,
}: {
isServer: boolean;
currentBundler: CurrentBundler;
options: PluginOptions;
}) {
const RsdoctorPlugin = getRsdoctorPlugin(currentBundler.name);
currentBundlerName: CurrentBundler['name'];
rsdoctorOptions: RsdoctorOptions;
}): Promise<CreateBundlerConfig> {
return currentBundlerName === 'rspack'
? getCreateRspackConfig(rsdoctorOptions)
: getCreateWebpackConfig(rsdoctorOptions);
}

// Little type incompatibility?
type WebpackPlugin = NonNullable<ConfigureWebpackResult['plugins']>[number];
async function getCreateRspackConfig(
rsdoctorOptions: RsdoctorOptions,
): Promise<CreateBundlerConfig> {
const {RsdoctorRspackPlugin, migrateRsdoctorOptions} =
await import('@rsdoctor/core');
// Rsdoctor v1 options keep working, with a warning to migrate them
// A single instance applied to both compilers merges them in one report
const plugin = new RsdoctorRspackPlugin(
migrateRsdoctorOptions(rsdoctorOptions),
);
return ({name}) => ({
// Rsdoctor v2 names each compiler report after the compiler name
name,
plugins: [plugin] as unknown as BundlerConfig['plugins'],
});
}

return new RsdoctorPlugin({
name: isServer ? 'server' : 'client',
...options.rsdoctorOptions,
}) as unknown as WebpackPlugin;
async function getCreateWebpackConfig(
rsdoctorOptions: RsdoctorOptions,
): Promise<CreateBundlerConfig> {
const {RsdoctorWebpackMultiplePlugin} =
await import('@rsdoctor/webpack-plugin');
return ({name}) => ({
plugins: [
new RsdoctorWebpackMultiplePlugin({name, ...rsdoctorOptions}),
] as unknown as BundlerConfig['plugins'],
});
}

export default (async function pluginRsdoctor(
context: LoadContext,
options: PluginOptions,
): Promise<Plugin | null> {
const createBundlerConfig = await getCreateBundlerConfig({
currentBundlerName: context.currentBundler.name,
rsdoctorOptions: options.rsdoctorOptions,
});
return {
name: 'docusaurus-plugin-rsdoctor',
configureWebpack: (__config, isServer) => {
return {
plugins: [
createRsdoctorBundlerPlugin({
isServer,
currentBundler: context.currentBundler,
options,
}),
],
};
return createBundlerConfig({name: isServer ? 'server' : 'client'});
},
};
});
Expand Down
Loading
Loading