Skip to content

Commit

Permalink
fix(commerce): replace barrel import with proper import (#3729)
Browse files Browse the repository at this point in the history
As part of CAPI-496, we compared bundle sizes for different changes we
wanted to make to headless and spotted issues with imports in the
commerce package.

Running dependency cruiser to map a graph of dependencies in the
`packages/headless` folder showed that `index.ts` was being included
([full dependencies.svg file
here](https://github.com/coveo/test-headless-tree-shaking/blob/main/bundler-comparison/dependencies.svg)):
```shell
npx depcruise --no-config \
  --exclude "^node_modules" \
  --output-type dot \
  src/commerce.index.ts > dependencies.out && dot dependencies.out -T svg -o dependencies.svg
```

![image](https://github.com/coveo/ui-kit/assets/8978908/fa02b1e4-3367-4dc5-a509-f956e910200b)

We then found the source of the issue was
`product-listing-recent-results` with:
```shell
npx depcruise --no-config \
  --exclude "^node_modules" \
  --output-type dot \
  --ts-config tsconfig.json \
  --do-not-follow "^src/index.ts" \
  --focus "^src/index.ts" \
  src/commerce.index.ts > dependencies.out && dot dependencies.out -T svg -o dependencies.svg
```

![image](https://github.com/coveo/ui-kit/assets/8978908/5104100d-bd52-4329-a213-46cb2070b705)

This PR replace the `index.ts` barrel import with the proper redux
import.
  • Loading branch information
Spuffynism authored Mar 21, 2024
1 parent 55fc157 commit 8f4d68d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/headless/src/api/commerce/commerce-api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface CommerceAPISuccessResponse<T> {
success: T;
}

export const isErrorResponse = <T>(
r: CommerceAPIResponse<T>
): r is CommerceAPIErrorResponse => {
return (r as CommerceAPIErrorResponse).error !== undefined;
};

export class CommerceAPIClient implements CommerceFacetSearchAPIClient {
constructor(private options: CommerceAPIClientOptions) {}

Expand Down
1 change: 0 additions & 1 deletion packages/headless/src/commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type {ProductRecommendation} from './api/search/search/product-recommenda
// Actions
export * from './features/commerce/product-listing/product-listing-actions-loader';
export * from './features/configuration/configuration-actions-loader';
export * from './features/analytics/search-analytics-actions-loader';

// Controllers
export type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {createAsyncThunk} from '@reduxjs/toolkit';
import {AsyncThunkCommerceOptions} from '../../../api/commerce/commerce-api-client';
import {isErrorResponse} from '../../../api/search/search-api-client';
import {
AsyncThunkCommerceOptions,
isErrorResponse,
} from '../../../api/commerce/commerce-api-client';
import {
CartSection,
CommerceContextSection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {createAsyncThunk} from '@reduxjs/toolkit';
import {getVisitorID} from '../../../api/analytics/coveo-analytics-utils';
import {AsyncThunkCommerceOptions} from '../../../api/commerce/commerce-api-client';
import {
AsyncThunkCommerceOptions,
isErrorResponse,
} from '../../../api/commerce/commerce-api-client';
import {QuerySuggestRequest} from '../../../api/commerce/search/query-suggest/query-suggest-request';
import {QuerySuggestSuccessResponse} from '../../../api/commerce/search/query-suggest/query-suggest-response';
import {isErrorResponse} from '../../../api/search/search-api-client';
import {
CartSection,
CommerceContextSection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {createAsyncThunk} from '@reduxjs/toolkit';
import {AsyncThunkCommerceOptions} from '../../../api/commerce/commerce-api-client';
import {isErrorResponse} from '../../../api/search/search-api-client';
import {
AsyncThunkCommerceOptions,
isErrorResponse,
} from '../../../api/commerce/commerce-api-client';
import {CommerceQuerySection} from '../../../state/state-sections';
import {logQueryError} from '../../search/search-analytics-actions';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {ArrayValue, StringValue} from '@coveo/bueno';
import {createAction, createAsyncThunk} from '@reduxjs/toolkit';
import {getVisitorID} from '../../api/analytics/coveo-analytics-utils';
import {isErrorResponse} from '../../api/commerce/commerce-api-client';
import {AsyncThunkProductListingOptions} from '../../api/commerce/product-listings/product-listing-api-client';
import {
ProductListingRequest,
ProductListingSuccessResponse,
} from '../../api/commerce/product-listings/product-listing-request';
import {isErrorResponse} from '../../api/search/search-api-client';
import {
CategoryFacetSection,
ConfigurationSection,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createAction} from '../..';
import {createAction} from '@reduxjs/toolkit';
import {validateProductRecommendationPayload} from '../analytics/analytics-utils';
import {ProductRecommendation} from './../../api/search/search/product-recommendation';

Expand Down

0 comments on commit 8f4d68d

Please sign in to comment.