Skip to content

Commit 474aa1e

Browse files
authored
feat: export APIs to support modern.js rsbuild storybook addon (#7233)
1 parent 8a07b69 commit 474aa1e

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

.changeset/weak-words-wonder.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
'@modern-js/plugin-v2': patch
4+
'@modern-js/uni-builder': patch
5+
---
6+
7+
feat: export APIs to support modern.js rsbuild storybook addon
8+
feat: 暴露 createStorybookOptions 和 resolveStorybookOptions API 支持 storybook 插件

packages/cli/uni-builder/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ export {
5252
isHtmlDisabled,
5353
castArray,
5454
} from './shared/utils';
55+
56+
export { parseConfig as parseRspackConfig } from './rspack';

packages/solutions/app-tools/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
},
3030
"default": "./dist/cjs/index.js"
3131
},
32+
"./builder": {
33+
"types": "./dist/types/builder/index.d.ts",
34+
"jsnext:source": "./src/builder/index.ts",
35+
"default": "./dist/cjs/builder/index.js"
36+
},
3237
"./cli": {
3338
"types": "./dist/types/index.d.ts",
3439
"jsnext:source": "./src/index.ts",

packages/solutions/app-tools/src/builder/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ export async function createBuilderGenerator(bundler: 'webpack' | 'rspack') {
1111
);
1212
return createWebpackBuilderForModern;
1313
}
14+
15+
export { parseRspackConfig } from '@modern-js/uni-builder';
16+
17+
export {
18+
builderPluginAdapterBasic,
19+
builderPluginAdapterHooks,
20+
} from './shared/builderPlugins';

packages/toolkit/plugin-v2/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@
8888
"dependencies": {
8989
"@modern-js/runtime-utils": "workspace:*",
9090
"@modern-js/utils": "workspace:*",
91+
"@modern-js/types": "workspace:*",
9192
"@swc/helpers": "^0.5.17",
92-
"jiti": "1.21.7"
93+
"jiti": "1.21.7",
94+
"@rsbuild/core": "1.3.22"
9395
},
9496
"devDependencies": {
95-
"@modern-js/types": "workspace:*",
9697
"@modern-js/uni-builder": "workspace:*",
97-
"@rsbuild/core": "1.3.22",
9898
"@scripts/build": "workspace:*",
9999
"@scripts/jest-config": "workspace:*",
100100
"@types/jest": "^29",

packages/toolkit/plugin-v2/src/cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export {
3434
type ServerPluginConfig,
3535
} from './hooks';
3636
export { cli, createLoadedConfig, initAppDir, createCli, loadEnv } from './run';
37+
export { createStorybookOptions } from './run/create';

packages/toolkit/plugin-v2/src/cli/run/create.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,63 @@ export const createCli = <Extends extends CLIPluginExtends>() => {
178178
getPrevInitOptions: () => initOptions,
179179
};
180180
};
181+
182+
// for storybook get config
183+
type UselessOptions = 'handleSetupResult' | 'command' | 'internalPlugins';
184+
export const createStorybookOptions = async <Extends extends CLIPluginExtends>(
185+
options: Omit<CLIRunOptions<Extends>, UselessOptions>,
186+
) => {
187+
const pluginManager = createPluginManager();
188+
pluginManager.clear();
189+
190+
const { configFile, cwd, metaName = 'modern-js' } = options;
191+
const appDirectory = await initAppDir(cwd);
192+
193+
const loaded = await createLoadedConfig<Extends['config']>(
194+
appDirectory,
195+
configFile,
196+
);
197+
198+
pluginManager.addPlugins(
199+
(loaded.config as unknown as { plugins: Plugin[] }).plugins || [],
200+
);
201+
const plugins = (await pluginManager.getPlugins()) as CLIPlugin<Extends>[];
202+
203+
const context = await createContext<Extends>({
204+
appContext: initAppContext<Extends>({
205+
packageName: loaded.packageName,
206+
configFile: loaded.configFile,
207+
command: 'storybook',
208+
appDirectory,
209+
plugins,
210+
metaName,
211+
}),
212+
config: loaded.config,
213+
normalizedConfig: {},
214+
});
215+
216+
const pluginAPI = initPluginAPI<Extends>({
217+
context,
218+
pluginManager,
219+
});
220+
context.pluginAPI = pluginAPI;
221+
// In storybook scenario, we don't need to support old modernjs plugin apis
222+
for (const plugin of plugins) {
223+
await plugin.setup?.(pluginAPI);
224+
}
225+
226+
const extraConfigs = await context.hooks.config.call();
227+
228+
const normalizedConfig = await createResolveConfig<
229+
Extends['config'],
230+
Extends['normalizedConfig']
231+
>(loaded, extraConfigs as DeepPartial<Extends['config']>[]);
232+
233+
const resolved =
234+
await context.hooks.modifyResolvedConfig.call(normalizedConfig);
235+
236+
return {
237+
config: resolved || normalizedConfig,
238+
getAppContext: () => pluginAPI.getAppContext(),
239+
};
240+
};

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)