Skip to content

Commit

Permalink
feat: add svgr support
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed Jul 17, 2023
1 parent a8abb82 commit adcaa5e
Show file tree
Hide file tree
Showing 5 changed files with 1,468 additions and 9 deletions.
32 changes: 32 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ const nextConfig = {
// 'res.cloudinary.com',
// ],
// },

webpack(config) {
// 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: { not: /\.(css|scss|sass)$/ },
resourceQuery: { not: /url/ }, // exclude if *.svg?url
loader: '@svgr/webpack',
options: {
dimensions: false,
titleProp: true,
},
}
);

// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;

return config;
},
};

module.exports = nextConfig;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@commitlint/cli": "^16.3.0",
"@commitlint/config-conventional": "^16.2.4",
"@tailwindcss/forms": "^0.5.4",
"@svgr/webpack": "^8.0.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/react": "^18.2.15",
Expand Down
1 change: 1 addition & 0 deletions public/svg/Logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import ButtonLink from '@/components/links/ButtonLink';
import UnderlineLink from '@/components/links/UnderlineLink';
import UnstyledLink from '@/components/links/UnstyledLink';

/**
* SVGR Support
* Caveat: No React Props Type.
*
* You can override the next-env if the type is important to you
* @see https://stackoverflow.com/questions/68103844/how-to-override-next-js-svg-module-declaration
*/
import Logo from '~/svg/Logo.svg';

// !STARTERCONF -> Select !STARTERCONF and CMD + SHIFT + F
// Before you begin editing, follow all comments with `STARTERCONF`,
// to customize the default configuration.
Expand All @@ -20,6 +29,7 @@ export default function HomePage() {
</Head>
<section className='bg-white'>
<div className='layout relative flex min-h-screen flex-col items-center justify-center py-12 text-center'>
<Logo className='w-16' />
<h1 className='mt-4'>Next.js + Tailwind CSS + TypeScript Starter</h1>
<p className='mt-2 text-sm text-gray-800'>
A starter for Next.js, Tailwind CSS, and TypeScript with Absolute
Expand Down
Loading

1 comment on commit adcaa5e

@vercel
Copy link

@vercel vercel bot commented on adcaa5e Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.