@shopify/hydrogen 0.19.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +203 -0
- package/config.d.ts +1 -0
- package/dist/esnext/client.d.ts +1 -0
- package/dist/esnext/client.js +1 -0
- package/dist/esnext/components/CartEstimatedCost/CartEstimatedCost.client.d.ts +1 -1
- package/dist/esnext/components/CartLineImage/CartLineImage.client.d.ts +4 -7
- package/dist/esnext/components/CartLineImage/CartLineImage.client.js +1 -2
- package/dist/esnext/components/CartLinePrice/CartLinePrice.client.d.ts +1 -1
- package/dist/esnext/components/CartProvider/CartProvider.client.d.ts +9 -4
- package/dist/esnext/components/CartProvider/CartProvider.client.js +42 -30
- package/dist/esnext/components/CartProvider/cart-queries.d.ts +10 -9
- package/dist/esnext/components/CartProvider/cart-queries.js +58 -743
- package/dist/esnext/components/CartProvider/hooks.client.js +4 -2
- package/dist/esnext/components/CartProvider/types.d.ts +2 -0
- package/dist/esnext/components/Image/Image.d.ts +77 -39
- package/dist/esnext/components/Image/Image.js +57 -54
- package/dist/esnext/components/Image/index.d.ts +1 -0
- package/dist/esnext/components/MediaFile/MediaFile.d.ts +2 -2
- package/dist/esnext/components/MediaFile/MediaFile.js +5 -4
- package/dist/esnext/components/Money/Money.client.d.ts +11 -5
- package/dist/esnext/components/Money/Money.client.js +16 -3
- package/dist/esnext/components/ProductPrice/ProductPrice.client.d.ts +1 -2
- package/dist/esnext/components/ProductPrice/ProductPrice.client.js +1 -2
- package/dist/esnext/components/Seo/NoIndexSeo.client.d.ts +3 -0
- package/dist/esnext/components/Seo/NoIndexSeo.client.js +10 -0
- package/dist/esnext/components/Seo/Seo.client.d.ts +4 -0
- package/dist/esnext/components/Seo/Seo.client.js +3 -0
- package/dist/esnext/components/Video/Video.d.ts +3 -3
- package/dist/esnext/components/Video/Video.js +7 -4
- package/dist/esnext/components/index.d.ts +0 -3
- package/dist/esnext/components/index.js +0 -3
- package/dist/esnext/config.d.ts +3 -3
- package/dist/esnext/entry-client.js +0 -3
- package/dist/esnext/entry-server.d.ts +14 -2
- package/dist/esnext/entry-server.js +97 -73
- package/dist/esnext/foundation/Analytics/Analytics.client.js +1 -1
- package/dist/esnext/foundation/Analytics/ClientAnalytics.js +2 -2
- package/dist/esnext/foundation/Analytics/connectors/PerformanceMetrics/PerformanceMetrics.client.js +2 -2
- package/dist/esnext/foundation/Analytics/index.d.ts +0 -1
- package/dist/esnext/foundation/Analytics/index.js +0 -1
- package/dist/esnext/foundation/FileRoutes/FileRoutes.server.js +5 -10
- package/dist/esnext/foundation/Router/BrowserRouter.client.d.ts +2 -1
- package/dist/esnext/foundation/Router/BrowserRouter.client.js +1 -1
- package/dist/esnext/foundation/ServerPropsProvider/ServerPropsProvider.js +0 -2
- package/dist/esnext/foundation/ServerRequestProvider/ServerRequestProvider.js +18 -3
- package/dist/esnext/foundation/ShopifyProvider/ShopifyProvider.server.js +0 -1
- package/dist/esnext/foundation/fetchSync/server/fetchSync.d.ts +1 -1
- package/dist/esnext/foundation/fetchSync/server/fetchSync.js +1 -1
- package/dist/esnext/foundation/useQuery/hooks.js +8 -9
- package/dist/esnext/foundation/useRouteParams/RouteParamsProvider.client.d.ts +2 -1
- package/dist/esnext/foundation/useRouteParams/RouteParamsProvider.client.js +1 -1
- package/dist/esnext/framework/Hydration/Html.js +3 -1
- package/dist/esnext/framework/Hydration/ServerComponentRequest.server.d.ts +2 -2
- package/dist/esnext/framework/Hydration/ServerComponentResponse.server.d.ts +1 -10
- package/dist/esnext/framework/Hydration/ServerComponentResponse.server.js +2 -20
- package/dist/esnext/framework/Hydration/rsc.js +55 -7
- package/dist/esnext/framework/cache/in-memory.js +0 -6
- package/dist/esnext/framework/cache-sub-request.d.ts +17 -0
- package/dist/esnext/framework/cache-sub-request.js +64 -0
- package/dist/esnext/framework/cache.d.ts +6 -6
- package/dist/esnext/framework/cache.js +36 -33
- package/dist/esnext/framework/middleware.js +1 -15
- package/dist/esnext/framework/plugin.js +7 -30
- package/dist/esnext/framework/plugins/vite-plugin-client-imports.d.ts +2 -0
- package/dist/esnext/framework/plugins/vite-plugin-client-imports.js +25 -0
- package/dist/esnext/framework/plugins/vite-plugin-css-modules-rsc.d.ts +1 -1
- package/dist/esnext/framework/plugins/vite-plugin-css-modules-rsc.js +73 -3
- package/dist/esnext/framework/plugins/vite-plugin-hydration-auto-import.js +1 -4
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-client-middleware.d.ts +1 -1
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-config.js +7 -5
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-middleware.d.ts +1 -1
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-middleware.js +8 -50
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-rsc.d.ts +1 -0
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-rsc.js +30 -0
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-virtual-files.d.ts +7 -0
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-virtual-files.js +99 -0
- package/dist/esnext/framework/plugins/vite-plugin-platform-entry.js +4 -4
- package/dist/esnext/framework/plugins/vite-plugin-ssr-interop.js +6 -3
- package/dist/esnext/framework/viteception.d.ts +4 -0
- package/dist/esnext/framework/viteception.js +15 -0
- package/dist/esnext/hooks/useShopQuery/hooks.d.ts +1 -2
- package/dist/esnext/hooks/useShopQuery/hooks.js +0 -1
- package/dist/esnext/index.d.ts +24 -9
- package/dist/esnext/index.js +24 -13
- package/dist/esnext/storefront-api-types.d.ts +6 -4
- package/dist/esnext/storefront-api-types.js +5 -3
- package/dist/esnext/streaming.server.d.ts +3 -1
- package/dist/esnext/types.d.ts +13 -5
- package/dist/esnext/utilities/apiRoutes.d.ts +4 -5
- package/dist/esnext/utilities/apiRoutes.js +2 -8
- package/dist/esnext/utilities/fetch.d.ts +1 -2
- package/dist/esnext/utilities/fetch.js +1 -3
- package/dist/esnext/utilities/graphql-tag.d.ts +1 -0
- package/dist/esnext/utilities/graphql-tag.js +6 -0
- package/dist/esnext/utilities/graphql-tracker.d.ts +1 -1
- package/dist/esnext/utilities/graphql-tracker.js +4 -0
- package/dist/esnext/utilities/html-encoding.d.ts +1 -0
- package/dist/esnext/utilities/html-encoding.js +8 -0
- package/dist/esnext/utilities/image_size.d.ts +4 -22
- package/dist/esnext/utilities/image_size.js +15 -33
- package/dist/esnext/utilities/index.d.ts +2 -1
- package/dist/esnext/utilities/index.js +2 -1
- package/dist/esnext/utilities/log/log-cache-api-status.js +5 -1
- package/dist/esnext/version.d.ts +1 -1
- package/dist/esnext/version.js +1 -1
- package/dist/node/components/Image/Image.d.ts +84 -0
- package/dist/node/components/Image/Image.js +89 -0
- package/dist/node/components/Image/index.d.ts +2 -0
- package/dist/node/components/Image/index.js +5 -0
- package/dist/node/entry-server.d.ts +14 -2
- package/dist/node/entry-server.js +97 -73
- package/dist/node/foundation/Analytics/Analytics.client.js +6 -6
- package/dist/node/foundation/Analytics/ClientAnalytics.js +2 -2
- package/dist/node/foundation/Router/BrowserRouter.client.d.ts +2 -1
- package/dist/node/foundation/Router/BrowserRouter.client.js +1 -1
- package/dist/node/foundation/ServerPropsProvider/ServerPropsProvider.js +0 -2
- package/dist/node/foundation/ServerRequestProvider/ServerRequestProvider.js +18 -3
- package/dist/node/framework/Hydration/Html.js +3 -1
- package/dist/node/framework/Hydration/ServerComponentRequest.server.d.ts +2 -2
- package/dist/node/framework/Hydration/ServerComponentResponse.server.d.ts +1 -10
- package/dist/node/framework/Hydration/ServerComponentResponse.server.js +2 -20
- package/dist/node/framework/Hydration/rsc.js +55 -7
- package/dist/node/framework/cache/in-memory.js +0 -6
- package/dist/node/framework/cache-sub-request.d.ts +17 -0
- package/dist/node/framework/cache-sub-request.js +95 -0
- package/dist/node/framework/cache.d.ts +6 -6
- package/dist/node/framework/cache.js +38 -35
- package/dist/node/framework/middleware.js +1 -15
- package/dist/node/framework/plugin.js +7 -53
- package/dist/node/framework/plugins/vite-plugin-client-imports.d.ts +2 -0
- package/dist/node/framework/plugins/vite-plugin-client-imports.js +28 -0
- package/dist/node/framework/plugins/vite-plugin-css-modules-rsc.d.ts +1 -1
- package/dist/node/framework/plugins/vite-plugin-css-modules-rsc.js +76 -3
- package/dist/node/framework/plugins/vite-plugin-hydration-auto-import.js +1 -4
- package/dist/node/framework/plugins/vite-plugin-hydrogen-client-middleware.d.ts +1 -1
- package/dist/node/framework/plugins/vite-plugin-hydrogen-config.js +7 -5
- package/dist/node/framework/plugins/vite-plugin-hydrogen-middleware.d.ts +1 -1
- package/dist/node/framework/plugins/vite-plugin-hydrogen-middleware.js +7 -49
- package/dist/node/framework/plugins/vite-plugin-hydrogen-rsc.d.ts +1 -0
- package/dist/node/framework/plugins/vite-plugin-hydrogen-rsc.js +36 -0
- package/dist/node/framework/plugins/vite-plugin-hydrogen-virtual-files.d.ts +7 -0
- package/dist/node/framework/plugins/vite-plugin-hydrogen-virtual-files.js +105 -0
- package/dist/node/framework/plugins/vite-plugin-platform-entry.js +4 -4
- package/dist/node/framework/plugins/vite-plugin-ssr-interop.js +6 -3
- package/dist/node/framework/viteception.d.ts +4 -0
- package/dist/node/framework/viteception.js +19 -0
- package/dist/node/storefront-api-types.d.ts +6 -4
- package/dist/node/storefront-api-types.js +5 -3
- package/dist/node/streaming.server.d.ts +3 -1
- package/dist/node/types.d.ts +13 -5
- package/dist/node/utilities/apiRoutes.d.ts +4 -5
- package/dist/node/utilities/apiRoutes.js +2 -8
- package/dist/node/utilities/fetch.d.ts +1 -2
- package/dist/node/utilities/fetch.js +1 -3
- package/dist/node/utilities/html-encoding.d.ts +1 -0
- package/dist/node/utilities/html-encoding.js +12 -0
- package/dist/node/utilities/image_size.d.ts +4 -22
- package/dist/node/utilities/image_size.js +16 -58
- package/dist/node/utilities/index.d.ts +2 -1
- package/dist/node/utilities/index.js +3 -2
- package/dist/node/utilities/log/log-cache-api-status.js +5 -1
- package/dist/node/version.d.ts +1 -1
- package/dist/node/version.js +1 -1
- package/entry-server.d.ts +1 -1
- package/package.json +6 -5
- package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-plugin.js +214 -28
- package/vendor/react-server-dom-vite/esm/react-server-dom-vite-client-proxy.js +2 -0
- package/vendor/react-server-dom-vite/esm/react-server-dom-vite-plugin.js +214 -28
- package/vendor/react-server-dom-vite/package.json +2 -1
- package/dist/esnext/components/ProductDescription/ProductDescription.client.d.ts +0 -13
- package/dist/esnext/components/ProductDescription/ProductDescription.client.js +0 -16
- package/dist/esnext/components/ProductDescription/index.d.ts +0 -1
- package/dist/esnext/components/ProductDescription/index.js +0 -1
- package/dist/esnext/components/ProductMetafield/ProductMetafield.client.d.ts +0 -21
- package/dist/esnext/components/ProductMetafield/ProductMetafield.client.js +0 -42
- package/dist/esnext/components/ProductMetafield/index.d.ts +0 -2
- package/dist/esnext/components/ProductMetafield/index.js +0 -1
- package/dist/esnext/components/ProductTitle/ProductTitle.client.d.ts +0 -13
- package/dist/esnext/components/ProductTitle/ProductTitle.client.js +0 -16
- package/dist/esnext/components/ProductTitle/index.d.ts +0 -1
- package/dist/esnext/components/ProductTitle/index.js +0 -1
- package/dist/esnext/components/UnitPrice/UnitPrice.client.d.ts +0 -15
- package/dist/esnext/components/UnitPrice/UnitPrice.client.js +0 -22
- package/dist/esnext/components/UnitPrice/index.d.ts +0 -1
- package/dist/esnext/components/UnitPrice/index.js +0 -1
- package/dist/esnext/utilities/findRoutePrefix.d.ts +0 -1
- package/dist/esnext/utilities/findRoutePrefix.js +0 -17
- package/dist/node/foundation/Analytics/index.d.ts +0 -2
- package/dist/node/foundation/Analytics/index.js +0 -7
- package/dist/node/utilities/findRoutePrefix.d.ts +0 -1
- package/dist/node/utilities/findRoutePrefix.js +0 -21
|
@@ -3,14 +3,64 @@
|
|
|
3
3
|
// so it is considered ESM instead of CJS?
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.useRefresh = exports.useServerResponse = void 0;
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
const react_1 = require("react");
|
|
8
6
|
const react_server_dom_vite_1 = require("@shopify/hydrogen/vendor/react-server-dom-vite");
|
|
9
7
|
const constants_1 = require("../../constants");
|
|
10
8
|
let rscReader;
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
// Hydrate an SSR response from <meta> tags placed in the DOM.
|
|
10
|
+
const flightChunks = [];
|
|
11
|
+
const FLIGHT_ATTRIBUTE = 'data-flight';
|
|
12
|
+
function addElementToFlightChunks(el) {
|
|
13
|
+
// We don't need to decode, because `.getAttribute` already decodes
|
|
14
|
+
const chunk = el.getAttribute(FLIGHT_ATTRIBUTE);
|
|
15
|
+
if (chunk) {
|
|
16
|
+
flightChunks.push(chunk);
|
|
17
|
+
}
|
|
13
18
|
}
|
|
19
|
+
// Get initial payload
|
|
20
|
+
document
|
|
21
|
+
.querySelectorAll('[' + FLIGHT_ATTRIBUTE + ']')
|
|
22
|
+
.forEach(addElementToFlightChunks);
|
|
23
|
+
// Create a mutation observer on the document to detect when new
|
|
24
|
+
// <meta data-flight> tags are added, and add them to the array.
|
|
25
|
+
const observer = new MutationObserver((mutations) => {
|
|
26
|
+
mutations.forEach((mutation) => {
|
|
27
|
+
mutation.addedNodes.forEach((node) => {
|
|
28
|
+
if (node instanceof HTMLElement &&
|
|
29
|
+
node.tagName === 'META' &&
|
|
30
|
+
node.hasAttribute(FLIGHT_ATTRIBUTE)) {
|
|
31
|
+
addElementToFlightChunks(node);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
observer.observe(document.documentElement, {
|
|
37
|
+
childList: true,
|
|
38
|
+
subtree: true,
|
|
39
|
+
});
|
|
40
|
+
if (flightChunks.length > 0) {
|
|
41
|
+
const contentLoaded = new Promise((resolve) => document.addEventListener('DOMContentLoaded', resolve));
|
|
42
|
+
try {
|
|
43
|
+
rscReader = new ReadableStream({
|
|
44
|
+
start(controller) {
|
|
45
|
+
const encoder = new TextEncoder();
|
|
46
|
+
const write = (chunk) => {
|
|
47
|
+
controller.enqueue(encoder.encode(chunk));
|
|
48
|
+
return 0;
|
|
49
|
+
};
|
|
50
|
+
flightChunks.forEach(write);
|
|
51
|
+
flightChunks.push = write;
|
|
52
|
+
contentLoaded.then(() => {
|
|
53
|
+
controller.close();
|
|
54
|
+
observer.disconnect();
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
catch (_) {
|
|
60
|
+
// Old browser, will try a new hydration request later
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const cache = new Map();
|
|
14
64
|
/**
|
|
15
65
|
* Much of this is borrowed from React's demo implementation:
|
|
16
66
|
* @see https://github.com/reactjs/server-components-demo/blob/main/src/Cache.client.js
|
|
@@ -19,7 +69,6 @@ function createResponseCache() {
|
|
|
19
69
|
*/
|
|
20
70
|
function useServerResponse(state) {
|
|
21
71
|
const key = JSON.stringify(state);
|
|
22
|
-
const cache = (0, react_1.unstable_getCacheForType)(createResponseCache);
|
|
23
72
|
let response = cache.get(key);
|
|
24
73
|
if (response) {
|
|
25
74
|
return response;
|
|
@@ -49,7 +98,6 @@ function useServerResponse(state) {
|
|
|
49
98
|
}
|
|
50
99
|
exports.useServerResponse = useServerResponse;
|
|
51
100
|
function useRefresh() {
|
|
52
|
-
|
|
53
|
-
refreshCache();
|
|
101
|
+
cache.clear();
|
|
54
102
|
}
|
|
55
103
|
exports.useRefresh = useRefresh;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.InMemoryCache = void 0;
|
|
4
|
-
const log_1 = require("../../utilities/log");
|
|
5
4
|
/**
|
|
6
5
|
* This is an in-memory implementation of `Cache` that *barely*
|
|
7
6
|
* works and is only meant to be used during development.
|
|
@@ -11,7 +10,6 @@ class InMemoryCache {
|
|
|
11
10
|
this.store = new Map();
|
|
12
11
|
}
|
|
13
12
|
put(request, response) {
|
|
14
|
-
(0, log_1.logCacheApiStatus)('PUT-dev', request.url);
|
|
15
13
|
this.store.set(request.url, {
|
|
16
14
|
value: response,
|
|
17
15
|
date: new Date(),
|
|
@@ -21,7 +19,6 @@ class InMemoryCache {
|
|
|
21
19
|
var _a, _b;
|
|
22
20
|
const match = this.store.get(request.url);
|
|
23
21
|
if (!match) {
|
|
24
|
-
(0, log_1.logCacheApiStatus)('MISS-dev', request.url);
|
|
25
22
|
return;
|
|
26
23
|
}
|
|
27
24
|
const { value, date } = match;
|
|
@@ -31,7 +28,6 @@ class InMemoryCache {
|
|
|
31
28
|
const age = (new Date().valueOf() - date.valueOf()) / 1000;
|
|
32
29
|
const isMiss = age > maxAge + swr;
|
|
33
30
|
if (isMiss) {
|
|
34
|
-
(0, log_1.logCacheApiStatus)('MISS-dev', request.url);
|
|
35
31
|
this.store.delete(request.url);
|
|
36
32
|
return;
|
|
37
33
|
}
|
|
@@ -39,7 +35,6 @@ class InMemoryCache {
|
|
|
39
35
|
const headers = new Headers(value.headers);
|
|
40
36
|
headers.set('cache', isStale ? 'STALE' : 'HIT');
|
|
41
37
|
headers.set('date', date.toUTCString());
|
|
42
|
-
(0, log_1.logCacheApiStatus)(`${headers.get('cache')}-dev`, request.url);
|
|
43
38
|
const response = new Response(value.body, {
|
|
44
39
|
headers,
|
|
45
40
|
});
|
|
@@ -47,7 +42,6 @@ class InMemoryCache {
|
|
|
47
42
|
}
|
|
48
43
|
delete(request) {
|
|
49
44
|
this.store.delete(request.url);
|
|
50
|
-
(0, log_1.logCacheApiStatus)('DELETE-dev', request.url);
|
|
51
45
|
}
|
|
52
46
|
keys(request) {
|
|
53
47
|
const cacheKeys = [];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { QueryKey, CachingStrategy } from '../types';
|
|
2
|
+
export declare function generateSubRequestCacheControlHeader(userCacheOptions?: CachingStrategy): string;
|
|
3
|
+
/**
|
|
4
|
+
* Get an item from the cache. If a match is found, returns a tuple
|
|
5
|
+
* containing the `JSON.parse` version of the response as well
|
|
6
|
+
* as the response itself so it can be checked for staleness.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getItemFromCache(key: QueryKey): Promise<undefined | [any, Response]>;
|
|
9
|
+
/**
|
|
10
|
+
* Put an item into the cache.
|
|
11
|
+
*/
|
|
12
|
+
export declare function setItemInCache(key: QueryKey, value: any, userCacheOptions?: CachingStrategy): Promise<void>;
|
|
13
|
+
export declare function deleteItemFromCache(key: QueryKey): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Manually check the response to see if it's stale.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isStale(key: QueryKey, response: Response): boolean;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.isStale = exports.deleteItemFromCache = exports.setItemInCache = exports.getItemFromCache = exports.generateSubRequestCacheControlHeader = void 0;
|
|
27
|
+
const runtime_1 = require("./runtime");
|
|
28
|
+
const hash_1 = require("../utilities/hash");
|
|
29
|
+
const CacheApi = __importStar(require("./cache"));
|
|
30
|
+
const CachingStrategy_1 = require("./CachingStrategy");
|
|
31
|
+
/**
|
|
32
|
+
* Wrapper Cache functions for sub queries
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* Cache API is weird. We just need a full URL, so we make one up.
|
|
36
|
+
*/
|
|
37
|
+
function getKeyUrl(key) {
|
|
38
|
+
return `https://shopify.dev/?${key}`;
|
|
39
|
+
}
|
|
40
|
+
function getCacheOption(userCacheOptions) {
|
|
41
|
+
return userCacheOptions || (0, CachingStrategy_1.CacheSeconds)();
|
|
42
|
+
}
|
|
43
|
+
function generateSubRequestCacheControlHeader(userCacheOptions) {
|
|
44
|
+
return CacheApi.generateDefaultCacheControlHeader(getCacheOption(userCacheOptions));
|
|
45
|
+
}
|
|
46
|
+
exports.generateSubRequestCacheControlHeader = generateSubRequestCacheControlHeader;
|
|
47
|
+
/**
|
|
48
|
+
* Get an item from the cache. If a match is found, returns a tuple
|
|
49
|
+
* containing the `JSON.parse` version of the response as well
|
|
50
|
+
* as the response itself so it can be checked for staleness.
|
|
51
|
+
*/
|
|
52
|
+
async function getItemFromCache(key) {
|
|
53
|
+
const cache = (0, runtime_1.getCache)();
|
|
54
|
+
if (!cache) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const url = getKeyUrl((0, hash_1.hashKey)(key));
|
|
58
|
+
const request = new Request(url);
|
|
59
|
+
const response = await CacheApi.getItemFromCache(request);
|
|
60
|
+
if (!response) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
return [await response.json(), response];
|
|
64
|
+
}
|
|
65
|
+
exports.getItemFromCache = getItemFromCache;
|
|
66
|
+
/**
|
|
67
|
+
* Put an item into the cache.
|
|
68
|
+
*/
|
|
69
|
+
async function setItemInCache(key, value, userCacheOptions) {
|
|
70
|
+
const cache = (0, runtime_1.getCache)();
|
|
71
|
+
if (!cache) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const url = getKeyUrl((0, hash_1.hashKey)(key));
|
|
75
|
+
const request = new Request(url);
|
|
76
|
+
const response = new Response(JSON.stringify(value));
|
|
77
|
+
await CacheApi.setItemInCache(request, response, getCacheOption(userCacheOptions));
|
|
78
|
+
}
|
|
79
|
+
exports.setItemInCache = setItemInCache;
|
|
80
|
+
async function deleteItemFromCache(key) {
|
|
81
|
+
const cache = (0, runtime_1.getCache)();
|
|
82
|
+
if (!cache)
|
|
83
|
+
return;
|
|
84
|
+
const url = getKeyUrl((0, hash_1.hashKey)(key));
|
|
85
|
+
const request = new Request(url);
|
|
86
|
+
await CacheApi.deleteItemFromCache(request);
|
|
87
|
+
}
|
|
88
|
+
exports.deleteItemFromCache = deleteItemFromCache;
|
|
89
|
+
/**
|
|
90
|
+
* Manually check the response to see if it's stale.
|
|
91
|
+
*/
|
|
92
|
+
function isStale(key, response) {
|
|
93
|
+
return CacheApi.isStale(new Request(getKeyUrl((0, hash_1.hashKey)(key))), response);
|
|
94
|
+
}
|
|
95
|
+
exports.isStale = isStale;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function
|
|
1
|
+
import type { CachingStrategy } from '../types';
|
|
2
|
+
export declare function generateDefaultCacheControlHeader(userCacheOptions?: CachingStrategy): string;
|
|
3
3
|
/**
|
|
4
4
|
* Get an item from the cache. If a match is found, returns a tuple
|
|
5
5
|
* containing the `JSON.parse` version of the response as well
|
|
6
6
|
* as the response itself so it can be checked for staleness.
|
|
7
7
|
*/
|
|
8
|
-
export declare function getItemFromCache(
|
|
8
|
+
export declare function getItemFromCache(request: Request): Promise<Response | undefined>;
|
|
9
9
|
/**
|
|
10
10
|
* Put an item into the cache.
|
|
11
11
|
*/
|
|
12
|
-
export declare function setItemInCache(
|
|
13
|
-
export declare function deleteItemFromCache(
|
|
12
|
+
export declare function setItemInCache(request: Request, response: Response, userCacheOptions: CachingStrategy): Promise<void>;
|
|
13
|
+
export declare function deleteItemFromCache(request: Request): Promise<void>;
|
|
14
14
|
/**
|
|
15
15
|
* Manually check the response to see if it's stale.
|
|
16
16
|
*/
|
|
17
|
-
export declare function isStale(
|
|
17
|
+
export declare function isStale(request: Request, response: Response): boolean;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isStale = exports.deleteItemFromCache = exports.setItemInCache = exports.getItemFromCache = exports.
|
|
3
|
+
exports.isStale = exports.deleteItemFromCache = exports.setItemInCache = exports.getItemFromCache = exports.generateDefaultCacheControlHeader = void 0;
|
|
4
4
|
const runtime_1 = require("./runtime");
|
|
5
5
|
const CachingStrategy_1 = require("../framework/CachingStrategy");
|
|
6
|
-
const hash_1 = require("../utilities/hash");
|
|
7
6
|
const log_1 = require("../utilities/log");
|
|
8
7
|
function getCacheControlSetting(userCacheOptions, options) {
|
|
9
8
|
if (userCacheOptions && options) {
|
|
@@ -16,47 +15,37 @@ function getCacheControlSetting(userCacheOptions, options) {
|
|
|
16
15
|
return userCacheOptions || (0, CachingStrategy_1.CacheSeconds)();
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
|
-
function
|
|
18
|
+
function generateDefaultCacheControlHeader(userCacheOptions) {
|
|
20
19
|
return (0, CachingStrategy_1.generateCacheControlHeader)(getCacheControlSetting(userCacheOptions));
|
|
21
20
|
}
|
|
22
|
-
exports.
|
|
23
|
-
/**
|
|
24
|
-
* Cache API is weird. We just need a full URL, so we make one up.
|
|
25
|
-
*/
|
|
26
|
-
function getKeyUrl(key) {
|
|
27
|
-
return `https://shopify.dev/?${key}`;
|
|
28
|
-
}
|
|
21
|
+
exports.generateDefaultCacheControlHeader = generateDefaultCacheControlHeader;
|
|
29
22
|
/**
|
|
30
23
|
* Get an item from the cache. If a match is found, returns a tuple
|
|
31
24
|
* containing the `JSON.parse` version of the response as well
|
|
32
25
|
* as the response itself so it can be checked for staleness.
|
|
33
26
|
*/
|
|
34
|
-
async function getItemFromCache(
|
|
27
|
+
async function getItemFromCache(request) {
|
|
35
28
|
const cache = (0, runtime_1.getCache)();
|
|
36
29
|
if (!cache) {
|
|
37
30
|
return;
|
|
38
31
|
}
|
|
39
|
-
const url = getKeyUrl((0, hash_1.hashKey)(key));
|
|
40
|
-
const request = new Request(url);
|
|
41
32
|
const response = await cache.match(request);
|
|
42
33
|
if (!response) {
|
|
43
|
-
(0, log_1.logCacheApiStatus)('MISS', url);
|
|
34
|
+
(0, log_1.logCacheApiStatus)('MISS', request.url);
|
|
44
35
|
return;
|
|
45
36
|
}
|
|
46
|
-
(0, log_1.logCacheApiStatus)('HIT', url);
|
|
47
|
-
return
|
|
37
|
+
(0, log_1.logCacheApiStatus)('HIT', request.url);
|
|
38
|
+
return response;
|
|
48
39
|
}
|
|
49
40
|
exports.getItemFromCache = getItemFromCache;
|
|
50
41
|
/**
|
|
51
42
|
* Put an item into the cache.
|
|
52
43
|
*/
|
|
53
|
-
async function setItemInCache(
|
|
44
|
+
async function setItemInCache(request, response, userCacheOptions) {
|
|
54
45
|
const cache = (0, runtime_1.getCache)();
|
|
55
46
|
if (!cache) {
|
|
56
47
|
return;
|
|
57
48
|
}
|
|
58
|
-
const url = getKeyUrl((0, hash_1.hashKey)(key));
|
|
59
|
-
const request = new Request(url);
|
|
60
49
|
/**
|
|
61
50
|
* We are manually managing staled request by adding this workaround.
|
|
62
51
|
* Why? cache control header support is dependent on hosting platform
|
|
@@ -96,37 +85,51 @@ async function setItemInCache(key, value, userCacheOptions) {
|
|
|
96
85
|
* `isStale` function will use the above information to test for stale-ness of a cached response
|
|
97
86
|
*/
|
|
98
87
|
const cacheControl = getCacheControlSetting(userCacheOptions);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
88
|
+
// The padded cache-control to mimic stale-while-revalidate
|
|
89
|
+
request.headers.set('cache-control', generateDefaultCacheControlHeader(getCacheControlSetting(cacheControl, {
|
|
90
|
+
maxAge: (cacheControl.maxAge || 0) + (cacheControl.staleWhileRevalidate || 0),
|
|
91
|
+
})));
|
|
92
|
+
// The cache-control we want to set on response
|
|
93
|
+
const cacheControlString = generateDefaultCacheControlHeader(getCacheControlSetting(cacheControl));
|
|
94
|
+
// CF will override cache-control, so we need to keep a
|
|
95
|
+
// non-modified real-cache-control
|
|
96
|
+
response.headers.set('cache-control', cacheControlString);
|
|
97
|
+
response.headers.set('real-cache-control', cacheControlString);
|
|
98
|
+
response.headers.set('cache-put-date', new Date().toUTCString());
|
|
99
|
+
(0, log_1.logCacheApiStatus)('PUT', request.url);
|
|
107
100
|
await cache.put(request, response);
|
|
108
101
|
}
|
|
109
102
|
exports.setItemInCache = setItemInCache;
|
|
110
|
-
async function deleteItemFromCache(
|
|
103
|
+
async function deleteItemFromCache(request) {
|
|
111
104
|
const cache = (0, runtime_1.getCache)();
|
|
112
105
|
if (!cache)
|
|
113
106
|
return;
|
|
114
|
-
|
|
115
|
-
const request = new Request(url);
|
|
116
|
-
(0, log_1.logCacheApiStatus)('DELETE', url);
|
|
107
|
+
(0, log_1.logCacheApiStatus)('DELETE', request.url);
|
|
117
108
|
await cache.delete(request);
|
|
118
109
|
}
|
|
119
110
|
exports.deleteItemFromCache = deleteItemFromCache;
|
|
120
111
|
/**
|
|
121
112
|
* Manually check the response to see if it's stale.
|
|
122
113
|
*/
|
|
123
|
-
function isStale(
|
|
124
|
-
const responseMaxAge = getCacheControlSetting(userCacheOptions).maxAge || 0;
|
|
114
|
+
function isStale(request, response) {
|
|
125
115
|
const responseDate = response.headers.get('cache-put-date');
|
|
126
|
-
|
|
116
|
+
const cacheControl = response.headers.get('real-cache-control');
|
|
117
|
+
let responseMaxAge = 0;
|
|
118
|
+
if (cacheControl) {
|
|
119
|
+
const maxAgeMatch = cacheControl.match(/max-age=(\d*)/);
|
|
120
|
+
if (maxAgeMatch && maxAgeMatch.length > 1) {
|
|
121
|
+
responseMaxAge = parseFloat(maxAgeMatch[1]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!responseDate) {
|
|
127
125
|
return false;
|
|
126
|
+
}
|
|
128
127
|
const ageInMs = new Date().valueOf() - new Date(responseDate).valueOf();
|
|
129
128
|
const age = ageInMs / 1000;
|
|
130
|
-
|
|
129
|
+
const result = age > responseMaxAge;
|
|
130
|
+
if (result) {
|
|
131
|
+
(0, log_1.logCacheApiStatus)('STALE', request.url);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
131
134
|
}
|
|
132
135
|
exports.isStale = isStale;
|
|
@@ -75,26 +75,12 @@ function hydrogenMiddleware({ dev, cache, indexTemplate, getServerEntrypoint, de
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
entrypointError = null;
|
|
78
|
-
|
|
78
|
+
await handleRequest(request, {
|
|
79
79
|
dev,
|
|
80
80
|
cache,
|
|
81
81
|
indexTemplate,
|
|
82
82
|
streamableResponse: response,
|
|
83
83
|
});
|
|
84
|
-
/**
|
|
85
|
-
* If a `Response` was returned, that means it was not streamed.
|
|
86
|
-
* Convert the response into a proper Node.js response.
|
|
87
|
-
*/
|
|
88
|
-
if (eventResponse) {
|
|
89
|
-
eventResponse.headers.forEach((value, key) => {
|
|
90
|
-
response.setHeader(key, value);
|
|
91
|
-
});
|
|
92
|
-
response.statusCode = eventResponse.status;
|
|
93
|
-
if (eventResponse.body) {
|
|
94
|
-
response.write(eventResponse.body);
|
|
95
|
-
}
|
|
96
|
-
response.end();
|
|
97
|
-
}
|
|
98
84
|
}
|
|
99
85
|
catch (e) {
|
|
100
86
|
if (dev && devServer)
|
|
@@ -1,80 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
28
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
6
|
const vite_plugin_hydrogen_config_1 = __importDefault(require("./plugins/vite-plugin-hydrogen-config"));
|
|
30
|
-
const vite_plugin_hydrogen_middleware_1 =
|
|
7
|
+
const vite_plugin_hydrogen_middleware_1 = __importDefault(require("./plugins/vite-plugin-hydrogen-middleware"));
|
|
31
8
|
const vite_plugin_hydrogen_client_middleware_1 = __importDefault(require("./plugins/vite-plugin-hydrogen-client-middleware"));
|
|
9
|
+
const vite_plugin_hydrogen_virtual_files_1 = __importDefault(require("./plugins/vite-plugin-hydrogen-virtual-files"));
|
|
32
10
|
const vite_plugin_platform_entry_1 = __importDefault(require("./plugins/vite-plugin-platform-entry"));
|
|
33
|
-
|
|
34
|
-
const plugin_1 = __importDefault(require("@shopify/hydrogen/vendor/react-server-dom-vite/plugin"));
|
|
11
|
+
const vite_plugin_hydrogen_rsc_1 = __importDefault(require("./plugins/vite-plugin-hydrogen-rsc"));
|
|
35
12
|
const vite_plugin_ssr_interop_1 = __importDefault(require("./plugins/vite-plugin-ssr-interop"));
|
|
36
13
|
const vite_plugin_purge_query_cache_1 = __importDefault(require("./plugins/vite-plugin-purge-query-cache"));
|
|
37
14
|
const vite_plugin_hydration_auto_import_1 = __importDefault(require("./plugins/vite-plugin-hydration-auto-import"));
|
|
38
15
|
const vite_plugin_inspect_1 = __importDefault(require("vite-plugin-inspect"));
|
|
39
16
|
const plugin_react_1 = __importDefault(require("@vitejs/plugin-react"));
|
|
40
|
-
const path_1 = __importDefault(require("path"));
|
|
41
17
|
const vite_plugin_css_modules_rsc_1 = __importDefault(require("./plugins/vite-plugin-css-modules-rsc"));
|
|
18
|
+
const vite_plugin_client_imports_1 = __importDefault(require("./plugins/vite-plugin-client-imports"));
|
|
42
19
|
exports.default = (pluginOptions = {}) => {
|
|
43
|
-
let hydrogenUiPath;
|
|
44
|
-
try {
|
|
45
|
-
hydrogenUiPath = path_1.default.join(
|
|
46
|
-
// eslint-disable-next-line node/no-missing-require
|
|
47
|
-
path_1.default.dirname(require.resolve('@shopify/hydrogen-ui/client')));
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
// hydrogen-ui isn't installed, so don't worry about it
|
|
51
|
-
}
|
|
52
20
|
return [
|
|
53
21
|
process.env.VITE_INSPECT && (0, vite_plugin_inspect_1.default)(),
|
|
54
22
|
(0, vite_plugin_hydrogen_config_1.default)(),
|
|
55
23
|
(0, vite_plugin_hydrogen_client_middleware_1.default)(),
|
|
24
|
+
(0, vite_plugin_client_imports_1.default)(),
|
|
56
25
|
(0, vite_plugin_hydrogen_middleware_1.default)(pluginOptions),
|
|
26
|
+
(0, vite_plugin_hydrogen_virtual_files_1.default)(pluginOptions),
|
|
57
27
|
(0, plugin_react_1.default)(),
|
|
58
28
|
(0, vite_plugin_hydration_auto_import_1.default)(),
|
|
59
29
|
(0, vite_plugin_ssr_interop_1.default)(),
|
|
60
30
|
(0, vite_plugin_css_modules_rsc_1.default)(),
|
|
61
|
-
(0,
|
|
62
|
-
clientComponentPaths: [
|
|
63
|
-
path_1.default.join(path_1.default.dirname(require.resolve('@shopify/hydrogen/package.json'))),
|
|
64
|
-
...[hydrogenUiPath].filter(Boolean),
|
|
65
|
-
],
|
|
66
|
-
isServerComponentImporterAllowed(importer, source) {
|
|
67
|
-
// Always allow the entry server (e.g. App.server.jsx) to be imported
|
|
68
|
-
// in other files such as worker.js or server.js.
|
|
69
|
-
const entryServer = process.env.HYDROGEN_SERVER_ENTRY || vite_plugin_hydrogen_middleware_1.HYDROGEN_DEFAULT_SERVER_ENTRY;
|
|
70
|
-
return (source.includes(entryServer) ||
|
|
71
|
-
// TODO update this after handleEvent is replaced with handleRequest
|
|
72
|
-
/(index|entry-server|hydrogen\.config)\.[jt]s/.test(importer) ||
|
|
73
|
-
// Support importing server components for testing
|
|
74
|
-
// TODO: revisit this when RSC splits into two bundles
|
|
75
|
-
/\.test\.[tj]sx?$/.test(importer));
|
|
76
|
-
},
|
|
77
|
-
}),
|
|
31
|
+
(0, vite_plugin_hydrogen_rsc_1.default)(),
|
|
78
32
|
(0, vite_plugin_platform_entry_1.default)(),
|
|
79
33
|
pluginOptions.purgeQueryCacheOnBuild && (0, vite_plugin_purge_query_cache_1.default)(),
|
|
80
34
|
];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function clientImports() {
|
|
4
|
+
return {
|
|
5
|
+
name: 'hydrogen:client-imports',
|
|
6
|
+
enforce: 'pre',
|
|
7
|
+
/**
|
|
8
|
+
* When importer does not end in `server.jsx`, and source is `@shopify/hydrogen`,
|
|
9
|
+
* replace with `@shopify/hydrogen/client`. This prevents other server-only imports
|
|
10
|
+
* from "leaking" into the client bundle.
|
|
11
|
+
*/
|
|
12
|
+
async resolveId(source, importer, { ssr }) {
|
|
13
|
+
if (ssr)
|
|
14
|
+
return;
|
|
15
|
+
if (/\.server\.(j|t)sx?/.test(importer !== null && importer !== void 0 ? importer : ''))
|
|
16
|
+
return;
|
|
17
|
+
if ('@shopify/hydrogen' !== source)
|
|
18
|
+
return;
|
|
19
|
+
const resolution = await this.resolve('@shopify/hydrogen/client', importer, {
|
|
20
|
+
skipSelf: true,
|
|
21
|
+
});
|
|
22
|
+
if (resolution) {
|
|
23
|
+
return resolution.id;
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
exports.default = clientImports;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
2
|
export default function cssModulesRsc(): Plugin[];
|
|
@@ -1,18 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const vite_plugin_hydrogen_middleware_1 = require("./vite-plugin-hydrogen-middleware");
|
|
7
|
+
const magic_string_1 = __importDefault(require("magic-string"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const cssModuleRE = /\.module\.(s?css|sass|less|stylus)/;
|
|
3
10
|
function cssModulesRsc() {
|
|
4
11
|
// 1. Original CSS module: `.myStyle { color: red; }`
|
|
5
12
|
// 2. CSS module after 'pre' Vite transforms: `.myStyle_hashedXYZ { color: red; }`
|
|
6
13
|
// 3. CSS module after 'post' Vite transforms: `export const myStyle = 'myStyle_hashedXYZ';`
|
|
7
14
|
let cssMap = new Map();
|
|
15
|
+
let config;
|
|
8
16
|
return [
|
|
9
17
|
{
|
|
10
18
|
name: 'css-modules-rsc',
|
|
11
|
-
configResolved() {
|
|
19
|
+
configResolved(_config) {
|
|
20
|
+
config = _config;
|
|
12
21
|
cssMap = new Map();
|
|
22
|
+
// Place this plugin before react-refresh to
|
|
23
|
+
// modify files before JSX is compiled.
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
config.plugins.unshift(autoStyleTagPlugin());
|
|
13
26
|
},
|
|
14
27
|
transform(code, id) {
|
|
15
|
-
if (
|
|
28
|
+
if (cssModuleRE.test(id)) {
|
|
16
29
|
cssMap.set(id, code);
|
|
17
30
|
}
|
|
18
31
|
},
|
|
@@ -22,10 +35,70 @@ function cssModulesRsc() {
|
|
|
22
35
|
enforce: 'post',
|
|
23
36
|
transform(code, id) {
|
|
24
37
|
if (id.includes('.module.') && cssMap.has(id)) {
|
|
25
|
-
|
|
38
|
+
const isDev = config.command === 'serve';
|
|
39
|
+
const key = path_1.default.relative(config.root, id.split('?')[0]);
|
|
40
|
+
const s = new magic_string_1.default(code);
|
|
41
|
+
s.prepend((isDev
|
|
42
|
+
? `import {jsxDEV as _jsx} from 'react/jsx-dev-runtime';`
|
|
43
|
+
: `import {jsx as _jsx} from 'react/jsx-runtime';`) +
|
|
44
|
+
`export const StyleTag = () => _jsx('style', {dangerouslySetInnerHTML: {__html: ${JSON.stringify(cssMap.get(id))}}});` +
|
|
45
|
+
`\nStyleTag.key = '${key}';\n`);
|
|
46
|
+
s.replace(/export default \{/gs, `export default {\n StyleTag,`);
|
|
47
|
+
return {
|
|
48
|
+
code: s.toString(),
|
|
49
|
+
map: s.generateMap({ file: id, source: id }),
|
|
50
|
+
};
|
|
26
51
|
}
|
|
27
52
|
},
|
|
28
53
|
},
|
|
29
54
|
];
|
|
30
55
|
}
|
|
31
56
|
exports.default = cssModulesRsc;
|
|
57
|
+
function autoStyleTagPlugin() {
|
|
58
|
+
return {
|
|
59
|
+
name: 'css-modules-auto-style-tag',
|
|
60
|
+
transform(code, id) {
|
|
61
|
+
id = id.split('?')[0];
|
|
62
|
+
if (/\.[jt]sx$/.test(id) &&
|
|
63
|
+
!id.endsWith(vite_plugin_hydrogen_middleware_1.HYDROGEN_DEFAULT_SERVER_ENTRY) &&
|
|
64
|
+
!id.endsWith(path_1.default.format({
|
|
65
|
+
name: vite_plugin_hydrogen_middleware_1.HYDROGEN_DEFAULT_SERVER_ENTRY,
|
|
66
|
+
ext: path_1.default.extname(id),
|
|
67
|
+
})) &&
|
|
68
|
+
cssModuleRE.test(code) &&
|
|
69
|
+
code.includes('export default')) {
|
|
70
|
+
const s = new magic_string_1.default(code);
|
|
71
|
+
// 1. Gather style tags in an array
|
|
72
|
+
let styleCount = 0;
|
|
73
|
+
s.prepend(`const __styleTags = [];\n`);
|
|
74
|
+
s.replace(/^import\s+(.+?)\s+from\s+['"]([^'"]+?\.module\.[^'"]+?)['"]/gm, (all, statements, from) => {
|
|
75
|
+
if (!cssModuleRE.test(from)) {
|
|
76
|
+
return all;
|
|
77
|
+
}
|
|
78
|
+
if (statements.startsWith('{')) {
|
|
79
|
+
// Add default import
|
|
80
|
+
const replacement = `__style${styleCount++}, {`;
|
|
81
|
+
statements = statements.replace('{', replacement);
|
|
82
|
+
all = all.replace('{', replacement);
|
|
83
|
+
}
|
|
84
|
+
const defaultImport = statements
|
|
85
|
+
.split(',')[0]
|
|
86
|
+
.replace(/\*\s+as\s+/, '')
|
|
87
|
+
.trim();
|
|
88
|
+
return all + `; __styleTags.push(${defaultImport}.StyleTag)`;
|
|
89
|
+
});
|
|
90
|
+
// 2. Wrap default export in a new component that includes the style tags
|
|
91
|
+
s.replace(/export default/gm, 'const __defaultExport = ');
|
|
92
|
+
s.append(`\nconst __ApplyStyleTags = function (props) {\n` +
|
|
93
|
+
` return <>{__styleTags.map(ST => <ST key={ST.key} />)}<__defaultExport {...props} /></>;` +
|
|
94
|
+
`\n}\n\n` +
|
|
95
|
+
`Object.defineProperty(__ApplyStyleTags, 'name', {value: 'ApplyStyleTags_' + (__defaultExport.name || '')});\n` +
|
|
96
|
+
`export default __ApplyStyleTags;`);
|
|
97
|
+
return {
|
|
98
|
+
code: s.toString(),
|
|
99
|
+
map: s.generateMap({ file: id, source: id }),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|