Skip to content

Commit

Permalink
fix: lodash tree shaking (#1852)
Browse files Browse the repository at this point in the history
fix: replace named lodash imports with deep ones for better tree shaking

Instead of

  import { debounce } from 'lodash';

use

  import debounce from 'lodash/debounce';

This helps with tree shaking. In particular I ran into issues with this
when trying to update Remix apps to use Vite.

An alternative would be to use 'lodash-es' instead, which is esm modules.
  • Loading branch information
ahuth authored Feb 23, 2024
1 parent 59267c3 commit f55f9cb
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@
},
{
"files": ["src/**/*.{ts,tsx}"],
"excludedFiles": ["src/**/*.stories.{ts,tsx}"],
"excludedFiles": ["src/**/*.{stories,test,spec}.{ts,tsx}"],
"rules": {
"no-restricted-imports": [
"error",
{
"paths": [
{
"name": "lodash",
"message": "For better tree shaking, prefer deep imports from lodash (e.g. import at from 'lodash/at') instead of named (e.g. import {at} from 'lodash)"
}
],
"patterns": [
{
"group": ["**/tokens-dist/ts/colors"],
Expand Down
2 changes: 1 addition & 1 deletion src/components/Breadcrumbs/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from 'clsx';
import { debounce } from 'lodash';
import debounce from 'lodash/debounce';
import React, { createContext, useContext, type ReactNode } from 'react';
import { flattenReactChildren } from '../../util/flattenReactChildren';
import Icon, { type IconName } from '../Icon';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Icon/Icon.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StoryObj, Meta } from '@storybook/react';
import { kebabCase } from 'lodash';
import kebabCase from 'lodash/kebabCase';
import React from 'react';
import { Icon, type IconProps } from './Icon';
import icons, { type IconName } from '../../icons/spritemap';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/Filters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce } from 'lodash';
import debounce from 'lodash/debounce';
import React, { useEffect, useState } from 'react';
import {
Checkbox,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/StackedCardsToTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debounce } from 'lodash';
import debounce from 'lodash/debounce';
import React, { useEffect, useState } from 'react';
import { Card, Table, Text } from '../../../src';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from 'clsx';
import { debounce } from 'lodash';
import debounce from 'lodash/debounce';
import React, {
type ReactNode,
useCallback,
Expand Down

0 comments on commit f55f9cb

Please sign in to comment.