Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storybook): add adapter for babel and webpack #4946

Merged
merged 2 commits into from
Nov 17, 2023
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
6 changes: 6 additions & 0 deletions .changeset/hungry-balloons-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/storybook-builder': patch
---

feat(storybook): add adapter for storybook addons, read builderConfig
feat(storybook): 为 storybook addons 添加一些适配逻辑,读取builderConfig
8 changes: 6 additions & 2 deletions packages/storybook/builder/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createBuilder } from '@modern-js/builder';
import { createBuilder, mergeBuilderConfig } from '@modern-js/builder';
import { loadConfig } from '@modern-js/core';
import type { Options } from '@storybook/types';
import type { Compiler } from '@modern-js/builder-shared/webpack-dev-middleware';
Expand All @@ -14,6 +14,7 @@ export async function getCompiler(
const bundler = builderOptions.bundler || 'webpack';

const { presets } = options;

const entries = await presets.apply<string[]>('entries', []);

const res = await runWithErrorMsg(
Expand All @@ -26,7 +27,10 @@ export async function getCompiler(
(await presets.apply<BuilderConfig | void>('modern', loadedConfig)) ||
loadedConfig;

const provider = await getProvider(bundler, finalConfig);
const provider = await getProvider(
bundler,
mergeBuilderConfig(finalConfig, builderOptions.builderConfig) || {},
);

if (!provider) {
throw new Error(`@modern-js/builder-${bundler}-provider not found `);
Expand Down
86 changes: 84 additions & 2 deletions packages/storybook/builder/src/plugin-storybook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* eslint-disable max-lines */
import { isAbsolute, join, resolve } from 'path';
import { slash, watch, globby, applyOptionsChain } from '@modern-js/utils';
import {
slash,
watch,
globby,
applyOptionsChain,
logger,
} from '@modern-js/utils';
import {
BuilderPlugin,
SharedBuilderConfig,
Expand All @@ -22,7 +28,7 @@ import {
loadPreviewOrConfigFile,
} from '@storybook/core-common';
import { globals } from '@storybook/preview/globals';

import type { PluginItem } from '@babel/core';
import type {
BuilderPluginAPI as WebpackAPI,
WebpackConfig,
Expand Down Expand Up @@ -119,6 +125,8 @@ export const pluginStorybook: (
};

if ('modifyWebpackConfig' in api) {
addonAdapter(api, options);

api.modifyWebpackConfig(modifyConfig);
api.modifyWebpackChain(async chain => {
await applyDocgenWebpack(chain, options);
Expand Down Expand Up @@ -472,3 +480,77 @@ async function watchStories(
);
return watcher;
}

/**
* Some addons expose babel plugins and presets, or modify webpack
*/
function addonAdapter(api: WebpackAPI | RspackAPI, options: Options) {
const { presets } = options;

api.modifyBuilderConfig(async finalConfig => {
const babelOptions = await presets.apply('babel', {}, { ...options });
finalConfig.tools ??= {};
const userConfig = finalConfig.tools.babel;
finalConfig.tools.babel = (config, utils) => {
const getPluginName = (plugin: PluginItem) =>
Array.isArray(plugin) ? plugin[0] : plugin;
const getOptions = (plugin: PluginItem) =>
Array.isArray(plugin) ? plugin[1] : null;

const replaceOrInsert = (plugin: PluginItem, plugins: PluginItem[]) => {
const pluginName = getPluginName(plugin);

const append = [];
for (let i = 0; i < plugins.length; i++) {
if (getPluginName(plugins[i]) === pluginName) {
if (getOptions(plugin)) {
logger.info(
`Detected duplicated babel plugin or presets: ${pluginName}, overrides with the new one`,
);
plugins[i] = plugin;
}
} else {
append.push(plugin);
}
}

plugins.push(...append);
};

const currentPlugins = config.plugins || [];
const currentPresets = config.presets || [];

// O(n * n) but the number of plugins should be small
for (const plugin of babelOptions.plugins || []) {
replaceOrInsert(plugin, currentPlugins);
}
for (const preset of babelOptions.presets || []) {
replaceOrInsert(preset, currentPresets);
}

const finalConfig = {
...config,
...babelOptions,
plugins: currentPlugins,
presets: currentPresets,
};

if (typeof userConfig === 'function') {
return userConfig(finalConfig, utils);
} else if (typeof userConfig === 'object') {
return { ...finalConfig, ...userConfig };
} else {
return finalConfig;
}
};
});

(api as WebpackAPI).modifyWebpackConfig(async config => {
const finalDefaultConfig = await presets.apply(
'webpackFinal',
config,
options,
);
return finalDefaultConfig;
});
}