Detalhes do pacote

next-plugin-svgr

platypusrex90.3kMIT1.1.12

Next js plugin for SVGR

next.js, svg, svgr, webpack

readme (leia-me)

Next.js + SVGR

npm package Dependencies License

Flexible Next.js plugin for transforming svg's into react components using the svgr library

Installation

npm

npm install --save next-plugin-svgr

or yarn

yarn add next-plugin-svgr

Usage

Create a next.config.js in your project

// next.config.js
const withSvgr = require('next-plugin-svgr');

module.exports = withSvgr();

Optionally add Next.js configuration as a parameter

// next.config.js
const withSvgr = require('next-plugin-svgr');

module.exports = withSvgr({
  webpack(config, options) {
    return config;
  },
});

Or with next-compose-plugins when composing multiple plugins

// next.config.js
const withPlugins = require("next-compose-plugins");
const withSvgr = require("next-plugin-svgr");

module.exports = withPlugins([
  withSvgr
  // your other plugins here
]);

And now in your components you can use the svg as a component

import Icon from './icon.svg';

export default () => (
  <div>
    <Icon />
  </div>
);

Options

svgrOptions

The plugins supports all available options of svgr webpack loader. Check out the svgr documentation for the full list of options.

Example with options:

module.exports = withSvgr({
  svgrOptions: {
    titleProp: true,
    icon: true,
    svgProps: {
      height: 'auto',
    },
  },
});

You can optionally specify a project configuration file. SVGR uses cosmiconfig for configuration file support, which means that any file type accepted by cosmicconfig is supported.

  • .svgrrc file, written in YAML or JSON, with optional extensions: .yaml/.yml/.json/.js.
  • A svgr.config.js file that exports an object.
  • A "svgr" key in your package.json file.

note: The plugin will automatically detect your config file so you shouldn't have to include the configFile property in svgrOptions. The option to specify exists and can be accomplished following the example below.

// .svgrrc.js
module.exports = {
  icon: true,
  expandProps: false,
};

// next.config.js
module.exports = withSvgr({
  svgrOptions: {
    configFile: path.resolve(__dirname, '.svgrrc.js'),
  },
});

// or with next-compose-plugins
module.exports = withPlugins([
  withGraphql,
  [withSvgr, {
    svgrOptions: {
      configFile: path.resolve(__dirname, '.svgrrc.js'),
    },
  }],
]);

fileLoader

If you would like to use the svgr webpack loader with file-loader. Accepts a boolean or all available options for file-loader.

The fileLoader option is undefined by default. If defined, it will apply the options below.

note: If using file-loader and typescript remember to reference the svgr/file-loader types. See below.

Default options:

{
  limit: 8192,
  publicPath: `${assetPrefix ?? ''}/_next/${path}`,
  outputPath: `${isServer ? '../' : ''}${path}`,
  name: '[path][name].[hash].[ext]',
}
module.exports = withSvgr({
  fileLoader: true,
  svgrOptions: {
    ...options
  },
});
module.exports = withSvgr({
  fileLoader: {
    limit: 16384,
    name(resourcePath, resourceQuery) {
      if (process.env.NODE_ENV === 'development') {
        return '[path][name].[ext]';
      } 
      return '[contenthash].[ext]';
    }
  },
  svgrOptions: {
    ...options
  },
});
import url, { ReactComponent as Icon } from './icon.svg';

export default () => (
  <div>
    <Icon title="my awesome icon" />
    <img src={url} alt="my awesome image" />
  </div>
);

Typescript

Typescript is unable to interpret imported svg files, so next-plugin-svgr includes definitions for svg modules depending on your use case. Per the recommendations of the Next.js maintainers you should no longer reference these types in the next-env.d.ts file. You can instead create a typings directory inside your src directory. Then simple create a definitions file (ie: index.d.ts) and reference any of the definitions there. There shouldn't be any need to adjust your tsconfig.json for your project.

  1. if using the plugin without the fileLoader option

src/typings/index.d.ts

/// <reference types="next-plugin-svgr/types/svg" />
  1. If using the plugin with the fileLoader option

src/typings/index.d.ts

/// <reference types="next-plugin-svgr/types/svgFileLoader" />

Contributors

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

changelog (log de mudanças)

Changelog

1.1.12

Patch Changes

  • 1472d3d: build(deps-dev): bump braces from 3.0.2 to 3.0.3

1.1.11

Patch Changes

  • 0a4e537: Fixes issues with release worflow.
  • 61726c4: Updates the example application to the latest version of next.js (v15). Eslint migrated to v9 and configuration files updated. Husky and commitlint also migrated to latest versions and config files updated.

1.1.10

Patch Changes

  • 57869a7: Addresses an issue where the cache-control headers are not being set correctly for SVG assets, by @better-salmon (See #64)

1.1.9

Patch Changes

  • 3327a40: build(deps): bump @babel/traverse from 7.22.8 to 7.23.2 Also bumps any relevant core and dev dependencies.

1.1.8

Patch Changes

  • 428603f: Migrates from yarn v1 to pnpm for managing dependencies. Also updates dev dependencies.

1.1.7

Patch Changes

  • 9307d78: build(deps): bump word-wrap from 1.2.3 to 1.2.4

1.1.6

Patch Changes

  • 38c7026: build(deps): bump semver from 5.7.1 to 5.7.2

1.1.5

Patch Changes

  • d6c1ab3: Updating core and dev dependencies. Replacing standard-version (deprecated) with changesets for release automation.

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.1.4 (2023-04-08)

Bug Fixes

  • missing types in published npm package (a9b9015)

1.1.3 (2022-11-22)

1.1.2 (2022-07-25)

Bug Fixes

  • remove svgr properties from next.config (80300f5)

1.1.1 (2022-01-10)

1.1.0 (2022-01-10)

Features

  • add optional support for file-loader and ts svg module defs (0533577)
  • adds proper example app using next v12 (683677a)

Bug Fixes

  • config file takes priority over individual configs (a87db9f)
  • fixes any outstanding issues with next v12 and webpack/file-loader (0e1522b)
  • update deps, adds deps/configs to improve build/ci/release process (d3da39a)