Default Config

Let’s learn about the default config that comes with Storybook.

Babel

We use Babel for JavaScript(ES6) transpiling.Here are some key features of Storybook’s Babel configurations.

ES2016+ Support

We have added ES2016 support with Babel for transpiling your JS code.In addition to that, we’ve added a few experimental features, like object spreading and async await.Check out our source to learn more about these plugins.

.babelrc support

If your project has a .babelrc file, we’ll use that instead of the default config file.So, you could use any babel plugins or presets that you have used in your project with Storybook.

Webpack

We use Webpack to serve and load JavaScript modules for the web.

The webpack config is configurable, and the default can depend on which framework you’re using and whether you’ve used a generator like Create React App or Angular CLI etc.

We’re trying to make storybook more zero-config over time, help to hook into the config of generators is very welcome.

This is what the config for storybook looks like when using CRA in dev-mode:

  1. {
  2. mode: 'development',
  3. bail: false,
  4. devtool: '#cheap-module-source-map',
  5. entry: [
  6. '@storybook/core/dist/server/common/polyfills.js',
  7. '@storybook/core/dist/server/preview/globals.js',
  8. '<your-storybook-dir>/config.js',
  9. 'webpack-hot-middleware/client.js?reload=true',
  10. ],
  11. output: {
  12. path: './',
  13. filename: '[name].[hash].bundle.js',
  14. publicPath: '',
  15. },
  16. plugins: [
  17. HtmlWebpackPlugin {
  18. options: {
  19. template: '@storybook/core/dist/server/templates/index.ejs',
  20. templateContent: false,
  21. templateParameters: [Function: templateParameters],
  22. filename: 'iframe.html',
  23. hash: false,
  24. inject: false,
  25. compile: true,
  26. favicon: false,
  27. minify: undefined,
  28. cache: true,
  29. showErrors: true,
  30. chunks: 'all',
  31. excludeChunks: [],
  32. chunksSortMode: 'none',
  33. meta: {},
  34. title: 'Webpack App',
  35. xhtml: false,
  36. alwaysWriteToDisk: true,
  37. },
  38. },
  39. DefinePlugin {
  40. definitions: {
  41. 'process.env': {
  42. NODE_ENV: '"development"',
  43. NODE_PATH: '""',
  44. PUBLIC_URL: '"."',
  45. '<storybook-environment-variables>'
  46. '<dotenv-environment-variables>'
  47. },
  48. },
  49. },
  50. WatchMissingNodeModulesPlugin {
  51. nodeModulesPath: './node_modules',
  52. },
  53. HotModuleReplacementPlugin {},
  54. CaseSensitivePathsPlugin {},
  55. ProgressPlugin {},
  56. DefinePlugin {
  57. definitions: {
  58. '<storybook-environment-variables>'
  59. '<dotenv-environment-variables>'
  60. },
  61. },
  62. ],
  63. module: {
  64. rules: [
  65. { test: /\.(mjs|jsx?)$/,
  66. use: [
  67. { loader: 'babel-loader', options:
  68. { cacheDirectory: './node_modules/.cache/storybook',
  69. presets: [
  70. [ './node_modules/@babel/preset-env/lib/index.js', { shippedProposals: true, useBuiltIns: 'usage', corejs: '3' } ],
  71. './node_modules/@babel/preset-react/lib/index.js',
  72. './node_modules/@babel/preset-flow/lib/index.js',
  73. ],
  74. plugins: [
  75. './node_modules/@babel/plugin-proposal-object-rest-spread/lib/index.js',
  76. './node_modules/@babel/plugin-proposal-class-properties/lib/index.js',
  77. './node_modules/@babel/plugin-syntax-dynamic-import/lib/index.js',
  78. [ './node_modules/babel-plugin-emotion/dist/babel-plugin-emotion.cjs.js', { sourceMap: true, autoLabel: true } ],
  79. './node_modules/babel-plugin-macros/dist/index.js',
  80. './node_modules/@babel/plugin-transform-react-constant-elements/lib/index.js',
  81. './node_modules/babel-plugin-add-react-displayname/index.js',
  82. [ './node_modules/babel-plugin-react-docgen/lib/index.js', { DOC_GEN_COLLECTION_NAME: 'STORYBOOK_REACT_CLASSES' } ],
  83. ],
  84. },
  85. },
  86. ],
  87. include: [ './' ],
  88. exclude: [ './node_modules' ],
  89. },
  90. { test: /\.md$/,
  91. use: [
  92. { loader: './node_modules/raw-loader/index.js' },
  93. ],
  94. },
  95. { test: /\.css$/,
  96. use: [
  97. './node_modules/style-loader/index.js',
  98. { loader: './node_modules/css-loader/dist/cjs.js', options: { importLoaders: 1 } },
  99. { loader: './node_modules/postcss-loader/src/index.js', options: { ident: 'postcss', postcss: {}, plugins: [Function: plugins] } },
  100. ],
  101. },
  102. { test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|cur|ani)(\?.*)?$/,
  103. loader: './node_modules/file-loader/dist/cjs.js',
  104. query: { name: 'static/media/[name].[hash:8].[ext]' },
  105. },
  106. { test: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/,
  107. loader: './node_modules/url-loader/dist/cjs.js',
  108. query: { limit: 10000, name: 'static/media/[name].[hash:8].[ext]' },
  109. },
  110. ],
  111. },
  112. resolve: {
  113. extensions: [ '.mjs', '.js', '.jsx', '.json' ],
  114. modules: [ 'node_modules' ],
  115. mainFields: [ 'browser', 'main', 'module' ],
  116. alias: {
  117. 'core-js': './node_modules/core-js',
  118. react: './node_modules/react',
  119. 'react-dom': './node_modules/react-dom',
  120. },
  121. },
  122. optimization: {
  123. splitChunks: { chunks: 'all' },
  124. runtimeChunk: true,
  125. minimizer: [ [Object] ],
  126. },
  127. performance: { hints: false },
  128. }

CSS Support

You can simply import CSS files wherever you want, whether it’s in the storybook config file, a UI component, or inside a story definition file.

Basically, you can import CSS like this:

  1. // from NPM modules
  2. import 'bootstrap/dist/css/bootstrap.css';
  3. // from local path
  4. import './styles.css';

Note: with some frameworks/clis we inject plain CSS only. If you need a preprocessor like SASS, you need to customize the webpack config.

Warning: storybooks for projects that use Angular CLI cannot import CSS by default. They must either customize the webpack config, or use the inline loader syntax:

  1. import '!style-loader!css-loader!./styles.css';

Image and Static File Support

You can also import images and media files directly via JavaScript.This helps you to write stories with media files easily. This is how to do it:

  1. import React from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import imageFile from './static/image.png';
  4. storiesOf('<img />', module)
  5. .add('with an image', () => (
  6. <img src={imageFile} alt="covfefe" />
  7. ));

When you are building a storybook, we’ll also export the imported image.So, this is a good approach to loading all of your static content.

Alternative: storybook also has a way to mention static directories via the -s option of the start-storybook and build-storybook commands. read more

JSON Loader

You can import .json files, as you do with Node.js.This will also allow you to use NPM projects that import .json files inside them.

  1. import React from 'react';
  2. import { storiesOf } from '@storybook/react';
  3. import data from './data.json';
  4. storiesOf('Component', module)
  5. .add('with data', () => (
  6. <pre>{JSON.stringify(data, null, 2)}</pre>
  7. ));

NPM Modules

You can use any of the NPM modules installed on your project.You can simply import and use them.