Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook webpack not configured for Create React App 2.0 Inline SVG's #4298

Closed
semb09 opened this issue Oct 5, 2018 · 16 comments
Closed

Storybook webpack not configured for Create React App 2.0 Inline SVG's #4298

semb09 opened this issue Oct 5, 2018 · 16 comments
Labels

Comments

@semb09
Copy link

semb09 commented Oct 5, 2018

If you are reporting a bug or requesting support, start here:

Bug or support request summary

I recently upgraded to Create React App 2.0 so that I could use the new SVG import feature

import { ReactComponent as Icon } from 'icon.svg';

I then realised that this is provided via react-scripts webpack config and that Storybook has its own config. I then updated to the latest alpha version of Storybook but it still did not work.

I managed to get the feature to work by adding the following to storybook webpack config file

const path = require('path');
const fs = require('fs');
const appDirectory = fs.realpathSync(process.cwd());

module.exports = (baseConfig, env, defaultConfig) => {
  // Extend defaultConfig as you need.

  defaultConfig.module.rules.push({
    test: /\.(js|jsx)$/,
    include: path.resolve(appDirectory, 'src'),
    loader: require.resolve('babel-loader'),
    options: {
      presets: [require.resolve('babel-preset-react-app')],
      plugins: [
        [
          require.resolve('babel-plugin-named-asset-import'),
          {
            loaderMap: {
              svg: {
                ReactComponent: '@svgr/webpack?-prettier,-svgo![path]',
              },
            },
          },
        ],
      ]
    }
  });

  return defaultConfig;
};

Will this config be included in future Storybook updates?

Steps to reproduce

  • Create React App 2.0
  • Add Storybook
  • Use import { ReactComponent as Icon } from 'icon.svg'; syntax inside any component in a story

(A screencast can be useful for visual bugs, but it is not a substitute for a textual description.)

Please specify which version of Storybook and optionally any affected addons that you're running

  • @storybook/react ^4.0.0-alpha.24
@HerbCaudill
Copy link

+1

I ran into this as well, and @semb09's webpack config solved the problem.

For anyone else coming across this, I also needed to add the referenced packages to my vanilla CRA+Storybook app:

yarn add -D babel-loader, babel-preset-react-app, babel-plugin-named-asset-import, @svgr/webpack

@igor-dv
Copy link
Member

igor-dv commented Oct 7, 2018

I can relate it to #4306 as well.

IMO, we need to support as much as possible for CRA2 compatibility.

Does anyone want to take this as a PR?

@Itrulia
Copy link

Itrulia commented Nov 16, 2018

So I am trying to get this to work with typescript.

// webpack.config.js

module.exports = (storybookBaseConfig, configType) => {
  storybookBaseConfig.resolve.extensions.push(".ts", ".tsx");

  storybookBaseConfig.module.rules[0].test = /\.(mjs|jsx?|tsx?)$/;
  storybookBaseConfig.module.rules[0].use[0].options = require("./babel.config");

  return storybookBaseConfig;
};
// babel.config.js
module.exports = {
  presets: [
    require.resolve("babel-preset-react-app"),
  ],
  plugins: [
    [
      require.resolve("babel-plugin-named-asset-import"),
      {
        loaderMap: {
          svg: {
            ReactComponent: "@svgr/webpack?-prettier,-svgo![path]",
          },
        },
      },
    ]
  ]
};

This will result in a warning like this

WARNING in ./src/icons/social/youtube/youtube.story.tsx 6:29-36
"export 'ReactComponent' (imported as 'Youtube') was not found in '@svgr/webpack?-prettier,-svgo!./youtube.svg'
 @ ./src sync .story.tsx$
 @ ./config/storybook/config.tsx
 @ multi ./node_modules/@storybook/core/dist/server/config/polyfills.js ./node_modules/@storybook/core/dist/server/config/globals.js ./config/storybook/config.tsx ./node_modules/webpack-hot-middleware/client.js?reload=true

import is: import {ReactComponent as Youtube} from "./youtube.svg";

What am I missing?

@shilman
Copy link
Member

shilman commented Nov 25, 2018

@shilman shilman closed this as completed Nov 25, 2018
@issue-sh issue-sh bot removed the merged label Nov 25, 2018
@Itrulia
Copy link

Itrulia commented Nov 28, 2018

This does not work for me. Still getting the same warning/error:

WARNING in ./src/icons/social/youtube/youtube.story.tsx 6:29-36
"export 'ReactComponent' (imported as 'Youtube') was not found in '@svgr/webpack?-prettier,-svgo!./youtube.svg'
 @ ./src sync .story.tsx$
 @ ./config/storybook/config.tsx
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./config/storybook/config.tsx ./node_modules/webpack-hot-middleware/client.js?reload=true

this is the story:

import React from "react";
import { IconStories } from "../../icons.story";
import { ReactComponent as Youtube } from "./youtube.svg";

IconStories.add("Youtube", () => <Youtube />);

@igor-dv
Copy link
Member

igor-dv commented Nov 28, 2018

Do you have a reproduction?

@Itrulia
Copy link

Itrulia commented Nov 29, 2018

Unfortunately was not able to reproduce it. But thats great.
I will investigate and let you know what caused it.

@Itrulia
Copy link

Itrulia commented Nov 29, 2018

Okay, so I was able to reproduce it by having:

//webpack.config.js
module.exports = (storybookBaseConfig, configType) => {

  return storybookBaseConfig;
};

In the config directory.

Repo: https://github.com/Itrulia/storybook-svg-import-problem

@igor-dv
Copy link
Member

igor-dv commented Dec 1, 2018

@Itrulia do you use CRA2? I wonder why would you create a custom webpack config, since we support now TS and svgr out of the box for CRA2 users.

@Itrulia
Copy link

Itrulia commented Dec 3, 2018

Yes, I am.

Because I need more than CRA gives me so I override the config (I don't eject, override it ala https://github.com/timarney/react-app-rewired). Atleast with #4902 being merged I don't need to override it for Storybook.

@igor-dv
Copy link
Member

igor-dv commented Dec 3, 2018

Do you have a react-scripts somewhere in your node_modules ? We are applying rules from CRA when this package is listed.

@Itrulia
Copy link

Itrulia commented Dec 4, 2018

Yes, you can checkout the repo I linked earlier.

@igor-dv
Copy link
Member

igor-dv commented Dec 4, 2018

Ok, so the problem is that you should use defaultConfig and not basicConfig:

module.exports = (storybookBaseConfig, configType, defaultConfig) => {
    return defaultConfig;
};

default config has a rulse for svg, that is needed for SVGR later.

@Itrulia
Copy link

Itrulia commented Dec 5, 2018

Oh... This works great!

Thanks and sorry for wasting your time!

@igor-dv
Copy link
Member

igor-dv commented Dec 5, 2018

Linking the #4903 here.

@shirbr510
Copy link

shirbr510 commented Dec 19, 2018

I'm currently getting an error regarding my inline svgs from storybook with CRA 2.0.5 and don't have an exposed webpack.config.

which version of storybook is this supported from?

update

nvm, I was using storybook 4.0.2 and now after upgrading to 4.1.2 it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants