๐Ÿค ์€์ง€log ๐Ÿค

[NextJS] NextJS์—์„œ ์ปดํฌ๋„ŒํŠธํ˜•์‹์œผ๋กœ SVG์‚ฌ์šฉํ•˜๊ธฐ ๋ณธ๋ฌธ

๐ŸŒท Next JS

[NextJS] NextJS์—์„œ ์ปดํฌ๋„ŒํŠธํ˜•์‹์œผ๋กœ SVG์‚ฌ์šฉํ•˜๊ธฐ

Eun_zii 2022. 12. 5. 13:44
npm install -D @svgr/webpack

 

webpack์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

// next.config.js ์„ค์ •

module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\\.svg$/,
      use: ["@svgr/webpack"],
    });

    return config;
  },
};

 

next.js ํ”„๋กœ์ ํŠธ๋ฅผ ์ฒ˜์Œ ์ƒ์„ฑ์‹œ ๊ฐ™์ด ์ƒ์„ฑ๋˜๋Š” next.config.js๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

// ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”์— custom.d.ts ํŒŒ์ผ ์ƒ์„ฑ

declare module "*.svg" {
  import React from "react";
  const svg: React.FC<React.SVGProps<SVGSVGElement>>;
  export default svg;
}

 

ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ํด๋”์— custom.d.ts ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 

// tsconfig.json ์ˆ˜์ •

"include": ["custom.d.ts", "next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]

 

next.js ํ”„๋กœ์ ํŠธ๋ฅผ ์ฒ˜์Œ ์ƒ์„ฑ์‹œ ๊ฐ™์ด ์ƒ์„ฑ๋˜๋Š” tsconfig.json์„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

์‚ฌ์šฉํ•˜๊ธฐ

import Logo from "assets/icons/header/logo.svg";

<Logo/>
728x90