Skip to content

Commit

Permalink
fix: allow addon config objects (#664)
Browse files Browse the repository at this point in the history
* fix: allow addon config objects

* fix
  • Loading branch information
dannyhw authored Jan 13, 2025
1 parent 706461f commit 19b9557
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/expo-example/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const main: StorybookConfig = {
// '../components/**/*.storiesof.?(ts|tsx|js|jsx)',
],
addons: [
'@storybook/addon-ondevice-controls',
{ name: '@storybook/addon-ondevice-controls' },
'@storybook/addon-ondevice-backgrounds',
'@storybook/addon-ondevice-actions',
'@storybook/addon-ondevice-notes',
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native/scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function getPreviewExists({ configPath }) {
}

function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts'], configPath) {
if (!addon || typeof addon !== 'string') return null;

try {
const basePath = `${addon}/${file}`;

Expand Down Expand Up @@ -91,6 +93,16 @@ function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts'], configP
return null;
}

function getAddonName(addon) {
if (typeof addon === 'string') return addon;

if (typeof addon === 'object' && addon.name && typeof addon.name === 'string') return addon.name;

console.error('Invalid addon configuration', addon);

return null;
}

module.exports = {
toRequireContext,
requireUncached,
Expand All @@ -99,4 +111,5 @@ module.exports = {
ensureRelativePathHasDot,
getPreviewExists,
resolveAddonFile,
getAddonName,
};
5 changes: 3 additions & 2 deletions packages/react-native/scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
getMain,
getPreviewExists,
resolveAddonFile,
getAddonName,
} = require('./common');
const { normalizeStories, globToRegexp } = require('@storybook/core/common');
const fs = require('fs');
Expand Down Expand Up @@ -51,7 +52,7 @@ function generate({ configPath, absolute = false, useJs = false }) {

for (const addon of main.addons) {
const registerPath = resolveAddonFile(
addon,
getAddonName(addon),
'register',
['js', 'mjs', 'jsx', 'ts', 'tsx'],
configPath
Expand All @@ -68,7 +69,7 @@ function generate({ configPath, absolute = false, useJs = false }) {

for (const addon of main.addons) {
const previewPath = resolveAddonFile(
addon,
getAddonName(addon),
'preview',
['js', 'mjs', 'jsx', 'ts', 'tsx'],
configPath
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export { start, prepareStories, getProjectAnnotations, updateView } from './Star

export interface StorybookConfig {
stories: StorybookConfigBase['stories'];
addons: string[];
addons: Array<string | { name: string; options?: Record<string, any> }>;
reactNative?: ReactNativeOptions;
}

0 comments on commit 19b9557

Please sign in to comment.