Serving static files in "storybook js"

Vano picture Vano · Oct 7, 2019 · Viewed 8.3k times · Source

I am using storybook documentation and couldn't load images from assets folder. As documentations says: "if you are using a custom Webpack config, you need to add the file-loader into your custom Webpack config" - and my webpack.config file looks like:

const path = require('path');


 module.exports = ({ config }) => {
    config.module.rules.push({
      test: /\.scss$/,
      use: ['style-loader', 'css-loader', 'sass-loader']
    });
 config.module.rules.push({
   test: /\.(ts|tsx)$/,
   use: [
     {
       loader: require.resolve('awesome-typescript-loader'),
     },
   ],
 });
 config.module.rules.push({
   test: /\.(svg|png|jpe?g|gif)$/i,
   use: [
     {
       loader: 'file-loader',
     },
    ]
  },);

  config.resolve.extensions.push('.ts', '.tsx')


  return config;
};

package.json:

"react": "^16.10.1",
"react-dom": "^16.10.1",
"typescript": "^3.6.3",
"@storybook/react": "^5.2.1",
"@types/storybook__react": "^4.0.2",
"file-loader": "^4.2.0"

This is after yarn storybook webpack-debug

seems like something is missing from storybooks documentation, or I am doing something wrong :? thank you whoever can help me out with this problem. ^_^

Answer

Aakash picture Aakash · Nov 23, 2019

Configuring the static folder in the storybook-start script worked for me:

Documentation: Static Files Via Directory

You can also configure a directory (or a list of directories) for searching static content when you are starting Storybook. You can do that with the -s flag.

//package.json
{
"scripts": {
    "start-storybook": "start-storybook -s ./public -p 9001"
  }
}

Good Luck...