react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

Import from react-datepicker duplicates date-fns modules

Open alexandrchebotar opened this issue 3 years ago • 8 comments

Describe the bug Because of direct import from date-fns/[module] when you import react-datepicker with es6 import you add that modules to bundle. Same time if you import something from date-fns or other library that uses date-fns, for example date-fns-tz, you add to bundle modules from date-fns/esm. As a result you duplicate some date-fns modules.

To Reproduce Steps to reproduce the behavior:

  1. Import react-datepicker:
import DatePicker from "react-datepicker";
  1. Import something from date-fns:
import { parseISO } from "date-fns";
import { format, utcToZonedTime } from 'date-fns-tz';
  1. Make build.
  2. See duplicated modules in bundle.

Expected behavior import DatePicker from "react-datepicker" should add modules from date-fns/esm/

Screenshots image

Additional context React Date Picker version - 3.6.0.

alexandrchebotar avatar Feb 28 '21 21:02 alexandrchebotar

This is a bug in date-fns.

tu4mo avatar Mar 05 '21 08:03 tu4mo

@tu4mo these are different issues.

alexandrchebotar avatar Mar 06 '21 16:03 alexandrchebotar

@AlexandrChebotar Oh, sorry. Was looking at the wrong thing.

tu4mo avatar Mar 06 '21 18:03 tu4mo

@AlexandrChebotar have you found a temporary workaround to force the use of a common date-fns module?

himynameistimli avatar Jun 01 '21 07:06 himynameistimli

I hope that this issue will be resolved in near future

magiskboy avatar Feb 22 '22 11:02 magiskboy

I think rc-picker uses star import for all locales of date-fns so this leads to growing bundle size https://github.com/react-component/picker/blob/master/src/generate/dateFns.ts#L26

magiskboy avatar Feb 22 '22 11:02 magiskboy

Any updates on this?

gabycperezdias avatar Sep 07 '22 13:09 gabycperezdias

Looking through https://github.com/date-fns/date-fns/issues/2207 and verifying that treeshaking should work in my project, I think the "duplication" mentioned here is just because react-datepicker actually does import all those date-fns functions? Or am I missing something else?

Refer to the date_utils.js file where 57 functions are imported, used and re-exported in some way to be used elsewhere.

ThenTech avatar Oct 13 '23 16:10 ThenTech