Skip to content

Commit 60acae1

Browse files
authored
Merge pull request #2891 from andrew-from-toronto/add-webapi-cf-adapter-initialized-constants
[Patch][shopify-api] Add Web API and CF Worker adapter intialized constants for aggressive tree-shakers
2 parents 9e5219d + 46ec206 commit 60acae1

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

.changeset/small-moose-dig.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@shopify/shopify-api': patch
3+
---
4+
5+
Add Web API and CF Worker adapter intialized constants for aggressive tree-shakers
6+
7+
For example with the web-api adapter:
8+
9+
```ts
10+
// Instead of just:
11+
import '@shopify/shopify-api/adapters/web-api';
12+
13+
// You can now also import:
14+
import { webApiAdapterInitialized } from '@shopify/shopify-api/adapters/web-api';
15+
import { shopifyApi } from '@shopify/shopify-api';
16+
17+
// And check the adapter is initialized, which forces bundlers to keep the import
18+
if (!webApiAdapterInitialized) {
19+
throw new Error('Failed to initialize web API adapter');
20+
}
21+
```

packages/apps/shopify-api/README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,53 @@ The first thing you need to import is the adapter for your app's runtime. This w
5151
import '@shopify/shopify-api/adapters/node';
5252
```
5353

54+
> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
55+
>
56+
> ```ts
57+
> import { nodeAdapterInitialized } from '@shopify/shopify-api/adapters/node';
58+
> // Optional: Check the adapter loaded successfully
59+
> if (!nodeAdapterInitialized) {
60+
> throw new Error('Failed to initialize Node.js adapter');
61+
> }
62+
> ```
63+
5464
</div><div>CloudFlare Worker
5565
5666
```ts
5767
import '@shopify/shopify-api/adapters/cf-worker';
5868
```
5969
70+
> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
71+
>
72+
> ```ts
73+
> import { cfWorkerAdapterInitialized } from '@shopify/shopify-api/adapters/cf-worker';
74+
> // Optional: Check the adapter loaded successfully
75+
> if (!cfWorkerAdapterInitialized) {
76+
> throw new Error('Failed to initialize Cloudflare Worker adapter');
77+
> }
78+
> ```
79+
6080
</div>
6181
</div><div>Generic runtimes that implement the <a href="https://developer.mozilla.org/en-US/docs/Web/API">Web API</a>
6282
6383
```ts
6484
import '@shopify/shopify-api/adapters/web-api';
6585
```
6686
67-
</div>
68-
6987
> **Note** Some bundlers may aggressively tree-shake the adapter imports in production builds. If you encounter a "Missing adapter implementation" error, you can use the exported constant to ensure the adapter is loaded:
7088
>
7189
> ```ts
72-
> import { nodeAdapterInitialized } from '@shopify/shopify-api/adapters/node';
90+
> import { webApiAdapterInitialized } from '@shopify/shopify-api/adapters/web-api';
7391
> // Optional: Check the adapter loaded successfully
74-
> if (!nodeAdapterInitialized) {
75-
> throw new Error('Failed to initialize Node.js adapter');
92+
> if (!webApiAdapterInitialized) {
93+
> throw new Error('Failed to initialize web API adapter');
7694
> }
7795
> ```
7896
97+
</div>
98+
99+
100+
79101
Next, configure the library - you'll need some values in advance:
80102
81103
- Your app's API key from [Partners dashboard](https://www.shopify.com/partners) (also called `Client ID`)

packages/apps/shopify-api/adapters/cf-worker/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ setAbstractConvertRequestFunc(webApiConvertRequest);
1818
setAbstractConvertResponseFunc(webApiConvertResponse);
1919
setAbstractConvertHeadersFunc(webApiConvertHeaders);
2020
setAbstractRuntimeString(workerRuntimeString);
21+
22+
// Export a marker to prevent tree-shaking
23+
export const cfWorkerAdapterInitialized = true;

packages/apps/shopify-api/adapters/web-api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ setAbstractConvertRequestFunc(webApiConvertRequest);
1818
setAbstractConvertResponseFunc(webApiConvertResponse);
1919
setAbstractConvertHeadersFunc(webApiConvertHeaders);
2020
setAbstractRuntimeString(webApiRuntimeString);
21+
22+
// Export a marker to prevent tree-shaking
23+
export const webApiAdapterInitialized = true;

0 commit comments

Comments
 (0)