-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
ReactNative: Add true entrypoint generation #34663
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
70ab368
feat(create-storybook): add entrypoint generation for React Native
ndelangen 8e06425
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen 7841294
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen 99ce1fc
Enhance React Native entrypoint generation with Expo support
ndelangen ce25876
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen 7e9ffa8
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen 0d30814
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen e501c33
Refactor React Native entrypoint templates to improve AsyncStorage in…
ndelangen bad5dce
Update tests for React Native entrypoint generation to use inline sna…
ndelangen 854d505
Merge branch 'next' into norbert/m2-entrypoint-generation
ndelangen be238db
Refactor tests for React Native entrypoint generation to enhance inli…
ndelangen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
267 changes: 267 additions & 0 deletions
267
code/lib/create-storybook/src/generators/REACT_NATIVE/generateEntrypoint.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
|
|
||
| import { SupportedLanguage } from 'storybook/internal/types'; | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { generateReactNativeEntrypoint, getEntrypointTemplatePath } from './generateEntrypoint.ts'; | ||
|
|
||
| describe('generateReactNativeEntrypoint', () => { | ||
| it('resolves Expo template path when expo variant is requested', () => { | ||
| const templatePath = getEntrypointTemplatePath('expo'); | ||
|
|
||
| expect(path.basename(templatePath)).toMatchInlineSnapshot(`"index.expo.js"`); | ||
| }); | ||
|
|
||
| it('generates .rnstorybook/index.ts for TypeScript projects', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-ts-')); | ||
|
|
||
| try { | ||
| const result = await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.TYPESCRIPT, | ||
| cwd, | ||
| }); | ||
| const outputPath = path.join(cwd, '.rnstorybook', 'index.ts'); | ||
| const output = await readFile(outputPath, 'utf-8'); | ||
|
|
||
| expect(path.relative(cwd, result.targetPath)).toMatchInlineSnapshot( | ||
| `".rnstorybook/index.ts"` | ||
| ); | ||
| expect(output).toMatchInlineSnapshot(` | ||
| "import { AppRegistry } from 'react-native'; | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { view } from './storybook.requires'; | ||
|
|
||
| /** | ||
| * This file is user-editable. | ||
| * | ||
| * Use it as your React Native Storybook entrypoint and wrap \`StorybookUIRoot\` | ||
| * with application decorators/providers (theme, i18n, state, navigation, etc). | ||
| */ | ||
| const StorybookUIRoot = view.getStorybookUI({ | ||
| shouldPersistSelection: true, | ||
| storage: { | ||
| getItem: AsyncStorage.getItem, | ||
| setItem: AsyncStorage.setItem, | ||
| }, | ||
| }); | ||
|
|
||
| AppRegistry.registerComponent('main', () => StorybookUIRoot); | ||
|
|
||
| export default StorybookUIRoot; | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('generates .rnstorybook/index.js for JavaScript projects', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-js-')); | ||
|
|
||
| try { | ||
| const result = await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.JAVASCRIPT, | ||
| cwd, | ||
| }); | ||
| const outputPath = path.join(cwd, '.rnstorybook', 'index.js'); | ||
| const output = await readFile(outputPath, 'utf-8'); | ||
|
|
||
| expect(path.relative(cwd, result.targetPath)).toMatchInlineSnapshot( | ||
| `".rnstorybook/index.js"` | ||
| ); | ||
| expect(output).toMatchInlineSnapshot(` | ||
| "import { AppRegistry } from 'react-native'; | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { view } from './storybook.requires'; | ||
|
|
||
| /** | ||
| * This file is user-editable. | ||
| * | ||
| * Use it as your React Native Storybook entrypoint and wrap \`StorybookUIRoot\` | ||
| * with application decorators/providers (theme, i18n, state, navigation, etc). | ||
| */ | ||
| const StorybookUIRoot = view.getStorybookUI({ | ||
| shouldPersistSelection: true, | ||
| storage: { | ||
| getItem: AsyncStorage.getItem, | ||
| setItem: AsyncStorage.setItem, | ||
| }, | ||
| }); | ||
|
|
||
| AppRegistry.registerComponent('main', () => StorybookUIRoot); | ||
|
|
||
| export default StorybookUIRoot; | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('generates Expo-specific entrypoint contents for expo projects', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-expo-')); | ||
|
|
||
| try { | ||
| const result = await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.TYPESCRIPT, | ||
| templateVariant: 'expo', | ||
| cwd, | ||
| }); | ||
| const outputPath = path.join(cwd, '.rnstorybook', 'index.ts'); | ||
| const output = await readFile(outputPath, 'utf-8'); | ||
|
|
||
| expect(path.relative(cwd, result.targetPath)).toMatchInlineSnapshot( | ||
| `".rnstorybook/index.ts"` | ||
| ); | ||
| expect(output).toMatchInlineSnapshot(` | ||
| "import { registerRootComponent } from 'expo'; | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { view } from './storybook.requires'; | ||
|
|
||
| /** | ||
| * This file is user-editable. | ||
| * | ||
| * Use it as your React Native Storybook entrypoint and wrap \`StorybookUIRoot\` | ||
| * with application decorators/providers (theme, i18n, state, navigation, etc). | ||
| */ | ||
| const StorybookUIRoot = view.getStorybookUI({ | ||
| shouldPersistSelection: true, | ||
| storage: { | ||
| getItem: AsyncStorage.getItem, | ||
| setItem: AsyncStorage.setItem, | ||
| }, | ||
| }); | ||
|
|
||
| registerRootComponent(StorybookUIRoot); | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('overwrites existing target index file on rerun', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-overwrite-')); | ||
| const targetPath = path.join(cwd, '.rnstorybook', 'index.js'); | ||
|
|
||
| try { | ||
| await mkdir(path.dirname(targetPath), { recursive: true }); | ||
| await writeFile(targetPath, 'export default function Old() {};\n', 'utf-8'); | ||
|
|
||
| await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.JAVASCRIPT, | ||
| cwd, | ||
| }); | ||
| const output = await readFile(targetPath, 'utf-8'); | ||
|
|
||
| expect(output).toMatchInlineSnapshot(` | ||
| "import { AppRegistry } from 'react-native'; | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { view } from './storybook.requires'; | ||
|
|
||
| /** | ||
| * This file is user-editable. | ||
| * | ||
| * Use it as your React Native Storybook entrypoint and wrap \`StorybookUIRoot\` | ||
| * with application decorators/providers (theme, i18n, state, navigation, etc). | ||
| */ | ||
| const StorybookUIRoot = view.getStorybookUI({ | ||
| shouldPersistSelection: true, | ||
| storage: { | ||
| getItem: AsyncStorage.getItem, | ||
| setItem: AsyncStorage.setItem, | ||
| }, | ||
| }); | ||
|
|
||
| AppRegistry.registerComponent('main', () => StorybookUIRoot); | ||
|
|
||
| export default StorybookUIRoot; | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('keeps sibling extension file when generating target extension', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-sibling-')); | ||
| const tsPath = path.join(cwd, '.rnstorybook', 'index.ts'); | ||
|
|
||
| try { | ||
| await mkdir(path.dirname(tsPath), { recursive: true }); | ||
| await writeFile(tsPath, '// sibling\n', 'utf-8'); | ||
|
|
||
| await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.JAVASCRIPT, | ||
| cwd, | ||
| }); | ||
|
|
||
| const sibling = await readFile(tsPath, 'utf-8'); | ||
| const jsPath = path.join(cwd, '.rnstorybook', 'index.js'); | ||
| const generated = await readFile(jsPath, 'utf-8'); | ||
|
|
||
| expect(sibling).toMatchInlineSnapshot(` | ||
| "// sibling | ||
| " | ||
| `); | ||
| expect(generated).toMatchInlineSnapshot(` | ||
| "import { AppRegistry } from 'react-native'; | ||
| import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
|
||
| import { view } from './storybook.requires'; | ||
|
|
||
| /** | ||
| * This file is user-editable. | ||
| * | ||
| * Use it as your React Native Storybook entrypoint and wrap \`StorybookUIRoot\` | ||
| * with application decorators/providers (theme, i18n, state, navigation, etc). | ||
| */ | ||
| const StorybookUIRoot = view.getStorybookUI({ | ||
| shouldPersistSelection: true, | ||
| storage: { | ||
| getItem: AsyncStorage.getItem, | ||
| setItem: AsyncStorage.setItem, | ||
| }, | ||
| }); | ||
|
|
||
| AppRegistry.registerComponent('main', () => StorybookUIRoot); | ||
|
|
||
| export default StorybookUIRoot; | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| it('does not modify existing storybook.requires file', async () => { | ||
| const cwd = await mkdtemp(path.join(os.tmpdir(), 'sb-rn-entry-requires-')); | ||
| const requiresPath = path.join(cwd, '.rnstorybook', 'storybook.requires.ts'); | ||
| const originalRequires = 'export const view = {};\n'; | ||
|
|
||
| try { | ||
| await mkdir(path.dirname(requiresPath), { recursive: true }); | ||
| await writeFile(requiresPath, originalRequires, 'utf-8'); | ||
|
|
||
| await generateReactNativeEntrypoint({ | ||
| language: SupportedLanguage.TYPESCRIPT, | ||
| cwd, | ||
| }); | ||
|
|
||
| const requiresOutput = await readFile(requiresPath, 'utf-8'); | ||
| expect(requiresOutput).toMatchInlineSnapshot(` | ||
| "export const view = {}; | ||
| " | ||
| `); | ||
| } finally { | ||
| await rm(cwd, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
| }); |
50 changes: 50 additions & 0 deletions
50
code/lib/create-storybook/src/generators/REACT_NATIVE/generateEntrypoint.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises'; | ||
| import path, { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| import { RN_STORYBOOK_DIR } from '../../../../../core/src/shared/constants/config-folder.ts'; | ||
| import { SupportedLanguage } from 'storybook/internal/types'; | ||
|
|
||
| const ENTRYPOINT_TEMPLATE_DIR = join( | ||
| dirname(fileURLToPath(import.meta.resolve('create-storybook/package.json'))), | ||
| 'templates', | ||
| 'react-native' | ||
| ); | ||
|
|
||
| export type ReactNativeEntrypointTemplateVariant = 'default' | 'expo'; | ||
|
|
||
| export const getEntrypointExtension = (language: SupportedLanguage) => { | ||
| return language === SupportedLanguage.TYPESCRIPT ? 'ts' : 'js'; | ||
| }; | ||
|
|
||
| export const getEntrypointTemplatePath = ( | ||
| templateVariant: ReactNativeEntrypointTemplateVariant = 'default' | ||
| ) => { | ||
| const templateFileName = templateVariant === 'expo' ? 'index.expo.js' : 'index.js'; | ||
| return join(ENTRYPOINT_TEMPLATE_DIR, templateFileName); | ||
| }; | ||
|
|
||
| export const generateReactNativeEntrypoint = async ({ | ||
| language, | ||
| templateVariant = 'default', | ||
| cwd = process.cwd(), | ||
| }: { | ||
| language: SupportedLanguage; | ||
| templateVariant?: ReactNativeEntrypointTemplateVariant; | ||
| cwd?: string; | ||
| }) => { | ||
| const extension = getEntrypointExtension(language); | ||
| const templatePath = getEntrypointTemplatePath(templateVariant); | ||
| const targetDir = path.join(cwd, RN_STORYBOOK_DIR); | ||
| const targetPath = path.join(targetDir, `index.${extension}`); | ||
|
|
||
| const templateContents = await readFile(templatePath, 'utf-8'); | ||
|
|
||
| await mkdir(targetDir, { recursive: true }); | ||
| await writeFile(targetPath, templateContents, 'utf-8'); | ||
|
|
||
| return { | ||
| targetPath, | ||
| extension, | ||
| }; | ||
| }; | ||
31 changes: 31 additions & 0 deletions
31
code/lib/create-storybook/src/generators/REACT_NATIVE/index.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { detectReactNativeEntrypointTemplateVariant } from './index.ts'; | ||
|
|
||
| describe('detectReactNativeEntrypointTemplateVariant', () => { | ||
| it('returns expo when expo dependency is present', () => { | ||
| expect( | ||
| detectReactNativeEntrypointTemplateVariant({ | ||
| expo: '^51.0.0', | ||
| 'react-native': '0.76.0', | ||
| }) | ||
| ).toBe('expo'); | ||
| }); | ||
|
|
||
| it('returns expo when expo-router dependency is present', () => { | ||
| expect( | ||
| detectReactNativeEntrypointTemplateVariant({ | ||
| 'expo-router': '^4.0.0', | ||
| 'react-native': '0.76.0', | ||
| }) | ||
| ).toBe('expo'); | ||
| }); | ||
|
|
||
| it('returns default when expo dependencies are missing', () => { | ||
| expect( | ||
| detectReactNativeEntrypointTemplateVariant({ | ||
| 'react-native': '0.76.0', | ||
| }) | ||
| ).toBe('default'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.