diff --git a/examples/expo-example/.storybook/local-addon-example/preview.js b/examples/expo-example/.storybook/local-addon-example/preview.js new file mode 100644 index 0000000000..08af7dec4a --- /dev/null +++ b/examples/expo-example/.storybook/local-addon-example/preview.js @@ -0,0 +1 @@ +console.log('preview-local-addon'); diff --git a/examples/expo-example/.storybook/local-addon-example/register.js b/examples/expo-example/.storybook/local-addon-example/register.js new file mode 100644 index 0000000000..38b521b1c3 --- /dev/null +++ b/examples/expo-example/.storybook/local-addon-example/register.js @@ -0,0 +1 @@ +console.log('register-local-addon'); diff --git a/examples/expo-example/.storybook/main.ts b/examples/expo-example/.storybook/main.ts index 8c0b52f8e4..f9243330af 100644 --- a/examples/expo-example/.storybook/main.ts +++ b/examples/expo-example/.storybook/main.ts @@ -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, diff --git a/examples/expo-example/.storybook/storybook.requires.ts b/examples/expo-example/.storybook/storybook.requires.ts index fc9f4e8f91..3d3f410998 100644 --- a/examples/expo-example/.storybook/storybook.requires.ts +++ b/examples/expo-example/.storybook/storybook.requires.ts @@ -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 = [ { @@ -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; diff --git a/packages/react-native/scripts/common.js b/packages/react-native/scripts/common.js index 95385b8ca4..a613175432 100644 --- a/packages/react-native/scripts/common.js +++ b/packages/react-native/scripts/common.js @@ -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}`; @@ -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; } diff --git a/packages/react-native/scripts/generate.js b/packages/react-native/scripts/generate.js index f9bc6a4ed5..d8abbb0fdb 100644 --- a/packages/react-native/scripts/generate.js +++ b/packages/react-native/scripts/generate.js @@ -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`; @@ -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}')`);