Default Config
Storybook has a default Webpack setup which suits a large range of projects. It is similar to Create React App’s config when using react, and has been tweaked to closely resemble the webpack config generated by Vue CLI.
Let’s learn about the default config comes with Storybook.
Table of Contents
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. We’ve added some Webpack loaders to bring some good defaults. (This setup is very close to what you get with the Create React App.)
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:
// from NPM modules
import 'bootstrap/dist/css/bootstrap.css';
// from local path
import './styles.css';
Note: this is 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:
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:
import React from 'react';
import { storiesOf } from '@storybook/react';
import imageFile from './static/image.png';
storiesOf('<img />', module)
.add('with a image', () => (
<img src={imageFile} alt="covfefe" />
));
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 thestart-storybook
andbuild-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, which imports .json
files inside them.
NPM Modules
You can use any of the NPM modules installed on your project. You can simply import and use them.