Giter VIP home page Giter VIP logo

razzle-plugin-svg-react-component's Introduction

Razzle Plugin SVG as ReactComponent

This package contains a plugin to import SVG as ReactComponent.

Notes: This plugin is only compatible with Razzle v3.3.0 and above. If your project using an older version of Razzle, you could install the 1.0.3 version of this plugin. See below explanation for an older version of Razzle.

Usage in Razzle Projects

  • Install razzle-plugin-svg-react-component and @svgr/webpack

    if you're using yarn

    yarn add razzle-plugin-svg-react-component @svgr/webpack
    

    if you're using npm

    npm install razzle-plugin-svg-react-component @svgr/webpack --save
    
  • create a razzle.config.js file in root directory of project (next to the package.json) and put this content inside it

    // razzle.config.js
    module.exports = {
      plugins: [
        'svg-react-component'
      ],
    };
    

Example

By default you can use SVG like below :

import Logo from './logo.svg';

const App = () => (
  <div>
    <img src={Logo} alt="Here are my logo" />
  </div>
);

With this plugin, You can also import SVGs directly as React components.

import { ReactComponent as Logo } from './logo.svg';

const App = () => (
  <div>
    {/* Logo is an actual React component */}
    <Logo />
  </div>
);

Usage with Typescript

If you encounter this error message, Module '"*.svg"' has no exported member 'ReactComponent'.

You need to add declaration type for SVG to your declarations.d.ts like below:

declare module '*.svg' {
  import * as React from 'react';

  export const ReactComponent: React.FunctionComponent<React.SVGProps<
    SVGSVGElement
  > & { title?: string }>;

  const src: string;
  export default src;
}

Notes: If your project doesn't have declarations.d.ts file, you could create a new one inside src folder.

Older version Razzle (below v3.3.0)

Usage in Razzle Projects

  • Install [email protected]

    if you're using yarn

    if you're using npm

    npm install [email protected] --save
    
  • create a razzle.config.js file in root directory of project (next to the package.json) and put this content inside it

    // razzle.config.js
    module.exports = {
      plugins: [
        'svg-react-component'
      ],
    };
    

Usage with razzle-plugin-typescript

  • specify useBabel: true option in typescript plugin

    // razzle.config.js
    module.exports = {
      plugins: [
        { name: "typescript", options: { useBabel: true } },
        'svg-react-component',
      ],
    };
    
  • use target ES6 or later

    // tsconfig.json
    {
      "compilerOptions": {
        "target": "ES6",
        "moduleResolution": "Node",
        ...
      }
    }
    

Under the hood, this plugin uses babel-plugin-named-asset-import. By default, razzle-plugin-typescript does not includes babel-loader for .tsx files. To include babel-loader, useBabel: true has to be specified.

babel-plugin-named-asset-import only transforms ES2015 static import and export statements and not require() calls. If target is ES5 or below, static import statements is transformed to require calls. This prevents transformation of babel-plugin-named-asset-import.

razzle-plugin-svg-react-component's People

Contributors

chrisdostert avatar dependabot-preview[bot] avatar finmavis avatar hibikiledo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

razzle-plugin-svg-react-component's Issues

Error compiling

output

WAIT  Compiling...

TypeError: Cannot read property 'push' of undefined
    at modify (/home/juanpablocs/Projects/razzle/with-scss/node_modules/razzle-plugin-svg-react-component/index.js:11:61)

Cannot find module '@svgr/webpack?-svgo,+titleProp,+ref!../../assets/svgs/AppleDownload.svg'

Been trying to debug this for the past few days but can't figure it out. We are using the latest versions of Razzle and Razzle dev utils (4.0.4). It was working correctly before we upgraded razzle. Not sure what is causing this issue, maybe you can help. We are using the 2.0.0 version of this package and 5.5.0 of @svgr/webpack.

import { ReactComponent as Apple } from "../../assets/svgs/AppleDownload.svg";
<Apple />
Cannot find module "@svgr/webpack?-svgo,+titleProp,+ref!../../assets/svgs/AppleDownload.svg"

Razzle 4.0.4
Razzle-dev-utils 4.0.4
webpack 4.46.0
@svgr/webpack 5.5.0
razzle-plugin-svg-react-component 2.0.0

Thanks!

SVGs are not rendered server-side

Details

SVG files are rendering on the client-side hydration, but not the server-side render. This results in a brief flicker of content popping onto the page, whereas it should just always be there starting from the first paint.

razzle.config.js

module.exports = {
  plugins: [
    'svg-react-component',
    {
      name: 'typescript',
      options: {
        forkTsChecker: {
          tsconfig: './tsconfig.json',
          tslint: './tslint.json',
          watch: './src',
          typeCheck: true
        }
      }
    }
  ]
};

React Component

import React from 'react';

import smGhostsDark from 'client/assets/svg/empty-ghosts-sm--dark.svg';

export const EmptyState = () => {
  return <img src={smGhostsDark} />;
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.