λ“€μ–΄κ°€λ©°πŸ˜ƒ


ν”„λ‘œμ νŠΈμ—μ„œ 차트λ₯Ό λ³΄μ—¬μ£ΌλŠ” νŽ˜μ΄μ§€λ₯Ό λ‚΄κ°€ λ‹΄λ‹Ήν•˜κ²Œ λ˜μ—ˆλ‹€. λ‹€μ–‘ν•œ λΌμ΄λΈŒλŸ¬λ¦¬κ°€ 후보ꡰ에 μžˆμ—ˆμ§€λ§Œ ν‘œν˜„ν•΄μ•Όν–ˆλ˜ κ·Έλž˜ν”„ μ’…λ₯˜λ₯Ό λͺ¨λ‘ ν¬ν•¨ν•˜κ³  μžˆμ—ˆκ³  κ°€μž₯ 정보λ₯Ό 많이 얻을 수 μžˆμ—ˆλ˜ echart 라이브러리λ₯Ό μ±„νƒν•˜μ˜€λ‹€. ν•΄λ‹Ή 라이브러리λ₯Ό μ‚¬μš©ν•˜λ©΄μ„œ νŠΈλ¦¬μ‰μ΄ν‚Ή κ΄€λ ¨ λ¬Έμ„œλ₯Ό 많이 μ ‘ν•  수 μžˆμ—ˆλŠ”λ°, μ΄λŠ” echart 곡식 λ¬Έμ„œμ—μ„œλ„ tree shaking APIλ₯Ό μ‚¬μš©ν•˜λΌκ³ λ„ λ‚˜μ™€μžˆμ—ˆλ‹€. μ—¬νŠΌ νŠΈλ¦¬μ‰μ΄ν‚Ή κ΄€λ ¨ 정보λ₯Ό 찾아보며 λ‹€λ₯Έ 뢀가적인 정보듀도 μ•Œκ²Œλ˜μ–΄ μ •λ¦¬ν•˜κ³ μž ν•˜λŠ” 였늘의 ν¬μŠ€νŒ…μ΄λ‹€.πŸƒ



1. Tree Shaking μ΄λž€?


Tree shaking은 JavaScript의 λͺ¨λ“ˆ λ²ˆλ“€λ§ λ„κ΅¬μ—μ„œ μ‚¬μš©λ˜λŠ” μ΅œμ ν™” 기법 쀑 ν•˜λ‚˜λ‘œ, λ²ˆλ“€λ§ν•  λ•Œ μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μ½”λ“œλ₯Ό μ œκ±°ν•˜μ—¬ λ²ˆλ“€ 크기λ₯Ό μ΅œμ†Œν™”ν•œλ‹€.

*λ²ˆλ“€λ§: μ—¬λŸ¬κ°œμ˜ νŒŒμΌμ„ ν•©μΉ˜λŠ” κ³Όμ •

일반적으둜 JavaScript ν”„λ‘œμ νŠΈμ—μ„œλŠ” μ—¬λŸ¬ 개의 λͺ¨λ“ˆμ„ μ‚¬μš©ν•˜κ²Œ λ˜λŠ”λ°, 이 λͺ¨λ“ˆλ“€ μ€‘μ—μ„œ μ‹€μ œ ν”„λ‘œμ νŠΈμ— μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μ½”λ“œλ“€μ΄ μ‘΄μž¬ν•  수 μžˆλ‹€. 이런 경우 λ²ˆλ“€λ§ν•  λ•Œ ν•΄λ‹Ήλ˜μ§€ μ•ŠλŠ” μ½”λ“œκΉŒμ§€ λͺ¨λ‘ ν¬ν•¨ν•˜μ—¬ λ²ˆλ“€ 크기가 μ»€μ§€κ²Œ λ˜λŠ” 것이닀.

κ·Έλž˜μ„œ Tree shaking 과정을 톡해 λͺ¨λ“ˆ κ°„μ˜ 의쑴 관계λ₯Ό λΆ„μ„ν•˜μ—¬ μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μ½”λ“œλ₯Ό μ‹λ³„ν•˜κ³  μ œκ±°ν•˜μ—¬ μ΅œμ’… λ²ˆλ“€μ—λŠ” ν”„λ‘œμ νŠΈμ— μ‹€μ œλ‘œ μ‚¬μš©λ˜λŠ” μ½”λ“œλ§Œ ν¬ν•¨λ˜λ‘œλ… ν•¨μœΌλ‘œμ¨ ν”„λ‘œμ νŠΈμ˜ μ„±λŠ₯을 높일 수 μžˆλŠ” 것이닀.

2. Echart Tree Shaking API


κ°€μž₯ 기본적으둜 Echart λ₯Ό μ‚¬μš©ν•˜λŠ” 방법은 μ•„λž˜μ™€ κ°™λ‹€.

import React from "react";
import type { EChartsOption } from "echarts";
import ReactECharts from "echarts-for-react";

const EchartPage = () => {
  const chartRef = React.useRef(null);

  const options: EChartsOption = {
    xAxis: {
      type: "category",
      data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    },
    yAxis: {
      type: "value",
    },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: "line",
      },
    ],
  };

  return (
    <ReactECharts
      ref={chartRef}
      option={options}
      opts=
      style=
    />
  );
};

export default EchartPage;

μœ„μ˜ κ΅¬μ‘°λŠ” echarts-for-react μ—μ„œ μ œκ³΅ν•΄μ£ΌλŠ” λͺ¨λ“ˆλ“€μ„ λͺ¨μ‘°λ¦¬ 뢈러였고 μžˆλ‹€. 이제 μ—¬κΈ°μ„œ λ‚΄κ°€ ν”„λ‘œμ νŠΈμ— μ“°λŠ” κ·Έλž˜ν”„μ™€ μ†μ„±λ“€λ§Œ λΆˆλŸ¬μ˜€λ„λ‘ 변경해보겠닀.

import {
  BarChart,
  LineChart,
  ParallelChart,
  ScatterChart,
} from "echarts/charts";
import {
  GridComponent,
  LegendComponent,
  LegendPlainComponent,
  MarkPointComponent,
  TooltipComponent,
} from "echarts/components";
import * as echarts from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import type { EChartsOption } from "echarts";
import EChartsReactCore from "echarts-for-react/lib/core";
import "echarts/lib/component/markPoint";

echarts.use([
  LineChart,
  CanvasRenderer,
  GridComponent,
  MarkPointComponent,
  TooltipComponent,
  LegendComponent,
  ParallelChart,
  BarChart,
  ScatterChart,
  LegendPlainComponent,
]);

const EchartTreeShaking = () => {
  const options: EChartsOption = {
    xAxis: {
      type: "category",
      data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    },
    yAxis: {
      type: "value",
    },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: "line",
      },
    ],
  };
  return (
    <EChartsReactCore
      echarts={echarts}
      option={options}
      theme="space-theme"
      style=
      notMerge={true}
    />
  );
};

export default EchartTreeShaking;

λ‘œμ§μ€ κ½€ κ°„λ‹¨ν•˜λ‹€. ν•„μš”ν•œ λͺ¨λ“ˆμ„ λΆˆλŸ¬λ‚΄μ„œ echarts.use에 μ„€μ •λ§Œ ν•΄μ£Όλ©΄ 끝!

μœ„λŠ” λ‚΄κ°€ ν”„λ‘œμ νŠΈμ—μ„œ μ‹€μ œλ‘œ μ μš©ν–ˆλ˜ μ½”λ“œμ΄λ‹€. λΆˆλŸ¬μ˜€λŠ”κ²Œ λ§Žμ•„λ³΄μ΄κΈ΄ ν•˜λ‚˜ κ²°κ³Όμ μœΌλ‘œλŠ” 이전 μ½”λ“œλ³΄λ‹€ 훨씬 크기가 μž‘λ‹€λŠ” 것…!🫒

Echart 곡식 λ¬Έμ„œμ— λ”°λ₯΄λ©΄ 5버전뢀터 μƒˆλ‘œμš΄ tree shaking API λ₯Ό λ„μž…ν•˜μ˜€λ‹€κ³  ν•œλ‹€. μ˜΅μ…˜μ— 따라 μ–΄λ–€ λͺ¨λ“ˆμ„ 가져와야 ν•˜λŠ”μ§€ 더 μ‰½κ²Œ μ•Œ 수 μžˆλ„λ‘ importλ₯Ό ν•  수 μžˆλ‹€ν•˜λŠ”λ° μ‹€μ œλ‘œ μ°¨νŠΈλŠ” μ°¨νŠΈλŒ€λ‘œ, 속성은 μ†μ„±λŒ€λ‘œ λΆˆλŸ¬μ˜€λŠ” ꡬ쑰라 μ½”λ“œλ₯Ό μ»€μŠ€ν…€ν•˜κΈ° μš©μ΄ν–ˆλ‹€.

μ•„λ‹ˆ, κ·Έλž˜μ„œ 크기가 μ€„μ—ˆλŠ”μ§€ μ–΄λ–»κ²Œ 확인할 수 μžˆμ§€?πŸ€·πŸ»β€β™€οΈ



3. λ²ˆλ“€ μ‚¬μ΄μ¦ˆ 비ꡐ_cra-bundle-analyzer


cra-bundle-analyzerλŠ” CRA ν”„λ‘œμ νŠΈμ—μ„œ λ²ˆλ“€ 크기λ₯Ό λΆ„μ„ν•˜κ³  μ‹œκ°ν™”ν•˜λŠ” 도ꡬ이닀. 이λ₯Ό 톡해 λ²ˆλ“€μ— ν¬ν•¨λœ νŒŒμΌλ“€μ˜ 크기λ₯Ό κ·Έλž˜ν”„λ‚˜ 트리 λ§΅κ³Ό 같은 μ‹œκ°μ μΈ ν˜•νƒœλ‘œ 확인할 수 μžˆλ‹€.

  • μ„€μΉ˜ : npm install --save-dev cra-bundle-analyzer
  • μ‚¬μš© : npx cra-bundle-analyzer

λͺ…λ Ήμ–΄λ₯Ό μˆ˜ν–‰ν•˜λ©΄ html 파일이 ν•˜λ‚˜ λ§Œλ“€μ–΄μ§€λŠ”λ° μ•„λž˜μ˜ 이미와 같은 파일이 μƒμ„±λœλ‹€. <p style="color: lightgray; font-size: 12px">μ˜€λ©”,,,이와쀑에 λ†€λΌμš΄ μ‘΄μž¬κ°μ„ ν‘œν•˜λŠ” faker…</p> image

자, 그럼 Echart 의 트리 쉐이킹 μ „ ν›„ 크기λ₯Ό λΉ„κ΅ν•΄λ³΄μž. (Parse size)

echart νŠΈλ¦¬μ‰μ΄ν‚Ή ν›„ echart νŠΈλ¦¬μ‰μ΄ν‚Ή μ „


였호라 νŠΈλ¦¬μ‰μ΄ν‚Ή 성곡! 430KB 정도 μ€„μ–΄λ“€μ—ˆλ‹€.😎






μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œλŠ” μ‚¬μš©μž κ²½ν—˜ μΈ‘λ©΄μ—μ„œ 초기 λ‘œλ”© 속도가 맀우 μ€‘μš”ν•˜λ‹€. 이λ₯Ό μœ„ν•΄ μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μ½”λ“œλ₯Ό μ€„μ—¬λ‚˜κ°€λŠ” 것이 맀우 μ€‘μš”ν•œλ° νŠΈλ¦¬μ‰μ΄ν‚Ήμ΄ ν•˜λ‚˜μ˜ 방법이 λ˜λŠ” 것을 이번 Echart 라이브러리λ₯Ό μ»€μŠ€ν…€ν•΄λ³΄λ©΄μ„œ μ•Œκ²Œλ˜μ—ˆλ‹€. λ‚˜ 이 파트 λ‹΄λ‹Ήν•œκ±° μ°Έ μž˜ν•œ 걸지도..? μ˜€λŠ˜λ„ ν•˜λ‚˜ λ°°μ›Œκ°„λ‹€. 근데 그러고 λ³΄λ‹ˆ λ‚΄ ν”„λ‘œμ νŠΈμ— μ§€κΈˆ echart만 νŠΈλ¦¬μ‰μ΄ν‚Ήμ΄ ν•„μš”ν•œκ²Œ μ•„λ‹Œλ“―ν•˜λ‹€(?) μ–΄μ¨‹λ˜ μ§€μ‹μ˜ μ˜μ—­μ„ 더 λ»—μΉ˜κ³  μžˆλŠ” 이 κ³Όμ •μ—μ„œ μ½”λ“œ μž‘μ„± μš”λ Ή 뿐만이 μ•„λ‹Œ ν”„λ‘œμ νŠΈ μ„±λŠ₯κΉŒμ§€ 잘.μ•Œ.이 되기 μœ„ν•΄ 더 λ§Žμ€ 곡뢀λ₯Ό 해야함을 κΉ¨λ‹¬μ•˜λ‹€. λ‚΄μΉœκΉ€μ— λ¦¬μ•‘νŠΈ μ„±λŠ₯ μ΅œμ ν™”μ— λŒ€ν•΄ 더 μ•Œμ•„λ΄μ•Όκ² λ‹€ ν›„ν›„. μ˜€λŠ˜λ„ λΆ€λ¦‰λΆ€λ¦‰ν•˜λŠ” λ‚˜μ˜ ꢁλ‘₯이!! ν™”μ΄νŒ…~~!πŸ€“