Skip to content

Commit

Permalink
fix: local addons not resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyhw committed Dec 12, 2024
1 parent eacb2e3 commit 94cb6cd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('preview-local-addon');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('register-local-addon');
1 change: 1 addition & 0 deletions examples/expo-example/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const main: StorybookConfig = {
'@storybook/addon-ondevice-actions',
'@storybook/addon-ondevice-notes',
'storybook-addon-deep-controls',
'./local-addon-example',
],
reactNative: {
playFn: false,
Expand Down
2 changes: 2 additions & 0 deletions examples/expo-example/.storybook/storybook.requires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@storybook/addon-ondevice-backgrounds/register";
import "@storybook/addon-ondevice-actions/register";
import "@storybook/addon-ondevice-notes/register";
import "storybook-addon-deep-controls/register";
import "./local-addon-example/register";

const normalizedStories = [
{
Expand Down Expand Up @@ -60,6 +61,7 @@ const annotations = [
require("@storybook/react-native/preview"),
require("@storybook/addon-ondevice-actions/preview"),
require("storybook-addon-deep-controls/preview"),
require("./local-addon-example/preview"),
];

global.STORIES = normalizedStories;
Expand Down
13 changes: 12 additions & 1 deletion packages/react-native/scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getPreviewExists({ configPath }) {
return !!getFilePathExtension({ configPath }, 'preview');
}

function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts']) {
function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts'], configPath) {
try {
const basePath = `${addon}/${file}`;

Expand All @@ -77,6 +77,17 @@ function resolveAddonFile(addon, file, extensions = ['js', 'mjs', 'ts']) {
} catch (error) {}
}

// attempt to resolve as a relative path for local addons
if (addon.startsWith('./') || addon.startsWith('../')) {
try {
const extension = getFilePathExtension({ configPath }, `${addon}/${file}`);

if (extension) {
return `${addon}/${file}`;
}
} catch (error) {}
}

return null;
}

Expand Down
14 changes: 12 additions & 2 deletions packages/react-native/scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ function generate({ configPath, absolute = false, useJs = false }) {
let registerAddons = '';

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

if (registerPath) {
registerAddons += `import "${registerPath}";\n`;
Expand All @@ -62,7 +67,12 @@ function generate({ configPath, absolute = false, useJs = false }) {
const enhancers = [docTools];

for (const addon of main.addons) {
const previewPath = resolveAddonFile(addon, 'preview', ['js', 'mjs', 'jsx', 'ts', 'tsx']);
const previewPath = resolveAddonFile(
addon,
'preview',
['js', 'mjs', 'jsx', 'ts', 'tsx'],
configPath
);

if (previewPath) {
enhancers.push(`require('${previewPath}')`);
Expand Down

0 comments on commit 94cb6cd

Please sign in to comment.