Skip to content

Commit

Permalink
fix: addon file resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyhw committed Oct 26, 2024
1 parent 0d9dbca commit 244ba1e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 0 additions & 1 deletion examples/expo-example/.storybook/storybook.requires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "@storybook/addon-ondevice-controls/register";
import "@storybook/addon-ondevice-backgrounds/register";
import "@storybook/addon-ondevice-actions/register";
import "@storybook/addon-ondevice-notes/register";
import "storybook-addon-deep-controls/register";

const normalizedStories = [
{
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native/scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,34 @@ function getPreviewExists({ configPath }) {
return !!getFilePathExtension({ configPath }, 'preview');
}

function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts']) {
try {
const basePath = path.join(addon, file);

require.resolve(basePath);

return basePath;
} catch (error) {}

for (const ext of extensions) {
try {
const filePath = path.join(addon, `${file}.${ext}`);

require.resolve(filePath);

return filePath;
} catch (error) {}
}

return null;
}

module.exports = {
toRequireContext,
requireUncached,
getFilePathExtension,
getMain,
ensureRelativePathHasDot,
getPreviewExists,
resolveAddonFile,
};
27 changes: 14 additions & 13 deletions packages/react-native/scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
ensureRelativePathHasDot,
getMain,
getPreviewExists,
resolveAddonFile,
} = require('./common');
const { normalizeStories, globToRegexp } = require('@storybook/core/common');
const fs = require('fs');
Expand Down Expand Up @@ -46,26 +47,26 @@ function generate({ configPath, absolute = false, useJs = false }) {
}`;
});

const registerAddons = main.addons?.map((addon) => `import "${addon}/register";`).join('\n');
let registerAddons = '';

for (const addon of main.addons) {
const registerPath = resolveAddonFile(addon, 'register', ['js', 'mjs', 'ts', 'tsx']);

if (registerPath) {
registerAddons += `import "${registerPath}";\n`;
}
}

const docTools = 'require("@storybook/react-native/dist/preview")';

const enhancers = [docTools];

for (const addon of main.addons) {
let addonPath;
try {
addonPath = path.dirname(require.resolve(addon));
} catch (error) {
console.error(`Failed to resolve addon: ${addon}`, error);

return null; // Skip this addon if it cannot be resolved
}

const isPreviewFileExists = getPreviewExists({ configPath: addonPath });
const previewPath = resolveAddonFile(addon, 'preview', ['js', 'mjs', 'jsx', 'ts', 'tsx']);

if (isPreviewFileExists) {
enhancers.push(`require('${addon}/preview')`);
if (previewPath) {
enhancers.push(`require('${previewPath}')`);
continue;
}
}

Expand Down

0 comments on commit 244ba1e

Please sign in to comment.