Gathering detailed insights and metrics for babel-plugin-storybook-csf-title
Gathering detailed insights and metrics for babel-plugin-storybook-csf-title
Gathering detailed insights and metrics for babel-plugin-storybook-csf-title
Gathering detailed insights and metrics for babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
npm install babel-plugin-storybook-csf-title
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
17 Stars
27 Commits
4 Forks
4 Watchers
8 Branches
12 Contributors
Updated on Sep 08, 2023
Latest Version
2.1.0
Package Id
babel-plugin-storybook-csf-title@2.1.0
Unpacked Size
30.29 kB
Size
9.31 kB
File Count
8
NPM Version
6.14.12
Node Version
12.22.1
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
5
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
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:
In this scenario, the plugin creates a default export { title: "foo" }
.
E.g.,
1import React from 'react'; 2import Component from './index'; 3 4export const Example = () => <Component />;
is transformed into
1import React from 'react'; 2import Component from './index'; 3 4export const Example = () => <Component />; 5 6export default { title: 'foo' };
In this scenario, the plugin adds a title: foo
property to the existing export.
E.g.,
1import React from 'react'; 2import Component from './index'; 3 4export default { 5 something: 'something' 6}; 7 8export const Example = () => <Component />;
is transformed into
1import React from 'react'; 2import Component from './index'; 3 4export default { 5 title: 'foo' 6 something: 'something' 7}; 8 9export const Example = () => <Component />;
If the existing export already contains a title
property, an error is thrown.
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"
,
1import React from 'react'; 2import Component from './index'; 3 4export default () => <Component />;
is transformed into
1import React from 'react'; 2import Component from './index'; 3 4export const Default = () => <Component />; 5 6export default { 7 title: 'foo' 8};
If a ${renameDefaultExportsTo}
export already exists, an error is thrown.
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:
1plugins: [ 2 ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }], 3]
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:
1/* add plugin to babel, however disable it by default */ 2plugins: [ 3 ['babel-plugin-storybook-csf-title', false], 4], 5/* enable the plugin for all files that match your story name pattern */ 6overrides: [{ 7 include: /\/stories\.(ts|tsx)$/, 8 plugins: [ 9 ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }] 10 ] 11}]
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 codeundefined
(or any other value) - raise an error if processing a file that already defines a titlerenameDefaultExportsTo
is an optional string value that controls scenario 3 as described above. It is undefined
by default.
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:
1const path = require('path'); 2const pkgUp = require('pkg-up'); 3 4module.exports = (state) => { 5 6 // find the closest package.json 7 const packageJsonPath = pkgUp.sync({ cwd: state.filename }); 8 9 // read the package.json 10 const packageJson = require(packageJsonPath); 11 12 // get the path of the story file relative to the package root 13 const { dir: packageJsonDir } = path.parse(packageJsonPath); 14 const { dir: fileDir, name: fileName } = path.parse(path.relative(packageJsonDir, state.filename)); 15 16 const storybookPath = [ 17 // package name; "/" has meaning to storybook, hence replace a possible "/" by "|" 18 packageJson.name.replace('/', '|'), 19 20 // file dir 21 ...fileDir.split(path.sep), 22 ]; 23 24 // handle file names 25 if (fileName === 'examples' || fileName === 'stories') { 26 // nothing to do 27 } else if (fileName.endsWith('.stories')) { 28 storybookPath.push(fileName.slice(0, '.stories'.length + 1)); 29 } else if (fileName.endsWith('.examples')) { 30 storybookPath.push(fileName.slice(0, '.examples'.length + 1)); 31 } 32 33 return storybookPath.join('/'); 34}
Contributions to babel-plugin-storybook-csf-title
are welcome! Please see CONTRIBUTING.md for details.
Copyright (c) 2020 Atlassian and others. Apache 2.0 licensed, see LICENSE file.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/14 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
27 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More