Skip to content

Commit 6936c1d

Browse files
authored
feat(rspeedy/core): add callerName option to createRspeedy (#757)
## Summary Set the `callerName` option of `createRsbuild`, which can be accessed by Rsbuild plugins through `api.context.callerName`. See: - web-infra-dev/rsbuild#5204 - web-infra-dev/rslib#974 ## Checklist <!--- Check and mark with an "x" --> - [x] Tests updated (or not required). - [x] Documentation updated (or not required).
1 parent fe93e46 commit 6936c1d

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

.changeset/little-candies-carry.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
"@lynx-js/rspeedy": patch
3+
---
4+
5+
Add `callerName` option to `createRspeedy`.
6+
7+
It can be accessed by Rsbuild plugins through [`api.context.callerName`](https://rsbuild.dev/api/javascript-api/instance#contextcallername), and execute different logic based on this identifier.
8+
9+
```js
10+
export const myPlugin = {
11+
name: 'my-plugin',
12+
setup(api) {
13+
const { callerName } = api.context;
14+
15+
if (callerName === 'rslib') {
16+
// ...
17+
} else if (callerName === 'rspeedy') {
18+
// ...
19+
}
20+
},
21+
};
22+
```

packages/rspeedy/core/etc/rspeedy.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ export interface Config {
7171
export type ConsoleType = 'log' | 'warn' | 'error' | 'info' | 'debug' | 'profile' | 'profileEnd' | (string & Record<never, never>);
7272

7373
// @public
74-
export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment }: CreateRspeedyOptions): Promise<RspeedyInstance>;
74+
export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment, callerName, }: CreateRspeedyOptions): Promise<RspeedyInstance>;
7575

7676
// @public
7777
export interface CreateRspeedyOptions {
78+
callerName?: string;
7879
cwd?: string;
7980
environment?: CreateRsbuildOptions['environment'];
8081
loadEnv?: CreateRsbuildOptions['loadEnv'];

packages/rspeedy/core/src/create-rspeedy.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,43 @@ export interface CreateRspeedyOptions {
5555
* @defaultValue []
5656
*/
5757
environment?: CreateRsbuildOptions['environment']
58+
59+
/**
60+
* The name of the framework or tool that is currently invoking Rsbuild.
61+
* This allows plugins to tailor their behavior based on the calling context.
62+
*
63+
* @example
64+
*
65+
* Rsbuild plugins can access this value via `api.context.callerName`.
66+
*
67+
* ```js
68+
* export function myPlugin() {
69+
* return {
70+
* name: 'my-plugin',
71+
* setup(api) {
72+
* // Log the name of the tool invoking Rsbuild
73+
* console.log(`Called by: ${api.context.callerName}`);
74+
*
75+
* // Conditionally apply plugin logic based on caller
76+
* if (api.context.callerName === 'rspeedy') {
77+
* api.modifyRsbuildConfig((config) => {
78+
* // Apply rspeedy-specific config changes
79+
* return config;
80+
* });
81+
* } else if (api.context.callerName === 'rslib') {
82+
* api.modifyRsbuildConfig((config) => {
83+
* // Apply rslib-specific config changes
84+
* return config;
85+
* });
86+
* }
87+
* }
88+
* };
89+
* }
90+
* ```
91+
*
92+
* @defaultValue 'rspeedy'
93+
*/
94+
callerName?: string
5895
}
5996
/**
6097
* The `createRspeedy` method can let you create a Rspeedy instance and you can customize the build or development process in Node.js Runtime.
@@ -76,8 +113,13 @@ export interface CreateRspeedyOptions {
76113
* @public
77114
*/
78115
export async function createRspeedy(
79-
{ cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true, environment = [] }:
80-
CreateRspeedyOptions,
116+
{
117+
cwd = process.cwd(),
118+
rspeedyConfig = {},
119+
loadEnv = true,
120+
environment = [],
121+
callerName = 'rspeedy',
122+
}: CreateRspeedyOptions,
81123
): Promise<RspeedyInstance> {
82124
const config = applyDefaultRspeedyConfig(rspeedyConfig)
83125

@@ -87,6 +129,7 @@ export async function createRspeedy(
87129
loadEnv,
88130
rsbuildConfig: toRsbuildConfig(config) as RsbuildConfig,
89131
environment,
132+
callerName,
90133
}),
91134
import('./plugins/index.js'),
92135
])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 The Lynx Authors. All rights reserved.
2+
// Licensed under the Apache License Version 2.0 that can be found in the
3+
// LICENSE file in the root directory of this source tree.
4+
import type { RsbuildPluginAPI } from '@rsbuild/core'
5+
import { describe, expect, test } from 'vitest'
6+
7+
import { createRspeedy } from '../src/create-rspeedy.js'
8+
9+
describe('createRspeedy', () => {
10+
test('default callerName', async () => {
11+
const rspeedy = await createRspeedy({
12+
rspeedyConfig: {
13+
plugins: [
14+
{
15+
name: 'test',
16+
setup(api: RsbuildPluginAPI) {
17+
expect(api.context.callerName).toBe('rspeedy')
18+
},
19+
},
20+
],
21+
},
22+
})
23+
24+
await rspeedy.initConfigs()
25+
26+
expect.assertions(1)
27+
})
28+
29+
test('custom callerName', async () => {
30+
const rspeedy = await createRspeedy({
31+
callerName: 'my-custom-framework',
32+
rspeedyConfig: {
33+
plugins: [
34+
{
35+
name: 'test',
36+
setup(api: RsbuildPluginAPI) {
37+
expect(api.context.callerName).toBe('my-custom-framework')
38+
},
39+
},
40+
],
41+
},
42+
})
43+
44+
await rspeedy.initConfigs()
45+
46+
expect.assertions(1)
47+
})
48+
})

0 commit comments

Comments
 (0)