react-native-svg with storybook

69 Views Asked by At

I have installed a library for displaying icons which internally uses react-native-svg. I maintain this library and it has created the files using @svgr/cli.

I'm trying to start storybook and I get errors on all of those library files

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import Svg, { Rect, Path } from "react-native-svg"; 

My main.js

const custom = require("../webpack.config.js");

module.exports = {
  webpackFinal: config => {
    return {
      ...config,
      resolve: { alias: { ...config.resolve.alias, ...custom.resolve.alias } },
      module: { ...config.module, rules: custom.module.rules },
    };
  },
  stories: ["../storybook/**/*.stories.?(ts|tsx|js|jsx)"],
  addons: [
    "@storybook/addon-ondevice-notes",
    "@storybook/addon-ondevice-controls",
    "@storybook/addon-ondevice-backgrounds",
    "@storybook/addon-ondevice-actions",
  ],
};

webpack.config.js

const path = require("path");

const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");

const appDirectory = path.resolve(__dirname);

const babelLoaderConfiguration = {
  test: /\.js/,
  include: [
    path.resolve(__dirname, "index.web.js"),
    path.resolve(__dirname, "App.web.js"),
    path.resolve(__dirname, "src"),
    path.resolve(__dirname, "./storybook/stories"),
    path.resolve(__dirname, "./.storybook/preview.js"),
    path.resolve(__dirname, "./node_modules/react-native-modal-selector"),
    path.resolve(__dirname, "./node_modules/react-native-swipe-gestures"),
    path.resolve(__dirname, "./node_modules/react-native-modal"),
    path.resolve(__dirname, "./node_modules/react-native-animatable"),
    path.resolve(__dirname, "./node_modules/react-native-reanimated"),
    path.resolve(__dirname, "./node_modules/react-native-gesture-handler"),
    path.resolve(__dirname, "./node_modules/react-native-remix-icon"),
    path.resolve(__dirname, "./node_modules/react-native-svg"),
    path.resolve(__dirname, "./node_modules/react-native-svg-transformer"),
    path.resolve(__dirname, "./node_modules/react-native-toast-message"),
    path.resolve(
      __dirname,
      "./node_modules/react-native-keyboard-aware-scroll-view"
    ),
    path.resolve(__dirname, "./node_modules/react-native-pell-rich-editor"),
    path.resolve(__dirname, "./node_modules/react-native-webview"),
    path.resolve(__dirname, "./node_modules/react-native-calendars"),
  ],
  use: {
    loader: "babel-loader",
    options: {
      cacheDirectory: true,
      presets: [
        "@babel/env",
        "@babel/preset-react",
        "module:metro-react-native-babel-preset",
      ],
      plugins: ["react-native-web", "@babel/plugin-proposal-class-properties"],
    },
  },
};

const ttfLoaderConfiguration = {
  test: /\.ttf$/,
  use: {
    loader: "url-loader",
  },
  include: [path.resolve(appDirectory, "./assets")],
};

const svgLoaderConfiguration = {
  test: /\.svg$/,
  use: [
    {
      loader: "@svgr/webpack",
    },
  ],
};

const imageLoaderConfiguration = {
  test: /\.(gif|jpe?g|png)$/,
  use: {
    loader: "url-loader",
    options: {
      name: "[name].[ext]",
    },
  },
};

module.exports = {
  entry: {
    app: path.join(__dirname, "index.web.js"),
  },
  output: {
    path: path.resolve(appDirectory, "dist"),
    publicPath: "/",
    filename: "rnw.bundle.js",
  },
  resolve: {
    extensions: [".web.tsx", ".web.ts", ".tsx", ".ts", ".web.js", ".js"],
    alias: {
      "react-native$": "react-native-web",
    },
  },
  module: {
    rules: [
      babelLoaderConfiguration,
      imageLoaderConfiguration,
      svgLoaderConfiguration,
      ttfLoaderConfiguration,
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, "index.html"),
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      __DEV__: JSON.stringify(true),
    }),
    new webpack.DefinePlugin({ process: { env: {} } }),
  ],
  devServer: {
    open: true,
  },
};

How can I make the react-native-web to display the svg from storybook?

0

There are 0 best solutions below