Next.js Antd 🐜 DatePicker ν•œκΈ€ν™”ν•˜κΈ° (Locale)

Taehyun NamΒ·2022λ…„ 11μ›” 8일
1
post-thumbnail

Localization Example

import 'moment/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';

<DatePicker locale={locale} />;

https://ant.design/components/date-picker/#Localization

예제λ₯Ό κ·ΈλŒ€λ‘œ λ”°λΌν–ˆλŠ”λ°, μ•„λž˜ λ¬Έμ œκ°€ λ°œμƒν–ˆλ‹€. 😭
별거 μ•„λ‹ˆμ§€λ§Œ, λ˜‘κ°™μ€ λ¬Έμ œκ°€ λ°œμƒν•œ κ°œλ°œμžμ—κ²Œ μ‚½μ§ˆμ˜ μ‹œκ°„μ„ μ€„λ €λ³΄κ³ μž μž‘μ„±ν•œλ‹€..

Error

Server Error
SyntaxError: Unexpected token 'export'

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
file:///node_modules/rc-picker/es/locale/en_US.js (1)
file:///node_modules/rc-pagination/es/locale/en_US.js (1)
file:///node_modules/rc-notification/es/locale/en_US.js (1)

πŸ”Ž 검색 κ²°κ³Ό

문제

  • λΈŒλΌμš°μ €κ°€ ES6 λͺ¨λ“ˆ ꡬ문을 인식할 수 μ—†κΈ° λ•Œλ¬Έμ— μ½”λ“œκ°€ λΈŒλΌμš°μ €μ—μ„œ 싀행될 λ•Œ 였λ₯˜κ°€ λ°œμƒν•œλ‹€.
  • Next.js λŠ” Default 둜 src 폴더에 μžˆλŠ” ES6 μ½”λ“œλ§Œ 트랜슀 파일 ν•˜λ„λ‘ babel-loader κ°€ κ΅¬μ„±λ˜μ–΄μžˆλ‹€.
  • node_modules μ—μ„œ κ°€μ Έμ˜¨ λͺ¨λ“  ES6 μ½”λ“œλŠ” λ³€ν™˜ 없이 μ΅œμ’… λ²ˆλ“€λ‘œ 직접 μ΄λ™ν•΄μ„œ λ°œμƒν•œ λ¬Έμ œλ‹€.

ν•΄κ²° λ°©μ•ˆ

  • next.config.js 파일 μ•ˆμ—μ„œ webpack 섀정을 μˆ˜μ •ν•˜μž.
  • next-transpile-modulesλ₯Ό ν†΅ν•΄μ„œ 트랜슀 파일 ν•΄λ³΄μž.
  • μ—λŸ¬ λ‚΄μš©μ— 포함 된 λͺ¨λ“ˆμ„ 트랜슀 파일 ν•˜λ„λ‘ μΆ”κ°€ν•˜μž.
// next.config.js
const withTM = require('next-transpile-modules')([
  'antd',
  'rc-picker',
  'rc-pagination',
  'rc-notification',
])

https://stackoverflow.com/a/65939797/17484964

μΆ”κ°€λ‘œ λ°œμƒν•œ Error

Server Error
SyntaxError: Cannot use import statement outside a module

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
/node_modules/rc-util/es/omit.js (1)
/node_modules/ (ant-design/icons/es/components/Context.js (1)

ν•΄λ‹Ή μ—λŸ¬λ„ λ™μΌν•˜κ²Œ μ²˜λ¦¬ν•˜λ©΄ λœλ‹€κ³  ν•œλ‹€.
ν•œκΈ€ν™”ν•˜λŠ”λ°, μ—°κ΄€λœ νŒ¨ν‚€μ§€λ₯Ό λ‹€ 트랜슀 파일 ν•΄μ€˜μ•Ό ν•˜λŠ” 것 κ°™λ‹€.
ν•˜λ‚˜ν•˜λ‚˜ λ„£μ–΄μ£Όλ‹€ λ³΄λ‹ˆ μ•„λž˜κ°€ μ΅œμ’… μ½”λ“œμ΄λ‹€.

// next.config.js
const withTM = require('next-transpile-modules')([
  'antd',
  'rc-picker',
  'rc-util',
  '@ant-design/icons',
  'rc-pagination',
  'rc-notification',
])

πŸ”¨ 이제 λ“œλ””μ–΄ μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šκ³  ν•œκΈ€ν™” λ˜μ—ˆλŠ”λ°, momentλŠ” λ²ˆλ“€ μ‚¬μ΄μ¦ˆλ₯Ό 많이 μž‘μ•„λ¨ΉλŠ”λ‹€. dayjs 둜 λ°”κΏ”λ³΄μž.

Replace Moment

// components/common/DatePicker.tsx
import { Dayjs } from 'dayjs';
import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs';
import generatePicker from 'antd/es/date-picker/generatePicker';

const DatePicker = generatePicker<Dayjs>(dayjsGenerateConfig);

export default DatePicker;

μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λ©΄ dayjsλ₯Ό μ‚¬μš©ν•œλ‹€.

import 'dayjs/locale/ko'

importλ₯Ό μΆ”κ°€ν•΄μ„œ ν•œκΈ€ν™”λ₯Ό λ§ˆλ¬΄λ¦¬ν•œλ‹€.

https://ant.design/docs/react/replace-moment#DatePicker.tsx

λŒ€λž΅ μ •λ¦¬λœ μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™λ‹€.
μ»΄ν¬λ„ŒνŠΈλ‘œ λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•˜λ©΄ 될 것 κ°™λ‹€.

import 'dayjs/locale/ko'

import { Dayjs } from 'dayjs'
import dayjsGenerateConfig from 'rc-picker/lib/generate/dayjs'
import generatePicker from 'antd/es/date-picker/generatePicker'
import locale from 'antd/es/date-picker/locale/ko_KR'

const DatePicker = generatePicker<Dayjs>(dayjsGenerateConfig)

interface AntDatePickerProps {
  // ...
}

export const AntDatePicker = ({ }: AntDatePickerProps) => {
  return <DatePicker locale={locale} />
}

κ²°κ³Ό

profile
Frontend Developer

2개의 λŒ“κΈ€

comment-user-thumbnail
2023λ…„ 2μ›” 22일

이젠 transpilePackagesλ₯Ό 써야 ν•©λ‹ˆλ‹€

λ‹΅κΈ€ 달기
comment-user-thumbnail
2024λ…„ 2μ›” 21일

덕뢄에 잘 ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€! κ°μ‚¬ν•©λ‹ˆλ‹€

λ‹΅κΈ€ 달기