@shopify/hydrogen 1.4.3 → 1.5.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.
Files changed (28) hide show
  1. package/dist/esnext/components/CartLineProvider/tests/fixtures.d.ts +86 -0
  2. package/dist/esnext/components/CartLineProvider/tests/fixtures.js +34 -0
  3. package/dist/esnext/components/CartProvider/CartProviderV2.client.js +40 -10
  4. package/dist/esnext/components/CartProvider/tests/fixtures.d.ts +254 -0
  5. package/dist/esnext/components/CartProvider/tests/fixtures.js +53 -0
  6. package/dist/esnext/components/Metafield/Metafield.client.js +1 -3
  7. package/dist/esnext/foundation/Router/BrowserRouter.client.js +7 -1
  8. package/dist/esnext/framework/plugins/vite-plugin-css-rsc.d.ts +1 -1
  9. package/dist/esnext/framework/plugins/vite-plugin-css-rsc.js +100 -3
  10. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-rsc.js +2 -2
  11. package/dist/esnext/framework/plugins/vite-plugin-platform-entry.js +1 -1
  12. package/dist/esnext/storefront-api-types.d.ts +334 -116
  13. package/dist/esnext/storefront-api-types.js +3 -1
  14. package/dist/esnext/testing.d.ts +2 -0
  15. package/dist/esnext/testing.js +2 -0
  16. package/dist/esnext/utilities/tests/MockedServerRequestProvider.server.d.ts +6 -0
  17. package/dist/esnext/utilities/tests/MockedServerRequestProvider.server.js +9 -0
  18. package/dist/esnext/utilities/tests/price.d.ts +5 -0
  19. package/dist/esnext/utilities/tests/price.js +9 -0
  20. package/dist/esnext/utilities/tests/provider-helpers.d.ts +31 -0
  21. package/dist/esnext/utilities/tests/provider-helpers.js +36 -0
  22. package/dist/esnext/version.d.ts +1 -1
  23. package/dist/esnext/version.js +1 -1
  24. package/dist/node/framework/plugins/vite-plugin-css-rsc.d.ts +1 -1
  25. package/dist/node/framework/plugins/vite-plugin-css-rsc.js +99 -2
  26. package/dist/node/framework/plugins/vite-plugin-hydrogen-rsc.js +2 -2
  27. package/dist/node/framework/plugins/vite-plugin-platform-entry.js +1 -1
  28. package/package.json +3 -5
@@ -1,8 +1,33 @@
1
1
  import path from 'path';
2
2
  import MagicString from 'magic-string';
3
- import { normalizePath } from 'vite';
3
+ import { normalizePath, } from 'vite';
4
4
  const VITE_CSS_CHUNK_NAME = 'style.css';
5
5
  const INJECT_STYLES_COMMENT = '<!--__INJECT_STYLES__-->';
6
+ const CSS_EXTENSIONS_RE = /\.(css|sass|scss|stylus|less)(\.|\?|$)/;
7
+ const CSS_MODULES_EXTENSIONS_RE = /\.module\.(css|sass|scss|stylus|less)(\?|$)/;
8
+ const EVENT_CSS_IMPORT = 'hydrogen-css-modules-update-imports';
9
+ const EVENT_CSS_CLASSES = 'hydrogen-css-modules-update-classes';
10
+ const CSS_MODULES_HMR_INJECT = `
11
+ import {createHotContext, injectQuery} from "/@vite/client";
12
+
13
+ if (!import.meta.hot) {
14
+ import.meta.hot = createHotContext("/index.html");
15
+ }
16
+
17
+ import.meta.hot.on('${EVENT_CSS_IMPORT}', ({ids, timestamp}) => {
18
+ ids.forEach((id) => {
19
+ import(injectQuery(id, 't=' + timestamp));
20
+ });
21
+ });
22
+
23
+ import.meta.hot.on('${EVENT_CSS_CLASSES}', ({replacements}) => {
24
+ replacements.forEach(([oldClass, newClass]) => {
25
+ document.querySelectorAll('.' + oldClass).forEach(node => {
26
+ node.classList.replace(oldClass, newClass);
27
+ })
28
+ });
29
+ });
30
+ `;
6
31
  // Keep this in the outer scope to share it
7
32
  // across client <> server builds.
8
33
  let clientBuildPath;
@@ -16,6 +41,10 @@ let clientBuildPath;
16
41
  */
17
42
  export default function cssRsc() {
18
43
  let config;
44
+ let server;
45
+ let isUsingCssModules = false;
46
+ const hmrCssCopy = new Map();
47
+ const hmrCssQueue = new Set();
19
48
  return {
20
49
  name: 'hydrogen:css-rsc',
21
50
  enforce: 'post',
@@ -27,6 +56,9 @@ export default function cssRsc() {
27
56
  configResolved(_config) {
28
57
  config = _config;
29
58
  },
59
+ configureServer(_server) {
60
+ server = _server;
61
+ },
30
62
  transform(code, id, options) {
31
63
  if (options?.ssr && id.includes('index.html?raw')) {
32
64
  // Mark the client build index.html to inject styles later
@@ -37,17 +69,63 @@ export default function cssRsc() {
37
69
  map: s.generateMap({ file: id, source: id }),
38
70
  };
39
71
  }
72
+ // Manual HMR for CSS Modules
73
+ if (server && CSS_MODULES_EXTENSIONS_RE.test(id)) {
74
+ isUsingCssModules = true;
75
+ const file = id.split('?')[0];
76
+ // Note: this "CSS" file is actually JavaScript code.
77
+ // Get a copy of how this CSS was before the current update
78
+ const oldCode = hmrCssCopy.get(file);
79
+ // Save a copy of the current CSS for future updates
80
+ hmrCssCopy.set(file, code);
81
+ if (!oldCode || !hmrCssQueue.has(file))
82
+ return;
83
+ hmrCssQueue.delete(file);
84
+ // Diff old code with new code and use the exported class names as a reference
85
+ // to find out how the resulting CSS classes are renamed. With this, we can
86
+ // update classes in the DOM without requesting a full rendering from the server.
87
+ // Example:
88
+ // Previous code => export const red = ".red_k3tz4_module";
89
+ // New code => export const red = ".red_t93kw_module";
90
+ const classRE = /export const (.+?) = "(.+?)"/g;
91
+ const oldClasses = [...oldCode.matchAll(classRE)];
92
+ const replacements = [];
93
+ for (const [, newKey, newClass] of code.matchAll(classRE)) {
94
+ const oldClass = oldClasses.find(([, oldKey]) => oldKey === newKey)?.[2];
95
+ if (oldClass && oldClass !== newClass) {
96
+ replacements.push([oldClass, newClass]);
97
+ }
98
+ }
99
+ if (replacements.length > 0) {
100
+ // This event asks the browser to replace old
101
+ // hash-based CSS classes with new ones.
102
+ // Example: from `.red_k3tz4_module` to `.red_t93kw_module`
103
+ server.ws.send({
104
+ type: 'custom',
105
+ event: EVENT_CSS_CLASSES,
106
+ data: { replacements },
107
+ });
108
+ }
109
+ }
40
110
  },
41
111
  transformIndexHtml(html, { server }) {
42
112
  // Add discovered styles during dev
43
113
  if (server) {
44
- const tags = [];
114
+ const tags = (isUsingCssModules
115
+ ? [
116
+ {
117
+ tag: 'script',
118
+ attrs: { type: 'module' },
119
+ children: CSS_MODULES_HMR_INJECT,
120
+ },
121
+ ]
122
+ : []);
45
123
  const foundCssFiles = new Set();
46
124
  for (const [key, value] of server.moduleGraph.idToModuleMap.entries()) {
47
125
  if (
48
126
  // Note: Some CSS-in-JS libraries use `.css.js`
49
127
  // extension and we should match it here:
50
- /\.(css|sass|scss|stylus|less)(\.|\?|$)/.test(normalizePath(key).split('/').pop())) {
128
+ CSS_EXTENSIONS_RE.test(normalizePath(key).split('/').pop())) {
51
129
  let { url, file, lastHMRTimestamp, importers } = value;
52
130
  if (!foundCssFiles.has(file) &&
53
131
  !Array.from(importers).some((importer) => foundCssFiles.has(importer.file))) {
@@ -112,5 +190,24 @@ export default function cssRsc() {
112
190
  }
113
191
  }
114
192
  },
193
+ async handleHotUpdate({ modules, server }) {
194
+ if (modules.every((m) => CSS_MODULES_EXTENSIONS_RE.test(m.file || ''))) {
195
+ // Opt-out of Vite's default HMR for CSS Modules, we'll handle this manually
196
+ const file = modules[0].file;
197
+ hmrCssQueue.add(file);
198
+ // This event asks the browser to download fresh CSS files.
199
+ // Fetching these fresh CSS files will trigger another event
200
+ // from the `transform` hook to replace classes in the DOM.
201
+ server.ws.send({
202
+ type: 'custom',
203
+ event: EVENT_CSS_IMPORT,
204
+ data: {
205
+ ids: modules.map((m) => m.id),
206
+ timestamp: modules[0].lastHMRTimestamp || Date.now(),
207
+ },
208
+ });
209
+ return [];
210
+ }
211
+ },
115
212
  };
116
213
  }
@@ -13,10 +13,10 @@ export default function (options) {
13
13
  // Always allow the entry server (e.g. App.server.jsx) to be imported
14
14
  // in other files such as worker.js or server.js.
15
15
  source.includes(HYDROGEN_DEFAULT_SERVER_ENTRY) ||
16
- /(index|entry-server|hydrogen\.config)\.[jt]s/.test(importer) ||
16
+ /(index|provider-helpers|entry-server|testing|hydrogen\.config)\.[jt]s/.test(importer) ||
17
17
  // Support importing server components for testing
18
18
  // TODO: revisit this when RSC splits into two bundles
19
- /\.test\.[tj]sx?$/.test(importer));
19
+ /\.(test|vitest|spec)\.[tj]sx?$/.test(importer));
20
20
  },
21
21
  ...options,
22
22
  });
@@ -49,7 +49,7 @@ export default () => {
49
49
  async transform(code, id, options) {
50
50
  if (config.command === 'build' &&
51
51
  options?.ssr &&
52
- /@shopify\/hydrogen\/.+platforms\/virtual\./.test(normalizePath(id))) {
52
+ /\/hydrogen\/.+platforms\/virtual\./.test(normalizePath(id))) {
53
53
  const ms = new MagicString(code);
54
54
  ms.replace('__HYDROGEN_ENTRY__', HYDROGEN_DEFAULT_SERVER_ENTRY);
55
55
  if (!clientBuildPath) {