npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

storybook-loader

v0.1.4

Published

<h1 align="center">

Downloads

2,520

Readme

storybook-loader

npm version Build Status dependencies Status devDependencies Status codecov

Description in medium

auto-load all stories in storybook by storybook-load

Table of Contents

Description

storybook-loader is a package for storybook to automatically load ALL files in a folder.

storybook-loader analyze the webpack's require.context return value and load the analyzed results to storyies.

After finishing the configuration, you will be free from manually adding story, as storybook-loader will load it for you automatically.

Installation

storybook-loader is available as an npm package.

npm install --save--dev storybook-loader

How to use

Steps of loading javascript component

  1. Create xxx.stories.js under the target folder.

    import { loadJSStories } from 'storybook-loader';
    
    const req = require.context('./');
    
    loadJSStories(req);
  2. Create sub folder and add js files.

    The following structure:

    ./rootPattern1.js
    ./folder1/pattern1.js
    ./folder1/pattern2.js
    ./folder2/pattern1.jsx
    ./folder2/pattern2.jsx

    will display like this(rootPattern1 will be ignored):

    folder1
    |-pattern1
    |-pattern2
    folder2
    |-pattern1
    |-pattern2

Steps of loading markdown

  1. Create xxx.stories.js under the target folder.

    storybook 4.x

    import { loadMDStories } from 'storybook-loader';
    import { doc } from 'storybook-readme';
    
    const req = require.context('./');
    const options = {
      // decorate md's content
      contentFuncList: [
        doc,
      ],
      // [optional] hide AddOnPanel
      storySubFuncList: [
        [
          'addParameters',
          [{ options: { showAddonPanel: false } }],
        ],
      ],
    }
    
    loadMDStories(req, options);

    storybook 5.x

    import { loadMDStories } from 'storybook-loader';
    import { doc } from 'storybook-readme';
    
    const req = require.context('./');
    const options = {
      // decorate md's content
      contentFuncList: [
        doc,
      ],
      // [optional] hide AddOnPanel
      storySubFuncList: [
        [
          'addParameters',
          [{ options: { showPanel: false } }],
        ],
      ],
    }
    
    loadMDStories(req, options);
  2. Create sub folder and add md files.

    The following structure:

    ./rootPattern1.md
    ./folder1/pattern1.md
    ./folder1/pattern2.md
    ./folder2/pattern1.md
    ./folder2/pattern2.md

    will display like this(rootPattern1 will be ignored):

    folder1
    |-pattern1
    |-pattern2
    folder2
    |-pattern1
    |-pattern2

Steps of loading javascript component with markdown

When you not only like to load js components, but also show notes in control panel.

  1. Create xxx.stories.js under the target folder.

    storybook 4.x

    import { loadJSWithNotesStories } from 'storybook-loader';
    import { withNotes } from '@storybook/addon-notes';
    
    const req = require.context('./');
    const options = {
      // [optional] you can also addDecorator(withNotes) in config.js
      storySubFuncList: [
        [
          'addDecorator',
          [withNotes],
        ],
      ],
    };
    
    loadJSWithNotesStories(reqList, options);

    storybook 5.x

    import { loadJSWithNotesStories } from 'storybook-loader';
    
    const req = require.context('./');
    
    loadJSWithNotesStories(reqList);
  2. Create sub folder and add md files.

    The following structure:

    ./folder1/pattern1.js
    ./folder1/pattern1.md
    ./folder1/pattern2.js
    ./folder1/pattern2.md
    ./folder2/pattern1.jsx
    ./folder2/pattern1.md
    ./folder2/pattern2.jsx

    will display like this

    folder1
    |-pattern1 (display folder1/pattern1.md in notes panel)
    |-pattern2 (display folder1/pattern2.md in notes panel)
    folder2
    |-pattern1 (display folder2/pattern1.md in notes panel)
    |-pattern2 (display nothing in notes panel)

Instructions

parameter

| No. | Parameter | Required | Type | Default | Description | |:------|:---------:|:--------:|:----:|:--------|:------------| | 1 | requireContext | 〇 | object / object list | | instance of require.context OR an object return the same architecture to require.context | | 2 | options | | object | { groupByFolder: true, folderNameWhenEmpty: 'ALL' } | *see options below for more details | | 3 | thirdParamMakerOptions | | object | { loader: mdLoader, formatter: formatWithNotesObject } | *see options below for more details |

options

storySubFuncList

  • Default: []

  • Description: list of sub functions in storiesOf to call

  • Attention: parameters of sub function MUST be an array

  • Example:

     -- NO.1 --------------------------------------------------------
     storySubFuncList: [ 
       'subFunc1',
       [ 'addDecorator', [withNotes] ],
       [ 'addParameters', [{ options: { showPanel: false } }] ],
     ]
     ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
     stories.subFunc1();
     stories.addDecorator(withNotes);
     stories.addParameters({ options: { showPanel: false } });

contentFuncList

  • Default: [ ramda.identity ]

  • Description: list of pure functions for decorating file's contents. It's useful when you want to apply hocs(High Order Component) or hofs(High Order Function) to your content like withMUITheme, withLayout...

  • Attention: functions will receive ONLY ONE parameter -- the contents (could be string or React.Component). If function has more than one parameters, you can use util.unaryFunc to apply the rest parameters and then pass it to contentFuncList.

  • Example:

    -- NO.1 --------------------------------------------------------
    function decPrefixMsg(content, msg) {
      return `${msg}: ${content}`;
    }
    
    contentFuncList: [ 
      util.unaryFunc(decPrefixMsg, ['MyMessage is']),
    ]
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    stories.add(fileName, decPrefixMsg(content, 'MyMessage is'));
    
    -- NO.2 --------------------------------------------------------
    contentFuncList: [ 
      withMUITheme,
      withLayout,
    ]
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    stories.add(fileName, withLayout(withMUITheme(content)));

hierarchyRoot

  • Default: ''

  • Description: prefix of stories' name. If you've enabled hierarchyRootSeparator, xxx| will be the name of hierarchyRoot.

  • Example:

    -- NO.1 --------------------------------------------------------
    hierarchyRoot: 'Components|'
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    storiesOf('Components|' + folderName, module);

groupByFolder

  • Default: true

  • Description: use folder name as the root stories' name.

  • Attention: If you set groupByFolder to false, all stories will be added to ONE root named by folderNameWhenEmpty(default is ALL).

  • Example:

    -- NO.1 --------------------------------------------------------
    groupByFolder: true
    
    /Button/Pattern1.js
    /Button/Pattern2.js
    /TextField/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    Button
      - Pattern1
      - Pattern2
    TextField
      - Pattern1
          
    -- NO.2 --------------------------------------------------------
    groupByFolder: false
    
    /Button/Pattern1.js
    /Button/Pattern2.js
    /TextField/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    ALL
      - Pattern1 (the second Pattern1 will be ignored)
      - Pattern2

thirdParamMaker

  • Default: null

  • Description: function to make 3rd parameter of stories.add(xx, xx, 3rdParam). thirdParamMaker(instance of require.context, filePath).

  • Attention: this function is called after every single file is loaded.

  • Example: createMDThirdParamMaker

sort

  • Default: true

  • Description: sort files by filename.

sortFunc

  • Default: (a, b) => a.localeCompare(b)

  • Description: sort function. sort by files asc as default.

noExt

  • Default: true

  • Description: display filename in menu without extention.

  • Example:

    -- NO.1 --------------------------------------------------------
    noExt: true
    
    /Button/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    Button
      - Pattern1
          
    -- NO.2 --------------------------------------------------------
    noExt: false
    
    /Button/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    Button
      - Pattern1.js

noExtRegExp

  • Default: null

  • Description: remove extention regexp.

  • Attention: if noExt is true, will use noExtRegExp > includeRegExp > regExpFromRequireContext.

  • Example: /\.md$/ to remove markdown's extention

includeRegExp

  • Default: null

  • Description: regexp for include files by file's full path.

  • Attention: will use includeRegExp > regExpFromRequireContext.

  • Example: /\.md$/ to add *.md files only.

excludeRegExp

  • Default: null

  • Description: regexp for exclude files by file's full path.

  • Example: /\.stories.js$/ to exclude all *.stories.js.

ignoreDotFolder

  • Default: true

  • Description: ignore files under root folder (. folder)

  • Example: -- NO.1 -------------------------------------------------------- ignoreDotFolder: true

    ./Pattern1.js
    /Button/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    Button
      - Pattern1
    
    -- NO.2 --------------------------------------------------------
    ignoreDotFolder: false
    
    ./Pattern1.js
    /Button/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    .
      - Pattern1
    Button
      - Pattern1

dotFolderName

  • Default: .

  • Description: used as folder name when it's a dot(.)

  • Example: -- NO.1 -------------------------------------------------------- ignoreDotFolder: false dotFolderName: 'RootFolder'

    ./Pattern1.js
    /Button/Pattern1.js
    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    RootFolder
      - Pattern1
    Button
      - Pattern1

third parameter options

ONLY nessacerry when use loadJSWithNotesStories or loadJsonWithNotesStories. Both of them are using createMDThirdParamMaker to make 3rd parameter in stories.add(xx, xx, 3rd param).

loader

  • Default: mdLoader

  • Description: loader to load files for making third param

formatter

  • Default: formatWithNotesObject() in createMDThirdParamMaker

  • Description: format the content

  • Example

    function formatWithNotesObject(content) {
      return {
        notes: {
          markdown: content,
        },
      };
    }

License

This project is licensed under the terms of the MIT license.