-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrstack.ts
More file actions
93 lines (82 loc) · 2.78 KB
/
Copy pathrstack.ts
File metadata and controls
93 lines (82 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { ConfigParams, RsbuildConfig } from '@rsbuild/core';
import { appendBuildContextPlugin, createBuildContextPlugin } from './build.ts';
import { resolveContextCapture, type ContextConfig } from './config.ts';
import { recordContextInputFiles } from './source.ts';
import { resolveContextWorkspace } from './workspace.ts';
type ContextRstackPluginOptions = {
config?: ContextConfig;
configFilePath: string | null;
configDependencies: readonly string[];
cwd: string;
};
type ContextRstackModifierContext = Readonly<{ params: ConfigParams }>;
type ContextBuildConfig = {
plugins?: RsbuildConfig['plugins'];
};
type ContextBuildModifier = <Config extends ContextBuildConfig>(
config: Config,
context: ContextRstackModifierContext,
) => Config | Promise<Config>;
type ContextRstackPluginApi = {
modifyConfig(kind: 'app' | 'lib', handler: ContextBuildModifier): void;
};
type ContextRstackPlugin = {
name: 'rstack:context';
setup(api: ContextRstackPluginApi): void;
};
const createRstackContextPlugin = (options: ContextRstackPluginOptions): ContextRstackPlugin => ({
name: 'rstack:context',
setup(api) {
const capture = resolveContextCapture(options.config);
if (capture === 'off') return;
const configPath = options.configFilePath ?? undefined;
let commonOptionsPromise:
| Promise<{
inputs?: Awaited<ReturnType<typeof recordContextInputFiles>>;
workspace: Awaited<ReturnType<typeof resolveContextWorkspace>>;
}>
| undefined;
const resolveCommonOptions = () =>
(commonOptionsPromise ??= (async () => {
const workspace = await resolveContextWorkspace(configPath ?? options.cwd);
const inputs =
configPath === undefined
? undefined
: await recordContextInputFiles(workspace.workspaceRoot, [
...new Set([configPath, ...options.configDependencies]),
]);
return { workspace, inputs };
})());
const register = (
kind: 'app' | 'lib',
producer: 'rsbuild' | 'rslib',
product: 'application' | 'library',
): void => {
api.modifyConfig(kind, async (config, { params }) => {
const common = await resolveCommonOptions();
return appendBuildContextPlugin(
config,
createBuildContextPlugin({
producer,
product,
capture,
...common,
configPath,
params,
variant: options.config?.variant,
}),
);
});
};
register('app', 'rsbuild', 'application');
register('lib', 'rslib', 'library');
},
});
export { createRstackContextPlugin };
export type {
ContextBuildModifier,
ContextRstackModifierContext,
ContextRstackPlugin,
ContextRstackPluginApi,
ContextRstackPluginOptions,
};