Skip to content

Commit

Permalink
feat: support registering async plugin in config (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Feb 26, 2024
1 parent 7dc1735 commit c0dee79
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 20 deletions.
42 changes: 42 additions & 0 deletions e2e/cases/async-plugin/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from '@playwright/test';
import { build, gotoPage, rspackOnlyTest } from '@e2e/helper';
import type { RsbuildPlugin } from '@rsbuild/core';

const asyncPlugin = async (): Promise<RsbuildPlugin> => {
await new Promise((resolve) => {
setTimeout(resolve);
});

return {
name: 'async-plugin',
setup(api) {
api.modifyRsbuildConfig((config, { mergeRsbuildConfig }) => {
return mergeRsbuildConfig(config, {
source: {
define: {
ENABLE_TEST: JSON.stringify(true),
},
},
});
});
},
};
};

rspackOnlyTest(
'should allow to register async plugin in plugins field',
async ({ page }) => {
const rsbuild = await build({
cwd: __dirname,
runServer: true,
plugins: [asyncPlugin()],
});

await gotoPage(page, rsbuild);

const testEl = page.locator('#test-el');
await expect(testEl).toHaveText('aaaaa');

await rsbuild.close();
},
);
12 changes: 12 additions & 0 deletions e2e/cases/async-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (ENABLE_TEST === true) {
const test = document.createElement('div');
test.id = 'test-el';
test.innerHTML = 'aaaaa';
document.body.appendChild(test);
}

const testEl = document.createElement('div');
testEl.id = 'test';
testEl.innerHTML = 'Hello Rsbuild!';

document.body.appendChild(testEl);
20 changes: 9 additions & 11 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { pluginSwc } from '@rsbuild/plugin-swc';
import type {
RsbuildConfig,
RsbuildPlugin,
RsbuildPlugins,
CreateRsbuildOptions,
} from '@rsbuild/core';
import type { Page } from 'playwright';
Expand All @@ -33,31 +34,28 @@ const noop = async () => {};

export const createRsbuild = async (
rsbuildOptions: CreateRsbuildOptions,
plugins: RsbuildPlugin[] = [],
plugins: RsbuildPlugins = [],
) => {
const { createRsbuild } = await import('@rsbuild/core');

rsbuildOptions.rsbuildConfig ||= {};
rsbuildOptions.rsbuildConfig.plugins = [
...(rsbuildOptions.rsbuildConfig.plugins || []),
...(plugins || []),
];

if (process.env.PROVIDE_TYPE === 'rspack') {
const rsbuild = await createRsbuild(rsbuildOptions);

if (plugins) {
rsbuild.addPlugins(plugins);
}

return rsbuild;
}

const { webpackProvider } = await import('@rsbuild/webpack');

rsbuildOptions.rsbuildConfig ||= {};
rsbuildOptions.rsbuildConfig.provider = webpackProvider;

const rsbuild = await createRsbuild(rsbuildOptions);

if (plugins) {
rsbuild.addPlugins(plugins);
}

const swc = pluginSwc();
if (!rsbuild.isPluginExists(swc.name)) {
rsbuild.addPlugins([swc]);
Expand Down Expand Up @@ -177,7 +175,7 @@ export async function build({
runServer = false,
...options
}: CreateRsbuildOptions & {
plugins?: RsbuildPlugin[];
plugins?: RsbuildPlugins;
runServer?: boolean;
}) {
process.env.NODE_ENV = 'production';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export async function createRsbuild(
};

if (rsbuildConfig.plugins) {
rsbuild.addPlugins(rsbuildConfig.plugins);
const plugins = await Promise.all(rsbuildConfig.plugins);
rsbuild.addPlugins(plugins);
}

return rsbuild;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/pluginManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { color, debug, logger, isFunction } from '@rsbuild/shared';
import type {
Falsy,
PluginManager,
RsbuildPlugin,
RsbuildPlugins,
RsbuildPluginAPI,
BundlerPluginInstance,
} from '@rsbuild/shared';
Expand Down Expand Up @@ -54,7 +54,7 @@ export function createPluginManager(): PluginManager {
let plugins: RsbuildPlugin[] = [];

const addPlugins = (
newPlugins: RsbuildPlugins,
newPlugins: Array<RsbuildPlugin | Falsy>,
options?: { before?: string },
) => {
const { before } = options || {};
Expand Down
19 changes: 17 additions & 2 deletions packages/document/docs/en/config/plugins.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
# plugins

Used to register Rsbuild plugins. Please check the [Plugin List](/plugins/list/index) to discover available plugins.
Used to register Rsbuild plugins.

Async plugin (promise) in the plugins array will be resolved, and falsy values will be ignored.

- **Type:**

```ts
type Falsy = false | null | undefined;

type RsbuildPlugins = (
| RsbuildPlugin
| Falsy
| Promise<RsbuildPlugin | Falsy>
)[];
```

- **Type:** `RsbuildPlugin[]`
- **Default:** `undefined`

> Please check out the [Plugin List](/plugins/list/index) page to discover all available plugins.
## Example

For example, register the Stylus plugin in Rsbuild.
Expand Down
25 changes: 23 additions & 2 deletions packages/document/docs/zh/config/plugins.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
# plugins

用于注册 Rsbuild 插件,请查看[插件列表](/plugins/list/index) 来发现可用的插件。
用于注册 Rsbuild 插件。

插件数组中的异步插件(promise)会自动被 resolve,falsy value 会被忽略。

- **类型:**

```ts
type Falsy = false | null | undefined;

type RsbuildPlugins = (
| RsbuildPlugin
| Falsy
| Promise<RsbuildPlugin | Falsy>
)[];
```

- **类型:** `RsbuildPlugin[]`
- **默认值:** `undefined`

> 请查看[插件列表](/plugins/list/index)页面来发现所有可用的插件。
## 示例

比如注册 Rsbuild 的 Stylus 插件。
Expand Down Expand Up @@ -64,6 +79,12 @@ export default defineConfig({
});
```

## 异步插件

```ts
import { RsbuildPlugin } from '@rsbuild/core';
```

## Rspack 插件

`plugins` 选项用于注册 Rsbuild 插件,如果你需要注册 Rspack 或 Webpack 插件,请使用 [tools.rspack](/config/tools/rspack)
Expand Down
11 changes: 9 additions & 2 deletions packages/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export type ModifyWebpackConfigFn = (

export type PluginManager = {
readonly plugins: RsbuildPlugin[];
addPlugins: (plugins: RsbuildPlugins, options?: { before?: string }) => void;
addPlugins: (
plugins: Array<RsbuildPlugin | Falsy>,
options?: { before?: string },
) => void;
removePlugins: (pluginNames: string[]) => void;
isPluginExists: (pluginName: string) => boolean;
/** The plugin API. */
Expand Down Expand Up @@ -108,7 +111,11 @@ export type RsbuildPlugin = {
remove?: string[];
};

export type RsbuildPlugins = (RsbuildPlugin | Falsy)[];
export type RsbuildPlugins = (
| RsbuildPlugin
| Falsy
| Promise<RsbuildPlugin | Falsy>
)[];

type PluginsFn<T = undefined> = T extends undefined
? () => Promise<RsbuildPlugin>
Expand Down

0 comments on commit c0dee79

Please sign in to comment.