From e3ac442d2ae39a166f7e1031be9170a47d7b8b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E4=BC=9F=E6=9D=B0?= <674416404@qq.com> Date: Sat, 25 Jan 2025 19:31:26 +0800 Subject: [PATCH 1/4] refactor: lodash-es instead of lodash --- .eslintrc.js | 2 +- package.json | 6 +++--- script/generate-coverage.js | 2 +- script/rollup.config.js | 4 ++-- src/_common | 2 +- src/_util/convertUnit.ts | 2 +- src/_util/getContainer.ts | 5 ++--- src/_util/nearest.ts | 2 +- src/_util/parseTNode.ts | 2 +- src/_util/withNativeProps.ts | 3 +-- src/back-top/Backtop.tsx | 2 +- src/badge/Badge.tsx | 3 +-- src/cell/Cell.tsx | 2 +- src/checkbox/CheckboxGroup.tsx | 2 +- src/grid/GridItem.tsx | 4 +--- src/guide/utils/dom.ts | 3 +-- src/guide/utils/shared.ts | 3 +-- src/indexes/Indexes.tsx | 2 +- src/input/Input.tsx | 2 +- src/message/Message.tsx | 2 +- src/message/hooks/useMessageCssTransition.ts | 1 - src/notice-bar/NoticeBar.tsx | 3 +-- src/picker/PickerItem.tsx | 2 +- src/picker/PickerView.tsx | 2 +- src/picker/_example/base.jsx | 2 +- src/progress/Progress.tsx | 2 +- src/pull-down-refresh/PullDownRefresh.tsx | 3 +-- src/skeleton/Skeleton.tsx | 3 +-- src/slider/Slider.tsx | 3 +-- src/swipe-cell/SwipeCell.tsx | 3 +-- src/switch/Switch.tsx | 2 +- src/table/BaseTable.tsx | 3 +-- 32 files changed, 35 insertions(+), 49 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 228493ad..2257a62e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -73,6 +73,6 @@ module.exports = { 'func-names': 'off', 'consistent-return': 'off', 'default-case': 'off', - 'lodash/import-scope': [2, 'method'], + 'lodash/import-scope': [2, 'member'], }, }; diff --git a/package.json b/package.json index 5a2dae70..28de4cce 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "@testing-library/jest-dom": "^5.16.2", "@testing-library/react": "^12.1.4", "@testing-library/user-event": "^13.5.0", - "@types/lodash": "^4.14.178", + "@types/lodash-es": "^4.17.12", "@types/node": "^20.14.11", "@types/react": "^17.0.2", "@types/react-dom": "17.0.2", @@ -119,7 +119,7 @@ "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.25.3", "eslint-plugin-jsx-a11y": "^6.5.1", - "eslint-plugin-lodash": "^7.4.0", + "eslint-plugin-lodash": "^8.0.0", "eslint-plugin-react": "^7.27.1", "eslint-plugin-react-hooks": "^4.3.0", "find-node-modules": "^2.1.2", @@ -167,7 +167,7 @@ "classnames": "^2.3.1", "dayjs": "^1.11.13", "hoist-non-react-statics": "^3.3.2", - "lodash": "^4.17.21", + "lodash-es": "^4.17.21", "react-spring": "9.6.1", "react-transition-group": "^4.4.2", "smoothscroll-polyfill": "^0.4.4", diff --git a/script/generate-coverage.js b/script/generate-coverage.js index b05fe62f..b0c31c00 100644 --- a/script/generate-coverage.js +++ b/script/generate-coverage.js @@ -1,6 +1,6 @@ const fs = require('fs'); const path = require('path'); -const camelCase = require('lodash/camelCase'); +const { camelCase } = require('lodash-es'); const DomParser = require('dom-parser'); diff --git a/script/rollup.config.js b/script/rollup.config.js index 5fa35c74..5171f1b5 100644 --- a/script/rollup.config.js +++ b/script/rollup.config.js @@ -215,7 +215,7 @@ const umdConfig = { banner, format: 'umd', exports: 'named', - globals: { react: 'React', lodash: '_' }, + globals: { react: 'React' }, sourcemap: true, file: `dist/${name}.js`, }, @@ -235,7 +235,7 @@ const umdMinConfig = { banner, format: 'umd', exports: 'named', - globals: { react: 'React', lodash: '_' }, + globals: { react: 'React' }, sourcemap: true, file: `dist/${name}.min.js`, }, diff --git a/src/_common b/src/_common index c683f47c..e23ad042 160000 --- a/src/_common +++ b/src/_common @@ -1 +1 @@ -Subproject commit c683f47c2c476bb2bf930c9e3fa791dbeede362d +Subproject commit e23ad04213d64f3b7161cc4cc758f8ca03d06ae9 diff --git a/src/_util/convertUnit.ts b/src/_util/convertUnit.ts index ad5128c2..9888ce56 100644 --- a/src/_util/convertUnit.ts +++ b/src/_util/convertUnit.ts @@ -1,4 +1,4 @@ -import isNumber from 'lodash/isNumber'; +import { isNumber } from 'lodash-es'; export const convertUnit = (val: string | number | undefined) => { if (val == null) return 0; diff --git a/src/_util/getContainer.ts b/src/_util/getContainer.ts index 77c6296d..b35e58b5 100644 --- a/src/_util/getContainer.ts +++ b/src/_util/getContainer.ts @@ -1,7 +1,6 @@ -import isFunction from 'lodash/isFunction'; -import isString from 'lodash/isString'; +import { isFunction, isString } from 'lodash-es'; -import { ScrollContainerElement, ScrollContainer } from '../common' +import { ScrollContainerElement, ScrollContainer } from '../common'; export function resolveContainer(getContainer: ScrollContainerElement | ScrollContainer): ScrollContainerElement { if (isString(getContainer)) { diff --git a/src/_util/nearest.ts b/src/_util/nearest.ts index c374f8b5..5ae00c0d 100644 --- a/src/_util/nearest.ts +++ b/src/_util/nearest.ts @@ -1,4 +1,4 @@ -import isNumber from 'lodash/isNumber'; +import { isNumber } from 'lodash-es'; export interface nearestParams { items: number[]; // 一组数字 diff --git a/src/_util/parseTNode.ts b/src/_util/parseTNode.ts index 468639ee..7dc12e87 100644 --- a/src/_util/parseTNode.ts +++ b/src/_util/parseTNode.ts @@ -1,5 +1,5 @@ import React, { ReactElement, ReactNode } from 'react'; -import isFunction from 'lodash/isFunction'; +import { isFunction } from 'lodash-es'; import { TNode } from '../common'; import log from '../_common/js/log'; diff --git a/src/_util/withNativeProps.ts b/src/_util/withNativeProps.ts index 86381931..702a4323 100644 --- a/src/_util/withNativeProps.ts +++ b/src/_util/withNativeProps.ts @@ -1,8 +1,7 @@ import React from 'react'; import type { CSSProperties, ReactElement } from 'react'; import cls from 'classnames'; -import keys from 'lodash/keys'; -import assign from 'lodash/assign'; +import { assign, keys } from 'lodash-es'; export interface NativeProps { className?: string; diff --git a/src/back-top/Backtop.tsx b/src/back-top/Backtop.tsx index d8795458..c439c71b 100644 --- a/src/back-top/Backtop.tsx +++ b/src/back-top/Backtop.tsx @@ -2,7 +2,7 @@ import React, { useRef, useEffect } from 'react'; import classNames from 'classnames'; import { useScroll, useMount, useBoolean } from 'ahooks'; import smoothscroll from 'smoothscroll-polyfill'; -import isString from 'lodash/isString'; +import { isString } from 'lodash-es'; import { Icon } from 'tdesign-icons-react'; import withNativeProps, { NativeProps } from '../_util/withNativeProps'; import useConfig from '../_util/useConfig'; diff --git a/src/badge/Badge.tsx b/src/badge/Badge.tsx index 9d876472..7732675a 100644 --- a/src/badge/Badge.tsx +++ b/src/badge/Badge.tsx @@ -1,7 +1,6 @@ import React, { forwardRef, useMemo } from 'react'; import classNames from 'classnames'; -import isNumber from 'lodash/isNumber'; -import isString from 'lodash/isString'; +import { isNumber, isString } from 'lodash-es'; import type { StyledProps } from '../common'; import type { TdBadgeProps } from './type'; import { badgeDefaultProps } from './defaultProps'; diff --git a/src/cell/Cell.tsx b/src/cell/Cell.tsx index c5c81658..20603097 100644 --- a/src/cell/Cell.tsx +++ b/src/cell/Cell.tsx @@ -1,6 +1,6 @@ import React, { useMemo, useCallback } from 'react'; import classnames from 'classnames'; -import isString from 'lodash/isString'; +import { isString } from 'lodash-es'; import { ChevronRightIcon } from 'tdesign-icons-react'; import parseTNode from '../_util/parseTNode'; diff --git a/src/checkbox/CheckboxGroup.tsx b/src/checkbox/CheckboxGroup.tsx index cea8d09f..7a177d47 100644 --- a/src/checkbox/CheckboxGroup.tsx +++ b/src/checkbox/CheckboxGroup.tsx @@ -1,5 +1,5 @@ import React, { FC, ReactElement, useEffect, useMemo, useState } from 'react'; -import isNumber from 'lodash/isNumber'; +import { isNumber } from 'lodash-es'; import classNames from 'classnames'; import { CheckboxOption, CheckboxOptionObj, TdCheckboxGroupProps } from './type'; import { StyledProps } from '../common'; diff --git a/src/grid/GridItem.tsx b/src/grid/GridItem.tsx index 2c3a0dfa..213451b3 100644 --- a/src/grid/GridItem.tsx +++ b/src/grid/GridItem.tsx @@ -1,8 +1,6 @@ import React, { FC, useContext, useMemo } from 'react'; import cls from 'classnames'; -import isString from 'lodash/isString'; -import isObject from 'lodash/isObject'; -import isFunction from 'lodash/isFunction'; +import { isFunction, isString, isObject } from 'lodash-es'; import useConfig from '../_util/useConfig'; import parseTNode from '../_util/parseTNode'; import useDefaultProps from '../hooks/useDefaultProps'; diff --git a/src/guide/utils/dom.ts b/src/guide/utils/dom.ts index 3884629d..ca3441c7 100644 --- a/src/guide/utils/dom.ts +++ b/src/guide/utils/dom.ts @@ -1,5 +1,4 @@ -import isString from 'lodash/isString'; -import isFunction from 'lodash/isFunction'; +import { isFunction, isString } from 'lodash-es'; import { AttachNode } from '../../common'; import { elementInViewport, getWindowScroll, getWindowSize } from './shared'; /** diff --git a/src/guide/utils/shared.ts b/src/guide/utils/shared.ts index c383cce7..c99ba2d5 100644 --- a/src/guide/utils/shared.ts +++ b/src/guide/utils/shared.ts @@ -1,5 +1,4 @@ -import isFunction from 'lodash/isFunction'; -import isString from 'lodash/isString'; +import { isFunction, isString } from 'lodash-es'; const trim = (str: string): string => (str || '').replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, ''); diff --git a/src/indexes/Indexes.tsx b/src/indexes/Indexes.tsx index b83718c2..421cae8c 100644 --- a/src/indexes/Indexes.tsx +++ b/src/indexes/Indexes.tsx @@ -1,5 +1,5 @@ import React, { useState, useRef, useEffect, useMemo } from 'react'; -import throttle from 'lodash/throttle'; +import { throttle } from 'lodash-es'; import cls from 'classnames'; import { TdIndexesProps } from './type'; import { StyledProps } from '../common'; diff --git a/src/input/Input.tsx b/src/input/Input.tsx index 6806da80..dfd887b7 100644 --- a/src/input/Input.tsx +++ b/src/input/Input.tsx @@ -1,7 +1,7 @@ import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'; import type { FocusEvent, TouchEvent, CompositionEvent, FormEvent } from 'react'; import classNames from 'classnames'; -import isFunction from 'lodash/isFunction'; +import { isFunction } from 'lodash-es'; import { CloseCircleFilledIcon, BrowseOffIcon, BrowseIcon } from 'tdesign-icons-react'; import useDefault from '../_util/useDefault'; import parseTNode from '../_util/parseTNode'; diff --git a/src/message/Message.tsx b/src/message/Message.tsx index e334d9c2..2f97c403 100644 --- a/src/message/Message.tsx +++ b/src/message/Message.tsx @@ -1,7 +1,7 @@ import React, { useEffect, useState, useRef } from 'react'; import classNames from 'classnames'; import { useTimeout } from 'ahooks'; -import isObject from 'lodash/isObject'; +import { isObject } from 'lodash-es'; import { CSSTransition } from 'react-transition-group'; import { Icon } from 'tdesign-icons-react'; diff --git a/src/message/hooks/useMessageCssTransition.ts b/src/message/hooks/useMessageCssTransition.ts index 30ce0af7..61d5f1da 100644 --- a/src/message/hooks/useMessageCssTransition.ts +++ b/src/message/hooks/useMessageCssTransition.ts @@ -1,5 +1,4 @@ import { useRef } from 'react'; -// import identity from 'lodash/identity'; import ReactDOM from 'react-dom'; interface UseMessageCssTransitionParams { diff --git a/src/notice-bar/NoticeBar.tsx b/src/notice-bar/NoticeBar.tsx index d2f4df6e..a4330a58 100644 --- a/src/notice-bar/NoticeBar.tsx +++ b/src/notice-bar/NoticeBar.tsx @@ -1,8 +1,7 @@ import React, { useEffect, useMemo, useRef, useState } from 'react'; import { InfoCircleFilledIcon, CheckCircleFilledIcon } from 'tdesign-icons-react'; import classNames from 'classnames'; -import isObject from 'lodash/isObject'; -import isArray from 'lodash/isArray'; +import { isArray, isObject } from 'lodash-es'; import parseTNode from '../_util/parseTNode'; import Swiper from '../swiper'; import SwiperItem from '../swiper/SwiperItem'; diff --git a/src/picker/PickerItem.tsx b/src/picker/PickerItem.tsx index 02146735..6e274e15 100644 --- a/src/picker/PickerItem.tsx +++ b/src/picker/PickerItem.tsx @@ -1,6 +1,6 @@ import React, { FC, useRef, memo, useCallback, useContext, useMemo } from 'react'; import { useDebounceEffect } from 'ahooks'; -import isUndefined from 'lodash/isUndefined'; +import { isUndefined } from 'lodash-es'; import { useDrag } from '@use-gesture/react'; import { useSpring, animated } from '@react-spring/web'; import useConfig from '../_util/useConfig'; diff --git a/src/picker/PickerView.tsx b/src/picker/PickerView.tsx index cdda9c30..b98c28f4 100644 --- a/src/picker/PickerView.tsx +++ b/src/picker/PickerView.tsx @@ -1,5 +1,5 @@ import React, { FC, useMemo } from 'react'; -import isUndefined from 'lodash/isUndefined'; +import { isUndefined } from 'lodash-es'; import useConfig from '../_util/useConfig'; import useDefault from '../_util/useDefault'; import withNativeProps, { NativeProps } from '../_util/withNativeProps'; diff --git a/src/picker/_example/base.jsx b/src/picker/_example/base.jsx index e67fd958..8935dd2d 100644 --- a/src/picker/_example/base.jsx +++ b/src/picker/_example/base.jsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import isEmpty from 'lodash/isEmpty'; +import { isEmpty } from 'lodash-es'; import { Picker, PickerItem, Cell } from 'tdesign-mobile-react'; import TDemoBlock from '../../../site/mobile/components/DemoBlock'; import './style/index.less'; diff --git a/src/progress/Progress.tsx b/src/progress/Progress.tsx index eb761599..50f4a2e2 100644 --- a/src/progress/Progress.tsx +++ b/src/progress/Progress.tsx @@ -8,7 +8,7 @@ import { CloseCircleFilledIcon, ErrorCircleFilledIcon, } from 'tdesign-icons-react'; -import isString from 'lodash/isString'; +import { isString } from 'lodash-es'; import { StyledProps } from '../common'; import { TdProgressProps } from './type'; import useConfig from '../_util/useConfig'; diff --git a/src/pull-down-refresh/PullDownRefresh.tsx b/src/pull-down-refresh/PullDownRefresh.tsx index 1f3d3be1..c3f5fb09 100644 --- a/src/pull-down-refresh/PullDownRefresh.tsx +++ b/src/pull-down-refresh/PullDownRefresh.tsx @@ -1,7 +1,6 @@ import React, { useRef, useState, type ReactNode, useEffect, useMemo } from 'react'; import classNames from 'classnames'; -import uniqueId from 'lodash/uniqueId'; -import isBoolean from 'lodash/isBoolean'; +import { uniqueId, isBoolean } from 'lodash-es'; import { useDebounceFn } from 'ahooks'; import Loading from '../loading'; diff --git a/src/skeleton/Skeleton.tsx b/src/skeleton/Skeleton.tsx index fb15c4ba..c4d5e1d2 100644 --- a/src/skeleton/Skeleton.tsx +++ b/src/skeleton/Skeleton.tsx @@ -1,7 +1,6 @@ import React, { useState, useEffect } from 'react'; import classNames from 'classnames'; -import isNumber from 'lodash/isNumber'; -import isArray from 'lodash/isArray'; +import { isArray, isNumber } from 'lodash-es'; import { SkeletonRowCol, SkeletonRowColObj, TdSkeletonProps } from './type'; import { StyledProps, Styles } from '../common'; import { skeletonDefaultProps } from './defaultProps'; diff --git a/src/slider/Slider.tsx b/src/slider/Slider.tsx index 517e6616..7b975031 100644 --- a/src/slider/Slider.tsx +++ b/src/slider/Slider.tsx @@ -1,8 +1,7 @@ import React, { FC, useEffect, useRef, useState } from 'react'; import type { MouseEvent, TouchEvent } from 'react'; import classNames from 'classnames'; -import isFunction from 'lodash/isFunction'; -import cloneDeep from 'lodash/cloneDeep'; +import { cloneDeep, isFunction } from 'lodash-es'; import { usePrefixClass } from '../hooks/useClass'; import useDefaultProps from '../hooks/useDefaultProps'; import useDefault from '../_util/useDefault'; diff --git a/src/swipe-cell/SwipeCell.tsx b/src/swipe-cell/SwipeCell.tsx index 29b3b547..bf4f6a0b 100644 --- a/src/swipe-cell/SwipeCell.tsx +++ b/src/swipe-cell/SwipeCell.tsx @@ -1,7 +1,6 @@ import React, { forwardRef, useImperativeHandle, useRef, useMemo, useState } from 'react'; import type { ReactNode } from 'react'; -import isArray from 'lodash/isArray'; -import isBoolean from 'lodash/isBoolean'; +import { isArray, isBoolean } from 'lodash-es'; import classNames from 'classnames'; import { useClickAway } from 'ahooks'; import { useDrag } from '@use-gesture/react'; diff --git a/src/switch/Switch.tsx b/src/switch/Switch.tsx index 5a89d5bc..a9a6518a 100644 --- a/src/switch/Switch.tsx +++ b/src/switch/Switch.tsx @@ -1,6 +1,6 @@ import React, { forwardRef, useMemo } from 'react'; import classNames from 'classnames'; -import isArray from 'lodash/isArray'; +import { isArray } from 'lodash-es'; import Loading from '../loading'; import { TdSwitchProps, SwitchValue } from './type'; import { switchDefaultProps } from './defaultProps'; diff --git a/src/table/BaseTable.tsx b/src/table/BaseTable.tsx index 6abcd5ec..85f8e0a1 100644 --- a/src/table/BaseTable.tsx +++ b/src/table/BaseTable.tsx @@ -1,6 +1,5 @@ import React, { forwardRef, useRef } from 'react'; -import isFunction from 'lodash/isFunction'; -import get from 'lodash/get'; +import { get, isFunction } from 'lodash-es'; import cx from 'classnames'; import { StyledProps } from '../common'; From b859d9031d00957df5c2bca6650f8d93e1ac2145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E4=BC=9F=E6=9D=B0?= <674416404@qq.com> Date: Sat, 25 Jan 2025 19:34:28 +0800 Subject: [PATCH 2/4] chore: remove eslint-plugin-lodash --- .eslintrc.js | 3 +-- package.json | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2257a62e..2bebd5d4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,7 @@ module.exports = { 'plugin:@typescript-eslint/recommended', ], parser: '@typescript-eslint/parser', - plugins: ['@typescript-eslint', 'react-hooks', 'lodash'], + plugins: ['@typescript-eslint', 'react-hooks'], env: { browser: true, node: true, @@ -73,6 +73,5 @@ module.exports = { 'func-names': 'off', 'consistent-return': 'off', 'default-case': 'off', - 'lodash/import-scope': [2, 'member'], }, }; diff --git a/package.json b/package.json index 28de4cce..9474f9f2 100644 --- a/package.json +++ b/package.json @@ -119,7 +119,6 @@ "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.25.3", "eslint-plugin-jsx-a11y": "^6.5.1", - "eslint-plugin-lodash": "^8.0.0", "eslint-plugin-react": "^7.27.1", "eslint-plugin-react-hooks": "^4.3.0", "find-node-modules": "^2.1.2", From ceebb49944ec0008811169ac9f4156d605a6367f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E4=BC=9F=E6=9D=B0?= <674416404@qq.com> Date: Sun, 26 Jan 2025 00:15:46 +0800 Subject: [PATCH 3/4] chore(eslint): restricted import lodash --- .eslintrc.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 2bebd5d4..222b0d9c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -73,5 +73,16 @@ module.exports = { 'func-names': 'off', 'consistent-return': 'off', 'default-case': 'off', + 'no-restricted-imports': [ + 'error', + { + paths: [ + { + name: 'lodash', + message: 'Please use lodash-es instead.', + }, + ], + }, + ], }, }; From b9ce4541bf6990654482ec0b7fc19096a8a0b4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=8E=E4=BC=9F=E6=9D=B0?= <674416404@qq.com> Date: Sun, 9 Feb 2025 21:17:40 +0800 Subject: [PATCH 4/4] chore: update common --- src/_common | 2 +- src/guide/Guide.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/_common b/src/_common index e23ad042..b7935cb3 160000 --- a/src/_common +++ b/src/_common @@ -1 +1 @@ -Subproject commit e23ad04213d64f3b7161cc4cc758f8ca03d06ae9 +Subproject commit b7935cb3e86877dd68091d1d24fd93f55be3f1e7 diff --git a/src/guide/Guide.tsx b/src/guide/Guide.tsx index 4445bf60..a4a0ba14 100644 --- a/src/guide/Guide.tsx +++ b/src/guide/Guide.tsx @@ -9,7 +9,7 @@ import { TdGuideProps, GuideCrossProps } from './type'; import Portal from '../common/Portal'; import { SizeEnum, StyledProps } from '../common'; -import setStyle from '../_common/js/utils/set-style'; +import setStyle from '../_common/js/utils/setStyle'; import { usePrefixClass } from '../hooks/useClass'; import useDefault from '../_util/useDefault';