Skip to content

Commit

Permalink
feat: export a list of modules for use in @storybook/react-native 6 (#…
Browse files Browse the repository at this point in the history
…122)

* feat: export a list of modules for use in @storybook/react-native 6

In @storybook/react-native version 6: the loader function passed to 'configure' should return void or an array of module exports that all contain a 'default' export. `loadStories()` just loads all the stories and `stories` is just a list of the story locations.  Adding `modules` so that we can also access a list of module exports.

* typo

* add modules export to readme

* feat: add modules to loader

* feat: use modules instead of storyFiles

* update storyFiles

* update snapshot

* fix: modules value on snapshot

* fix: return to storyFiles

* fix: remove file extensions

* fix: remove file extensions

* fix: trailing comma
  • Loading branch information
gezquinndesign committed Sep 4, 2022
1 parent de9cf44 commit fe72ee3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -155,6 +155,12 @@ An array of the stories that are loaded.

Returns: `string[]`

### storyLoader.modules

An array of the modules to load.

Returns: `NodeRequire[]`

## Story Loader Formatting

To ensure the formatting of your story loader is on par with the rest of your code base, `rnstl` uses [Prettier](https://prettier.io/) to format the generated story loaders. It will travese up the tree looking for a [Prettier configuration file](https://prettier.io/docs/en/configuration.html). If none is found, the defaul Prettier settings will be used.
9 changes: 9 additions & 0 deletions src/__snapshots__/template.test.ts.snap
Expand Up @@ -22,9 +22,18 @@ const stories = [
'./sub/sub/file5',
];
const modules = [
require('../file1'),
require('../sub/file2'),
require('../../parent/file3'),
require('./sub/file4'),
require('./sub/sub/file5'),
];
module.exports = {
loadStories,
stories,
modules,
};
"
`;
4 changes: 4 additions & 0 deletions src/locator.ts
Expand Up @@ -7,6 +7,7 @@ import { Configuration } from './configuration';
export type LoaderDefinition = {
outputFile: string;
storyFiles: string[];
modules: string[];
};

export const generateLoaderDefinition = async ({
Expand Down Expand Up @@ -36,5 +37,8 @@ export const generateLoaderDefinition = async ({
.map(f => getRelativePath(f, outputFileDir))
.map(f => stripExtension(f))
.map(f => formatPath(f)),
modules: uniqueFiles
.map(f => getRelativePath(f, outputFileDir))
.map(f => formatPath(f)),
};
};
5 changes: 5 additions & 0 deletions src/template.ts
Expand Up @@ -32,9 +32,14 @@ export const generateTemplate = async (
${formatter(loader.storyFiles, file => `'${file}'`, ',\n')}
];
const modules = [
${formatter(loader.storyFiles, file => `require('${file}')`, ',\n')}
];
module.exports = {
loadStories,
stories,
modules,
};
`;

Expand Down

0 comments on commit fe72ee3

Please sign in to comment.