Skip to content

refactor(ESModules): replace requires by imports in source files #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/calendar/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import {formatNumbers, weekDayNames} from '../../dateutils';
import styleConstructor from './style';
import {Theme, Direction} from '../../types';
import leftArrowIcon from '../img/previous.png';
import rightArrowIcon from '../img/next.png';

export interface CalendarHeaderProps {
/** The current month presented in the calendar */
Expand Down Expand Up @@ -234,7 +236,7 @@ const CalendarHeader = forwardRef((props: CalendarHeaderProps, ref) => {
const arrowId = isLeft ? 'leftArrow' : 'rightArrow';
const shouldDisable = isLeft ? disableArrowLeft : disableArrowRight;
const onPress = !shouldDisable ? isLeft ? onPressLeft : onPressRight : undefined;
const imageSource = isLeft ? require('../img/previous.png') : require('../img/next.png');
const imageSource = isLeft ? leftArrowIcon : rightArrowIcon;
const renderArrowDirection = isLeft ? 'left' : 'right';

return (
Expand Down
12 changes: 6 additions & 6 deletions src/componentUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {ReservationListProps} from './agenda/reservation-list';

import {MarkingProps} from './calendar/day/marking';

const get = require('lodash/get');
const omit = require('lodash/omit');
const pickBy = require('lodash/pickBy');
const isEqual = require('lodash/isEqual');
const includes = require('lodash/includes');
import get from 'lodash/get';
import omit from 'lodash/omit';
import pickBy from 'lodash/pickBy';
import isEqual from 'lodash/isEqual';
import includes from 'lodash/includes';

export function shouldUpdate(props: any, newProps: any, paths: string[]) {
for (let i = 0; i < paths.length; i++) {
Expand All @@ -28,7 +28,7 @@ export function extractComponentProps(component: any, props: any, ignoreProps?:
const keys = Object.keys(componentPropTypes);
const componentProps = omit(
pickBy(props, (_value: any, key: any) => includes(keys, key)),
ignoreProps
ignoreProps || []
);
return componentProps;
}
Expand Down
6 changes: 4 additions & 2 deletions src/dateutils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const XDate = require('xdate');
const {toMarkingFormat} = require('./interface');
import XDateClass from 'xdate';
import {toMarkingFormat} from './interface';

const XDate = XDateClass as any;
Comment on lines +1 to +4
Copy link
Author

Choose a reason for hiding this comment

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

require types modules as any, since this change is leading to a lot of typing problems, I prefered to keep the same behavior as before


const latinNumbersPattern = /[0-9]/g;

Expand Down
4 changes: 2 additions & 2 deletions src/day-state-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {isToday, isDateNotInRange, sameMonth} = require('./dateutils');
const {toMarkingFormat} = require('./interface');
import {isToday, isDateNotInRange, sameMonth} from './dateutils';
import {toMarkingFormat} from './interface';


export function getState(day: XDate, current: XDate, props: any, disableDaySelection?: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions src/expandableCalendar/Context/todayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import styleConstructor from '../style';
import Context from './index';

const TOP_POSITION = 65;
const DOWN_ICON = require('../../img/down.png');
const UP_ICON = require('../../img/up.png');
import DOWN_ICON from '../../img/down.png';
import UP_ICON from '../../img/up.png';

export interface TodayButtonProps extends ViewProps {
/** The opacity for the disabled button (0-1) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'react-native-calendars';
import {toMarkingFormat} from '../../interface';

const XDate = require('xdate');
import XDate from 'xdate';

const today = new XDate();
export const testIdExpandableCalendar = 'myExpandableCalendar';
Expand Down
3 changes: 1 addition & 2 deletions src/expandableCalendar/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {UpdateSources} from '../commons';
import times from 'lodash/times';
import {NUMBER_OF_PAGES} from '../WeekCalendar';
import {CalendarContextProviderProps} from 'react-native-calendars';

const XDate = require('xdate');
import XDate from 'xdate';

enum Direction {
LEFT = 'left',
Expand Down
5 changes: 3 additions & 2 deletions src/expandableCalendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import WeekCalendar from './WeekCalendar';
import Context from './Context';
import constants from '../commons/constants';
import {UpdateSources} from './commons';
import LEFT_ARROW from '../calendar/img/previous.png';
import RIGHT_ARROW from'../calendar/img/next.png';

export enum Positions {
CLOSED = 'closed',
Expand All @@ -42,8 +44,7 @@ const BOUNCINESS = 6;
const WEEK_HEIGHT = 46;
const DAY_NAMES_PADDING = 24;
const PAN_GESTURE_THRESHOLD = 30;
const LEFT_ARROW = require('../calendar/img/previous.png');
const RIGHT_ARROW = require('../calendar/img/next.png');

const knobHitSlop = {left: 10, right: 10, top: 10, bottom: 10};

export interface ExpandableCalendarProps extends CalendarListProps {
Expand Down
4 changes: 4 additions & 0 deletions src/images.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.png' {
const value: import('react-native').ImageSourcePropType;
export default value;
}
Comment on lines +1 to +4
Copy link
Author

Choose a reason for hiding this comment

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

Without this file, typescript does not allow to import png

2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const XDate = require('xdate');
import XDate from 'xdate';

export function padNumber(n: number) {
if (n < 10) {
Expand Down
4 changes: 2 additions & 2 deletions src/momentResolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let moment: any;

// Moment is an optional dependency
export const getMoment = () => {
export const getMoment = async () => {
if (!moment) {
try {
moment = require('moment');
moment = await import('moment');
} catch {
// Moment is not available
}
Expand Down
5 changes: 2 additions & 3 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import isDate from 'lodash/isDate';
import isString from 'lodash/isString';
import isNumber from 'lodash/isNumber';
import XDate from 'xdate';

const {getLocale} = require('../dateutils');
const {padNumber, toMarkingFormat} = require('../interface');
import {getLocale} from '../dateutils';
import {padNumber, toMarkingFormat} from '../interface';

export function getCalendarDateString(date?: Date | string | number) {
if (!isUndefined(date)) {
Expand Down