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

babel-plugin-storybook-csf-title

v2.1.0

Published

A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.

Downloads

12,098

Readme

babel-plugin-storybook-csf-title

Atlassian license PRs Welcome

A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.

Usage

The plugin adds a title property to all transformed files, based on the result of a toTitle function that is to be provided as an option to the plugin.

Assuming toTitle: () => 'foo', there are three general scenarios:

1️⃣ The file does not provide a default export

In this scenario, the plugin creates a default export { title: "foo" }.

E.g.,

import React from 'react';
import Component from './index';

export const Example = () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export const Example = () => <Component />;

export default { title: 'foo' };

2️⃣ The file provides an object as its default export

In this scenario, the plugin adds a title: foo property to the existing export.

E.g.,

import React from 'react';
import Component from './index';

export default { 
    something: 'something'
};

export const Example = () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export default { 
    title: 'foo'
    something: 'something'
};

export const Example = () => <Component />;

If the existing export already contains a title property, an error is thrown.

3️⃣ The file provides a non-object as its default export

If the renameDefaultExportsTo option is set, the plugin assumes that the default export is a component, and moves this component to a named export of the name ${renameDefaultExportsTo}. It then creates a default export { title: "foo" }.

E.g., assuming renameDefaultExportsTo is "Default",

import React from 'react';
import Component from './index';

export default () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export const Default = () => <Component />;

export default { 
    title: 'foo'
};

If a ${renameDefaultExportsTo} export already exists, an error is thrown.

Installation

Install the plugin e.g. via yarn;

yarn add --dev babel-plugin-storybook-csf-title

In your Babel configuration, add babel-plugin-storybook-csf-title as a plugin:

plugins: [
    ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }],
]

Note that the plugin really only makes sense for story files. You will want to make sure it is only applied to exactly these files, e.g. like this:

/* add plugin to babel, however disable it by default */
plugins: [
    ['babel-plugin-storybook-csf-title', false], 
],
/* enable the plugin for all files that match your story name pattern */
overrides: [{ 
    include: /\/stories\.(ts|tsx)$/, 
    plugins: [
        ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }]
    ]
}]

Options

The plugin takes three options, toTitle (required), ifTitleFound (optional), and renameDefaultExportsTo (optional):

  • toTitle is a function that, for every story file that is transformed, recieves Babel's state object, and must return the story file's title as a string. Most toTitle implementations will make decisions based on state.filename.

  • ifTitleFound is an optional string value that may either be set to:

    • 'skip' - skips adding a title if it has already been manually specified in the code
    • undefined (or any other value) - raise an error if processing a file that already defines a title
  • renameDefaultExportsTo is an optional string value that controls scenario 3 as described above. It is undefined by default.

Generating meaningful story names

In most cases, the story name will be generated based on the story file's file name. Here's a possible implementation of toTitle for a yarn workspaces-style monorepo setup:

const path = require('path');
const pkgUp = require('pkg-up');

module.exports = (state) => {

    // find the closest package.json
    const packageJsonPath = pkgUp.sync({ cwd: state.filename });

    // read the package.json
    const packageJson = require(packageJsonPath);

    // get the path of the story file relative to the package root
    const { dir: packageJsonDir } = path.parse(packageJsonPath);
    const { dir: fileDir, name: fileName } = path.parse(path.relative(packageJsonDir, state.filename));

    const storybookPath = [
        // package name; "/" has meaning to storybook, hence replace a possible "/" by "|"
        packageJson.name.replace('/', '|'),

        // file dir
        ...fileDir.split(path.sep),
    ];

    // handle file names
    if (fileName === 'examples' || fileName === 'stories') {
        // nothing to do
    } else if (fileName.endsWith('.stories')) {
        storybookPath.push(fileName.slice(0, '.stories'.length + 1));
    } else if (fileName.endsWith('.examples')) {
        storybookPath.push(fileName.slice(0, '.examples'.length + 1));
    }

    return storybookPath.join('/');
}

Contributions

Contributions to babel-plugin-storybook-csf-title are welcome! Please see CONTRIBUTING.md for details.

License

Copyright (c) 2020 Atlassian and others. Apache 2.0 licensed, see LICENSE file.

With ❤️ from Atlassian