Skip to content

Commit

Permalink
perf: Cherry-pick Lodash imports for smaller bundles
Browse files Browse the repository at this point in the history
Use Cherry-picked Lodash imports for smaller bundles
  • Loading branch information
jrood authored May 1, 2024
1 parent 25b0f34 commit 0ee58af
Show file tree
Hide file tree
Showing 43 changed files with 96 additions and 49 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,12 @@ module.exports = {
'@typescript-eslint/restrict-template-expressions': 'off',
},
},
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
plugins: ['lodash'],
rules: {
'lodash/import-scope': ['error', 'method'],
},
},
],
};
2 changes: 1 addition & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const nxPreset = require('@nx/jest/preset').default;
const { kebabCase } = require('lodash');
const kebabCase = require('lodash/kebabCase');

const targetProject = kebabCase(
process.env.NX_TASK_TARGET_PROJECT || 'unknown'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@types/classnames": "2.2.10",
"@types/invariant": "2.2.29",
"@types/konami-code-js": "^0.8.0",
"@types/lodash": "4.14.168",
"@types/lodash": "4.17.0",
"@types/react": "^17",
"@types/react-dom": "^17",
"@types/react-test-renderer": "^17.0.1",
Expand Down Expand Up @@ -70,6 +70,7 @@
"typescript": "5.1.3"
},
"devDependencies": {
"eslint-plugin-lodash": "^7.4.0",
"onchange": "^7.0.2",
"webpack": "4.46.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut-patterns/pattern-template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const { startCase } = require('lodash');
const startCase = require('lodash/startCase');

function patternTemplate(api, opts, { jsx }) {
const template = api.template.smart({ plugins: ['jsx', 'typescript'] });
Expand Down
3 changes: 2 additions & 1 deletion packages/gamut-styles/src/ColorMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from '@codecademy/variance';
import { CSSObject, Theme, ThemeProvider, useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { mapValues, pick } from 'lodash';
import mapValues from 'lodash/mapValues';
import pick from 'lodash/pick';
import {
ComponentProps,
createContext,
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut-styles/src/__tests__/Background.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { matchers } from '@emotion/jest';
import { ThemeProvider, useTheme } from '@emotion/react';
import { setupRtl as setupRtlBase } from 'component-test-setup';
import { overArgs } from 'lodash';
import overArgs from 'lodash/overArgs';
import * as React from 'react';

import { Background } from '../Background';
Expand Down
4 changes: 2 additions & 2 deletions packages/gamut-styles/src/utilities/themed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Path, PathValue } from '@codecademy/variance';
import { Theme } from '@emotion/react';
import { get } from 'lodash';
import get from 'lodash/get';

/**
* Creates a function that will look up the a design token from the `theme` context of a
Expand All @@ -19,5 +19,5 @@ export type SafeThemeValues = Omit<
export function themed<P extends Path<SafeThemeValues>>(
path: P
): (props: { theme: Theme }) => PathValue<SafeThemeValues, P> {
return ({ theme }) => get(theme, path);
return ({ theme }) => get(theme, path as string);
}
2 changes: 1 addition & 1 deletion packages/gamut-tests/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
setupEnzyme as setupEnzymeBase,
setupRtl as setupRtlBase,
} from 'component-test-setup';
import { overArgs } from 'lodash';
import overArgs from 'lodash/overArgs';
import * as React from 'react';

// See https://www.notion.so/codecademy/Frontend-Unit-Tests-1cbf4e078a6647559b4583dfb6d3cb18
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/ButtonDeprecated/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/destructuring-assignment */
import cx from 'classnames';
import { hasIn } from 'lodash';
import hasIn from 'lodash/hasIn';
import { ReactNode } from 'react';
import * as React from 'react';

Expand Down
4 changes: 3 additions & 1 deletion packages/gamut/src/ConnectedForm/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { isNull, isUndefined, omit } from 'lodash';
import isNull from 'lodash/isNull';
import isUndefined from 'lodash/isUndefined';
import omit from 'lodash/omit';
import {
ChangeEvent,
ChangeEventHandler,
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/DataList/Controls/FilterControl.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FilterIcon } from '@codecademy/gamut-icons';
import { states } from '@codecademy/gamut-styles';
import styled from '@emotion/styled';
import { kebabCase } from 'lodash';
import kebabCase from 'lodash/kebabCase';
import { useRef, useState } from 'react';
import * as React from 'react';

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/DataList/hooks/useListControls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { kebabCase } from 'lodash';
import kebabCase from 'lodash/kebabCase';
import { createContext, useCallback, useContext, useMemo } from 'react';

import {
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/DataList/hooks/useLocalQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { orderBy } from 'lodash';
import orderBy from 'lodash/orderBy';
import { useCallback, useMemo, useState } from 'react';

import { ColumnConfig, IdentifiableKeys, OnQueryChange, Query } from '..';
Expand Down
3 changes: 2 additions & 1 deletion packages/gamut/src/Form/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { each, isObject } from 'lodash';
import each from 'lodash/each';
import isObject from 'lodash/isObject';

import {
ExtendedOption,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from 'lodash';
import isString from 'lodash/isString';
import * as React from 'react';
import { UseFormReturn } from 'react-hook-form';

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/GridForm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from 'lodash';
import isArray from 'lodash/isArray';

import { GridFormField } from './types';

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/Layout/LayoutGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pxRem, styledOptions, system, theme } from '@codecademy/gamut-styles';
import { StyleProps, variance } from '@codecademy/variance';
import styled from '@emotion/styled';
import { pick } from 'lodash';
import pick from 'lodash/pick';

const columns = 12;

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/List/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from 'lodash';
import isArray from 'lodash/isArray';
import { ComponentProps, forwardRef, useEffect, useRef, useState } from 'react';
import * as React from 'react';

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/Markdown/libs/overrides/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cx from 'classnames';
import HtmlToReact from 'html-to-react';
import camelCaseMap from 'html-to-react/lib/camel-case-attribute-names';
import { get } from 'lodash';
import get from 'lodash/get';
import * as React from 'react';

// eslint-disable-next-line gamut/no-css-standalone
Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/Markdown/libs/overrides/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { find } from 'lodash';
import find from 'lodash/find';

import { HTMLToReactNode } from '.';

Expand Down
2 changes: 1 addition & 1 deletion packages/gamut/src/utils/__tests__/omitProps.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit } from 'lodash';
import omit from 'lodash/omit';

import { omitProps } from '../omitProps';

Expand Down
6 changes: 4 additions & 2 deletions packages/gamut/src/utils/omitProps.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import invariant from 'invariant';
import { AnyKindOfDictionary, isPlainObject, omit, without } from 'lodash';
import isPlainObject from 'lodash/isPlainObject';
import omit from 'lodash/omit';
import without from 'lodash/without';

export type RemoveFrom<TContainer, TRemoved> = {
[P in keyof TContainer]: P extends keyof TRemoved ? never : TContainer[P];
Expand Down Expand Up @@ -28,7 +30,7 @@ export function omitProps<TOmittedProps extends {}, TProps extends {}>(
const dataProps = propsToOmit.filter((p) => /^data-.*/.exec(p));

return omit(
(props as unknown) as AnyKindOfDictionary,
(props as unknown) as Record<string | number, unknown>,
without(propsToOmit, 'children', 'className', ...dataProps)
) as RemoveFrom<TOmittedProps, TProps>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentProps, useMemo } from 'react';
import * as React from 'react';
import { PROP_GROUPS } from '../propMeta';
import { ArgsTable } from '@storybook/addon-docs/blocks';
import { isArray } from 'lodash';
import isArray from 'lodash/isArray';

import {
PropItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useReducer, useMemo, useContext } from 'react';
import { intersection } from 'lodash';
import intersection from 'lodash/intersection';
import { DocsContext } from '@storybook/addon-docs/blocks';
import { ALL_PROPS, PropGroups, PROP_GROUPS } from '../propMeta';

Expand Down
8 changes: 6 additions & 2 deletions packages/styleguide/.storybook/addons/system/enhancers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { mapValues } from 'lodash/fp';
import mapValues from 'lodash/fp/mapValues';
import { ArgTypesEnhancer } from '@storybook/client-api';
import { hasIn, isObject, isString, kebabCase, merge } from 'lodash';
import hasIn from 'lodash/hasIn';
import isObject from 'lodash/isObject';
import isString from 'lodash/isString';
import kebabCase from 'lodash/kebabCase';
import merge from 'lodash/merge';
import { ALL_PROPS, PROP_MAP, PROP_GROUPS } from './propMeta';
import { theme } from '@codecademy/gamut-styles';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { NavigationProvider } from '../Navigation/NavigationProvider';
import { useEffect } from 'react';
import * as React from 'react';
import { merge } from 'lodash';
import merge from 'lodash/merge';
import { Link } from '../Markdown/Elements';
import { coreTheme } from '@codecademy/gamut-styles/src/themes/core';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DocsContext } from '@storybook/addon-docs/blocks';
import { get, mapValues } from 'lodash';
import get from 'lodash/get';
import mapValues from 'lodash/mapValues';
import { useContext, createContext, useMemo } from 'react';
import * as React from 'react';
import {
Expand Down
11 changes: 9 additions & 2 deletions packages/styleguide/.storybook/components/Navigation/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { DocsContextProps } from '@storybook/addon-docs/blocks';
import { set, head, keyBy, isEmpty, tail, update, assign } from 'lodash';
import { merge } from 'lodash/fp';
import set from 'lodash/set';
import head from 'lodash/head';
import keyBy from 'lodash/keyBy';
import isEmpty from 'lodash/isEmpty';
import tail from 'lodash/tail';
import update from 'lodash/update';
import assign from 'lodash/assign';

import merge from 'lodash/fp/merge';
import {
ComponentRegistry,
ContentItem,
Expand Down
4 changes: 3 additions & 1 deletion packages/styleguide/stories/Atoms/Icons/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// eslint-disable-next-line gamut/import-paths
import * as icons from '@codecademy/gamut-icons/src/icons/regular';
import { keys, omit, pick } from 'lodash';
import keys from 'lodash/keys';
import omit from 'lodash/omit';
import pick from 'lodash/pick';

export const ICONS = icons;

Expand Down
2 changes: 1 addition & 1 deletion packages/styleguide/stories/Foundations/System/tables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Anchor } from '@codecademy/gamut/src';
import * as ALL_PROPS from '@codecademy/gamut-styles/src/variance/config';
import { kebabCase } from 'lodash';
import kebabCase from 'lodash/kebabCase';

import { Code, LinkTo } from '~styleguide/blocks';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InfoCircleIcon } from '@codecademy/gamut-icons/src';
import title from '@codecademy/macros/lib/title.macro';
import { PropsTable } from '@codecademy/storybook-addon-variance';
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { upperFirst } from 'lodash';
import upperFirst from 'lodash/upperFirst';

import { LinkTo } from '~styleguide/blocks';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Text,
useLocalQuery,
} from '@codecademy/gamut';
import { uniq } from 'lodash';
import uniq from 'lodash/uniq';
import { useCallback, useMemo, useState } from 'react';

const crew = [
Expand Down
2 changes: 1 addition & 1 deletion packages/styleguide/stories/Typography/Anchor.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Anchor, Box, Column, LayoutGrid, Text } from '@codecademy/gamut/src';
import { Background } from '@codecademy/gamut-styles/src';
import title from '@codecademy/macros/lib/title.macro';
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { startCase } from 'lodash';
import startCase from 'lodash/startCase';

import { LinkTo, PropsTable } from '~styleguide/blocks';

Expand Down
7 changes: 6 additions & 1 deletion packages/variance/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { get, identity, isArray, isObject, isUndefined, merge } from 'lodash';
import get from 'lodash/get';
import identity from 'lodash/identity';
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
import isUndefined from 'lodash/isUndefined';
import merge from 'lodash/merge';

import { createScaleLookup } from './scales/createScaleLookup';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/variance/src/createTheme/createTheme.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapValues } from 'lodash';
import mapValues from 'lodash/mapValues';

import { createTheme } from './createTheme';

Expand Down
3 changes: 2 additions & 1 deletion packages/variance/src/createTheme/createTheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mapValues, merge } from 'lodash';
import mapValues from 'lodash/mapValues';
import merge from 'lodash/merge';

import { CSSObject } from '../types/props';
import { AbstractTheme } from '../types/theme';
Expand Down
5 changes: 4 additions & 1 deletion packages/variance/src/scales/createScaleLookup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { get, isArray, isObject, isString } from 'lodash';
import get from 'lodash/get';
import isArray from 'lodash/isArray';
import isObject from 'lodash/isObject';
import isString from 'lodash/isString';

import { Prop } from '../types/config';
import { ThemeProps } from '../types/props';
Expand Down
2 changes: 1 addition & 1 deletion packages/variance/src/transforms/transformSize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber } from 'lodash';
import isNumber from 'lodash/isNumber';

export const percentageOrAbsolute = (coordinate: number) => {
if (coordinate === 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/variance/src/utils/flattenScale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from 'lodash';
import isObject from 'lodash/isObject';

/**
* Returns an exhaustive list of all possible paths of an object T for keys K.
Expand Down
3 changes: 2 additions & 1 deletion packages/variance/src/utils/getStaticProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { keys, pick } from 'lodash';
import keys from 'lodash/keys';
import pick from 'lodash/pick';

export const getStaticCss = (
props: Record<string, any>,
Expand Down
3 changes: 2 additions & 1 deletion packages/variance/src/utils/responsive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { intersection, omit } from 'lodash';
import intersection from 'lodash/intersection';
import omit from 'lodash/omit';

import { AbstractPropTransformer } from '../types/config';
import {
Expand Down
3 changes: 2 additions & 1 deletion packages/variance/src/utils/serializeTokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Theme } from '@emotion/react';
import { isObject, merge } from 'lodash';
import isObject from 'lodash/isObject';
import merge from 'lodash/merge';

import { CSSObject } from '../types/props';

Expand Down
Loading

0 comments on commit 0ee58af

Please sign in to comment.