Skip to content

Commit

Permalink
Default SVGR config
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Jan 25, 2025
1 parent 55a9631 commit 412aa2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
27 changes: 23 additions & 4 deletions next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@ import type { NextConfig } from "next";
const nextConfig: NextConfig = {
reactStrictMode: true,
webpack(config) {
config.module.rules.push({
test: /\.svg$/i,
use: ['@svgr/webpack']
});
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) =>
rule.test?.test?.('.svg'),
);

config.module.rules.push(
// Reapply the existing rule, but only for svg imports ending in ?url
{
...fileLoaderRule,
test: /\.svg$/i,
resourceQuery: /url/, // *.svg?url
},
// Convert all other *.svg imports to React components
{
test: /\.svg$/i,
issuer: fileLoaderRule.issuer,
resourceQuery: {not: [...fileLoaderRule.resourceQuery.not, /url/]}, // exclude if *.svg?url
use: ['@svgr/webpack'],
}
);
// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;

return config;
}
};
Expand Down
7 changes: 6 additions & 1 deletion svgr.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
declare module '*.svg' {
import { FC, SVGProps } from "react";
import { FC, SVGProps } from 'react';
const content: FC<SVGProps<SVGElement>>;
export default content;
}

declare module '*.svg?url' {
const content: any;
export default content;
}

0 comments on commit 412aa2a

Please sign in to comment.