@shopify/hydrogen 1.2.0 → 1.3.2

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 (98) hide show
  1. package/README.md +1 -1
  2. package/dist/esnext/components/CartProvider/CartProvider.client.js +6 -0
  3. package/dist/esnext/components/CartProvider/cart-queries.d.ts +1 -1
  4. package/dist/esnext/components/CartProvider/cart-queries.js +1 -0
  5. package/dist/esnext/components/ExternalVideo/ExternalVideo.js +2 -2
  6. package/dist/esnext/components/Image/Image.d.ts +1 -1
  7. package/dist/esnext/components/Image/Image.js +4 -4
  8. package/dist/esnext/components/Image/index.d.ts +1 -1
  9. package/dist/esnext/components/index.d.ts +1 -1
  10. package/dist/esnext/components/index.js +1 -1
  11. package/dist/esnext/constants.d.ts +1 -0
  12. package/dist/esnext/constants.js +1 -0
  13. package/dist/esnext/entry-client.js +1 -1
  14. package/dist/esnext/entry-server.js +30 -28
  15. package/dist/esnext/experimental.d.ts +1 -0
  16. package/dist/esnext/experimental.js +1 -0
  17. package/dist/esnext/foundation/Cache/cache.js +0 -1
  18. package/dist/esnext/foundation/HydrogenRequest/HydrogenRequest.server.d.ts +1 -0
  19. package/dist/esnext/foundation/HydrogenRequest/HydrogenRequest.server.js +1 -0
  20. package/dist/esnext/foundation/HydrogenResponse/HydrogenResponse.server.d.ts +4 -2
  21. package/dist/esnext/foundation/HydrogenResponse/HydrogenResponse.server.js +17 -16
  22. package/dist/esnext/foundation/ServerPropsProvider/ServerPropsProvider.js +1 -3
  23. package/dist/esnext/foundation/fetchSync/ResponseSync.d.ts +2 -1
  24. package/dist/esnext/foundation/fetchSync/ResponseSync.js +14 -2
  25. package/dist/esnext/foundation/session/session-types.d.ts +2 -0
  26. package/dist/esnext/foundation/session/session.d.ts +3 -0
  27. package/dist/esnext/foundation/session/session.js +16 -0
  28. package/dist/esnext/foundation/useQuery/hooks.d.ts +3 -0
  29. package/dist/esnext/foundation/useQuery/hooks.js +1 -1
  30. package/dist/esnext/foundation/useSession/useSession.d.ts +1 -0
  31. package/dist/esnext/foundation/useSession/useSession.js +13 -0
  32. package/dist/esnext/framework/plugin.js +6 -5
  33. package/dist/esnext/framework/plugins/vite-plugin-assets-version.d.ts +2 -0
  34. package/dist/esnext/framework/plugins/vite-plugin-assets-version.js +12 -0
  35. package/dist/esnext/framework/plugins/vite-plugin-client-imports.js +6 -3
  36. package/dist/esnext/framework/plugins/vite-plugin-css-modules-rsc.js +3 -0
  37. package/dist/esnext/framework/plugins/vite-plugin-css-rsc.js +8 -5
  38. package/dist/esnext/framework/plugins/vite-plugin-hydration-auto-import.js +7 -1
  39. package/dist/{node/framework/plugins/vite-plugin-purge-query-cache.d.ts → esnext/framework/plugins/vite-plugin-hydrogen-client-components-cache.d.ts} +1 -1
  40. package/dist/esnext/framework/plugins/{vite-plugin-hydrogen-client-middleware.js → vite-plugin-hydrogen-client-components-cache.js} +2 -2
  41. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-config.d.ts +2 -1
  42. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-config.js +104 -90
  43. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-middleware.js +6 -6
  44. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-suppress-warnings.js +5 -0
  45. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-virtual-files.js +45 -7
  46. package/dist/esnext/framework/plugins/vite-plugin-platform-entry.js +14 -1
  47. package/dist/esnext/framework/plugins/vite-plugin-ssr-interop.js +5 -1
  48. package/dist/esnext/framework/types.d.ts +1 -0
  49. package/dist/esnext/framework/viteception.js +7 -1
  50. package/dist/esnext/storefront-api-types.d.ts +38 -31
  51. package/dist/esnext/storefront-api-types.js +4 -2
  52. package/dist/esnext/types.d.ts +2 -1
  53. package/dist/esnext/utilities/apiRoutes.d.ts +2 -0
  54. package/dist/esnext/utilities/apiRoutes.js +12 -1
  55. package/dist/esnext/utilities/fetch.js +4 -1
  56. package/dist/esnext/utilities/log/log.js +20 -5
  57. package/dist/esnext/utilities/log/utils.js +1 -1
  58. package/dist/esnext/utilities/template.d.ts +7 -6
  59. package/dist/esnext/utilities/template.js +39 -1
  60. package/dist/esnext/utilities/vite.d.ts +1 -0
  61. package/dist/esnext/utilities/vite.js +4 -0
  62. package/dist/esnext/version.d.ts +1 -1
  63. package/dist/esnext/version.js +1 -1
  64. package/dist/node/foundation/session/session-types.d.ts +2 -0
  65. package/dist/node/framework/plugin.js +6 -5
  66. package/dist/node/framework/plugins/vite-plugin-assets-version.d.ts +2 -0
  67. package/dist/node/framework/plugins/vite-plugin-assets-version.js +15 -0
  68. package/dist/node/framework/plugins/vite-plugin-client-imports.js +6 -3
  69. package/dist/node/framework/plugins/vite-plugin-css-modules-rsc.js +3 -0
  70. package/dist/node/framework/plugins/vite-plugin-css-rsc.js +8 -5
  71. package/dist/node/framework/plugins/vite-plugin-hydration-auto-import.js +7 -1
  72. package/dist/{esnext/framework/plugins/vite-plugin-purge-query-cache.d.ts → node/framework/plugins/vite-plugin-hydrogen-client-components-cache.d.ts} +1 -1
  73. package/dist/node/framework/plugins/{vite-plugin-hydrogen-client-middleware.js → vite-plugin-hydrogen-client-components-cache.js} +2 -2
  74. package/dist/node/framework/plugins/vite-plugin-hydrogen-config.d.ts +2 -1
  75. package/dist/node/framework/plugins/vite-plugin-hydrogen-config.js +107 -90
  76. package/dist/node/framework/plugins/vite-plugin-hydrogen-middleware.js +6 -6
  77. package/dist/node/framework/plugins/vite-plugin-hydrogen-suppress-warnings.js +5 -0
  78. package/dist/node/framework/plugins/vite-plugin-hydrogen-virtual-files.js +45 -7
  79. package/dist/node/framework/plugins/vite-plugin-platform-entry.js +14 -1
  80. package/dist/node/framework/plugins/vite-plugin-ssr-interop.js +5 -1
  81. package/dist/node/framework/types.d.ts +1 -0
  82. package/dist/node/framework/viteception.js +7 -1
  83. package/dist/node/utilities/vite.d.ts +1 -0
  84. package/dist/node/utilities/vite.js +30 -0
  85. package/package.json +7 -6
  86. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-plugin.js +36 -25
  87. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-writer.browser.development.server.js +105 -29
  88. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-writer.browser.production.min.server.js +30 -29
  89. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-writer.node.development.server.js +92 -29
  90. package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-writer.node.production.min.server.js +31 -29
  91. package/vendor/react-server-dom-vite/esm/react-server-dom-vite-plugin.js +29 -18
  92. package/vendor/react-server-dom-vite/esm/react-server-dom-vite-writer.browser.server.js +105 -29
  93. package/vendor/react-server-dom-vite/esm/react-server-dom-vite-writer.node.server.js +92 -29
  94. package/vendor/react-server-dom-vite/package.json +2 -2
  95. package/dist/esnext/framework/plugins/vite-plugin-hydrogen-client-middleware.d.ts +0 -9
  96. package/dist/esnext/framework/plugins/vite-plugin-purge-query-cache.js +0 -11
  97. package/dist/node/framework/plugins/vite-plugin-hydrogen-client-middleware.d.ts +0 -9
  98. package/dist/node/framework/plugins/vite-plugin-purge-query-cache.js +0 -16
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vite_1 = require("vite");
4
- /**
4
+ /* -- Plugin notes:
5
5
  * This dev server middleware prevents Vite from applying immutable cache control headers to client
6
6
  * components. These client components are part of the user's local source, but since they are
7
7
  * referenced via import globs in the `react-dom-server-vite` NPM package, Vite assumes they
@@ -9,7 +9,7 @@ const vite_1 = require("vite");
9
9
  */
10
10
  exports.default = () => {
11
11
  return {
12
- name: 'vite-plugin-hydrogen-client-middleware',
12
+ name: 'hydrogen:client-components-cache',
13
13
  enforce: 'pre',
14
14
  configureServer(server) {
15
15
  server.middlewares.use(async (req, res, next) => {
@@ -1,3 +1,4 @@
1
1
  import { Plugin } from 'vite';
2
- declare const _default: () => Plugin;
2
+ import type { HydrogenVitePluginOptions } from '../types.js';
3
+ declare const _default: (pluginOptions: HydrogenVitePluginOptions) => Plugin;
3
4
  export default _default;
@@ -1,104 +1,121 @@
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 });
3
- exports.default = () => {
6
+ const crypto_1 = __importDefault(require("crypto"));
7
+ /* -- Plugin notes:
8
+ * Provide configuration options to Vite to ensure Hydrogen is built correctly
9
+ * for worker environments, using latest ES syntax, and some other requirements.
10
+ */
11
+ exports.default = (pluginOptions) => {
4
12
  const rollupOptions = {
5
13
  output: {},
6
14
  };
7
15
  const isWorker = Boolean(process.env.WORKER) && process.env.WORKER !== 'undefined';
8
- if (isWorker) {
9
- /**
10
- * By default, SSR dedupe logic gets bundled which runs `require('module')`.
11
- * We don't want this in our workers runtime, because `require` is not supported.
12
- */
13
- rollupOptions.output = {
14
- format: 'es',
15
- };
16
- }
17
- if (process.env.NODE_ENV !== 'development' && !process.env.LOCAL_DEV) {
18
- /**
19
- * Ofuscate production asset name - To prevent ad blocker logics that blocks
20
- * certain files due to how it is named.
21
- */
22
- rollupOptions.output = {
23
- ...rollupOptions.output,
24
- chunkFileNames: 'assets/[hash].js',
25
- };
26
- }
27
16
  return {
28
- name: 'vite-plugin-hydrogen-config',
29
- config: async (config, env) => ({
30
- resolve: {
31
- alias: {
32
- // This library is currently included as a compiled vendor lib, not published yet to NPM
33
- 'react-server-dom-vite/client-proxy': require.resolve('@shopify/hydrogen/vendor/react-server-dom-vite/esm/react-server-dom-vite-client-proxy.js'),
34
- },
35
- },
36
- build: {
37
- minify: config.build?.minify ?? (process.env.LOCAL_DEV ? false : 'esbuild'),
38
- sourcemap: true,
39
- rollupOptions: config.build?.rollupOptions
40
- ? Object.assign(rollupOptions, config.build.rollupOptions)
41
- : rollupOptions,
42
- target: config.build?.ssr
43
- ? isWorker
44
- ? 'es2022' // CFW (Updates weekly to latest V8)
45
- : 'es2020' // Node (Support for v14.19 used in SB)
46
- : 'modules', // Browsers (Vite default value)
47
- },
48
- ssr: {
17
+ name: 'hydrogen:config',
18
+ config: async (config, env) => {
19
+ // @ts-ignore
20
+ const isSsrBuild = env.ssrBuild ?? !!config.build?.ssr;
21
+ /**
22
+ * By default, SSR dedupe logic gets bundled which runs `require('module')`.
23
+ * We don't want this in our workers runtime, because `require` is not supported.
24
+ */
25
+ rollupOptions.output = {
26
+ format: isWorker || !isSsrBuild ? 'es' : 'cjs',
27
+ inlineDynamicImports: isSsrBuild ? true : undefined,
28
+ };
29
+ if (process.env.NODE_ENV !== 'development' && !process.env.LOCAL_DEV) {
49
30
  /**
50
- * Tell Vite to bundle everything when we're building for Workers.
51
- * Otherwise, bundle RSC plugin as a workaround to apply the vendor alias above.
31
+ * Ofuscate production asset name - To prevent ad blocker logics that blocks
32
+ * certain files due to how it is named.
52
33
  */
53
- noExternal: isWorker || [/react-server-dom-vite/],
54
- target: isWorker ? 'webworker' : 'node',
55
- },
56
- // Reload when updating local Hydrogen lib
57
- server: process.env.LOCAL_DEV && {
58
- watch: {
59
- ignored: [
60
- '!**/node_modules/@shopify/hydrogen/**',
61
- '!**/node_modules/@shopify/hydrogen-ui/**',
62
- ],
34
+ rollupOptions.output = {
35
+ ...rollupOptions.output,
36
+ chunkFileNames: 'assets/[hash].js',
37
+ };
38
+ }
39
+ return {
40
+ resolve: {
41
+ alias: {
42
+ // This library is currently included as a compiled vendor lib, not published yet to NPM
43
+ 'react-server-dom-vite/client-proxy': require.resolve('@shopify/hydrogen/vendor/react-server-dom-vite/esm/react-server-dom-vite-client-proxy.js'),
44
+ },
63
45
  },
64
- },
65
- optimizeDeps: {
66
- exclude: [
67
- '@shopify/hydrogen',
68
- '@shopify/hydrogen/client',
69
- '@shopify/hydrogen/entry-client',
70
- '@shopify/hydrogen-ui',
71
- ],
72
- include: [
73
- /**
74
- * Additionally, the following dependencies have trouble loading the
75
- * correct version of the dependency (server vs client). This tells Vite to take the
76
- * server versions and optimize them for ESM.
77
- */
78
- 'react-helmet-async',
79
- 'react-error-boundary',
46
+ build: {
47
+ minify: config.build?.minify ?? (process.env.LOCAL_DEV ? false : 'esbuild'),
48
+ sourcemap: true,
49
+ rollupOptions: config.build?.rollupOptions
50
+ ? Object.assign(rollupOptions, config.build.rollupOptions)
51
+ : rollupOptions,
52
+ target: config.build?.ssr
53
+ ? isWorker
54
+ ? 'es2022' // CFW (Updates weekly to latest V8)
55
+ : 'es2020' // Node (Support for v14.19 used in SB)
56
+ : 'modules', // Browsers (Vite default value)
57
+ },
58
+ ssr: {
80
59
  /**
81
- * Vite cannot find the following dependencies since they might be
82
- * required in RSC asynchronously. This tells Vite to optimize them
83
- * at server start to avoid posterior page reloads and issues (#429 #430).
60
+ * Tell Vite to bundle everything when we're building for Workers.
61
+ * Otherwise, bundle RSC plugin as a workaround to apply the vendor alias above.
84
62
  */
85
- 'react',
86
- 'react-dom/client',
87
- 'react-server-dom-vite/client-proxy',
88
- // https://github.com/vitejs/vite/issues/6215
89
- 'react/jsx-runtime',
90
- // https://github.com/nfriedly/set-cookie-parser/issues/50
91
- 'set-cookie-parser',
92
- 'undici',
93
- ],
94
- },
95
- define: {
96
- __HYDROGEN_DEV__: env.mode !== 'production',
97
- __HYDROGEN_WORKER__: isWorker,
98
- __HYDROGEN_TEST__: false, // Used in unit tests
99
- },
100
- envPrefix: ['VITE_', 'PUBLIC_'],
101
- base: process.env.HYDROGEN_ASSET_BASE_URL,
102
- }),
63
+ noExternal: isWorker || [
64
+ /react-server-dom-vite/,
65
+ /@shopify\/hydrogen/,
66
+ ],
67
+ target: isWorker ? 'webworker' : 'node',
68
+ },
69
+ // Reload when updating local Hydrogen lib
70
+ server: process.env.LOCAL_DEV && {
71
+ watch: {
72
+ ignored: [
73
+ '!**/node_modules/@shopify/hydrogen/**',
74
+ '!**/node_modules/@shopify/hydrogen-ui/**',
75
+ ],
76
+ },
77
+ },
78
+ optimizeDeps: {
79
+ exclude: [
80
+ '@shopify/hydrogen',
81
+ '@shopify/hydrogen/client',
82
+ '@shopify/hydrogen/entry-client',
83
+ '@shopify/hydrogen-ui',
84
+ ],
85
+ include: [
86
+ /**
87
+ * Additionally, the following dependencies have trouble loading the
88
+ * correct version of the dependency (server vs client). This tells Vite to take the
89
+ * server versions and optimize them for ESM.
90
+ */
91
+ 'react-helmet-async',
92
+ 'react-error-boundary',
93
+ /**
94
+ * Vite cannot find the following dependencies since they might be
95
+ * required in RSC asynchronously. This tells Vite to optimize them
96
+ * at server start to avoid posterior page reloads and issues (#429 #430).
97
+ */
98
+ 'react',
99
+ 'react-dom/client',
100
+ 'react-server-dom-vite/client-proxy',
101
+ // https://github.com/vitejs/vite/issues/6215
102
+ 'react/jsx-runtime',
103
+ // https://github.com/nfriedly/set-cookie-parser/issues/50
104
+ 'set-cookie-parser',
105
+ 'undici',
106
+ ],
107
+ },
108
+ define: {
109
+ __HYDROGEN_DEV__: env.mode !== 'production',
110
+ __HYDROGEN_WORKER__: isWorker,
111
+ __HYDROGEN_TEST__: false,
112
+ __HYDROGEN_CACHE_ID__: pluginOptions.purgeQueryCacheOnBuild
113
+ ? `"${crypto_1.default.randomBytes(8).toString('hex').slice(0, 8)}"`
114
+ : '"__QUERY_CACHE_ID__"',
115
+ },
116
+ envPrefix: ['VITE_', 'PUBLIC_'],
117
+ base: process.env.HYDROGEN_ASSET_BASE_URL,
118
+ };
119
+ },
103
120
  };
104
121
  };
@@ -12,15 +12,15 @@ const middleware_js_1 = require("../middleware.js");
12
12
  const in_memory_js_1 = require("../cache/in-memory.js");
13
13
  const vite_plugin_hydrogen_virtual_files_js_1 = require("./vite-plugin-hydrogen-virtual-files.js");
14
14
  exports.HYDROGEN_DEFAULT_SERVER_ENTRY = process.env.HYDROGEN_SERVER_ENTRY || '/src/App.server';
15
+ /* -- Plugin notes:
16
+ * By adding a middleware to the Vite dev server, we can handle SSR without needing
17
+ * a custom node script. It works by handling any requests for `text/html` documents,
18
+ * loading them in an SSR context, rendering them using the `entry-server` endpoint in the
19
+ * user's project, and injecting the static HTML into the template.
20
+ */
15
21
  exports.default = (pluginOptions) => {
16
22
  return {
17
23
  name: 'hydrogen:middleware',
18
- /**
19
- * By adding a middleware to the Vite dev server, we can handle SSR without needing
20
- * a custom node script. It works by handling any requests for `text/html` documents,
21
- * loading them in an SSR context, rendering them using the `entry-server` endpoint in the
22
- * user's project, and injecting the static HTML into the template.
23
- */
24
24
  async configureServer(server) {
25
25
  const resolve = (p) => path_1.default.resolve(server.config.root, p);
26
26
  async function getIndexTemplate(url) {
@@ -1,5 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* -- Plugin notes:
4
+ * Vite prints some warnings automatically are not important and
5
+ * not actionable. We hide them here to prevent confusing devs when
6
+ * real warnings or errors appear.
7
+ */
3
8
  exports.default = () => {
4
9
  return {
5
10
  name: 'hydrogen:suppress-warnings',
@@ -8,6 +8,15 @@ const vite_1 = require("vite");
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const fs_1 = require("fs");
10
10
  const viteception_js_1 = require("../viteception.js");
11
+ const magic_string_1 = __importDefault(require("magic-string"));
12
+ const vite_js_1 = require("../../utilities/vite.js");
13
+ /* -- Plugin notes:
14
+ * The Hydrogen framework needs to import certain files from the user app, such as
15
+ * routes and config. A priori, we can't import these files from the framework
16
+ * because we don't know the user path to write it in an `import * from '...'` statement.
17
+ * Instead, we import "virtual files" that are resolved by Vite in this plugin.
18
+ * These virtual files can include the user path and re-export the in-app files.
19
+ */
11
20
  exports.HYDROGEN_DEFAULT_SERVER_ENTRY = process.env.HYDROGEN_SERVER_ENTRY || '/src/App.server';
12
21
  // The character ":" breaks Vite with Node >= 16.15. Use "_" instead
13
22
  const VIRTUAL_PREFIX = 'virtual__';
@@ -20,9 +29,11 @@ exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID = VIRTUAL_PREFIX + PROXY_PREFIX + HYDRO
20
29
  const HYDROGEN_ROUTES_ID = 'hydrogen-routes.server.jsx';
21
30
  const VIRTUAL_HYDROGEN_ROUTES_ID = VIRTUAL_PREFIX + HYDROGEN_ROUTES_ID;
22
31
  exports.VIRTUAL_PROXY_HYDROGEN_ROUTES_ID = VIRTUAL_PREFIX + PROXY_PREFIX + HYDROGEN_ROUTES_ID;
32
+ const VIRTUAL_STREAM_ID = 'virtual__stream';
23
33
  exports.default = (pluginOptions) => {
24
34
  let config;
25
35
  let server;
36
+ let resolvedConfigPath;
26
37
  return {
27
38
  name: 'hydrogen:virtual-files',
28
39
  configResolved(_config) {
@@ -33,16 +44,19 @@ exports.default = (pluginOptions) => {
33
44
  },
34
45
  resolveId(source, importer) {
35
46
  if (source === VIRTUAL_HYDROGEN_CONFIG_ID) {
36
- return findHydrogenConfigPath(config.root, pluginOptions.configPath).then((hcPath) =>
37
- // This direct dependency on a real file
38
- // makes HMR work for the virtual module.
39
- this.resolve(hcPath, importer, { skipSelf: true }));
47
+ return findHydrogenConfigPath(config.root, pluginOptions.configPath).then((hcPath) => {
48
+ resolvedConfigPath = (0, vite_1.normalizePath)(hcPath);
49
+ // This direct dependency on a real file
50
+ // makes HMR work for the virtual module.
51
+ return this.resolve(hcPath, importer, { skipSelf: true });
52
+ });
40
53
  }
41
54
  if ([
42
55
  exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID,
43
56
  exports.VIRTUAL_PROXY_HYDROGEN_ROUTES_ID,
44
57
  VIRTUAL_HYDROGEN_ROUTES_ID,
45
58
  VIRTUAL_ERROR_FILE,
59
+ VIRTUAL_STREAM_ID,
46
60
  ].includes(source)) {
47
61
  // Virtual modules convention
48
62
  // https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
@@ -50,10 +64,17 @@ exports.default = (pluginOptions) => {
50
64
  }
51
65
  },
52
66
  load(id) {
67
+ if (id === '\0' + VIRTUAL_STREAM_ID) {
68
+ return {
69
+ code: process.env.WORKER
70
+ ? `export default {};`
71
+ : `export {default} from 'stream';`,
72
+ };
73
+ }
53
74
  // Likely due to a bug in Vite, but virtual modules cannot be loaded
54
75
  // directly using ssrLoadModule from a Vite plugin. It needs to be proxied as follows:
55
76
  if (id === '\0' + exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID) {
56
- return `import hc from '${VIRTUAL_HYDROGEN_CONFIG_ID}'; export default hc;`;
77
+ return `import hc from '${VIRTUAL_HYDROGEN_CONFIG_ID}'; export default typeof hc === 'function' ? hc() : hc;`;
57
78
  }
58
79
  if (id === '\0' + exports.VIRTUAL_PROXY_HYDROGEN_ROUTES_ID) {
59
80
  return `import hr from '${VIRTUAL_HYDROGEN_ROUTES_ID}'; export default hr;`;
@@ -72,7 +93,10 @@ exports.default = (pluginOptions) => {
72
93
  routesPath += '**/*.server.[jt](s|sx)';
73
94
  }
74
95
  const [dirPrefix] = routesPath.split('/*');
75
- let code = `export default {\n dirPrefix: '${dirPrefix}',\n basePath: '${hc.routes?.basePath ?? ''}',\n files: import.meta.globEager('${routesPath}')\n};`;
96
+ const importGlob = vite_js_1.isVite3
97
+ ? `import.meta.glob('${routesPath}', {eager: true})`
98
+ : `import.meta.globEager('${routesPath}')`;
99
+ let code = `export default {\n dirPrefix: '${dirPrefix}',\n basePath: '${hc.routes?.basePath ?? ''}',\n files: ${importGlob}\n};`;
76
100
  if (config.command === 'serve') {
77
101
  // Add dependency on Hydrogen config for HMR
78
102
  code += `\nimport '${VIRTUAL_HYDROGEN_CONFIG_ID}';`;
@@ -88,13 +112,27 @@ exports.default = (pluginOptions) => {
88
112
  });
89
113
  }
90
114
  },
115
+ transform(code, id) {
116
+ if (id === resolvedConfigPath) {
117
+ const s = new magic_string_1.default(code);
118
+ // Wrap in function to avoid evaluating `Oxygen.env`
119
+ // in the config until we have polyfilled it properly.
120
+ s.replace(/export\s+default\s+(\w+)\s*\(/g, (all, m1) => all.replace(m1, `() => ${m1}`));
121
+ return {
122
+ code: s.toString(),
123
+ map: s.generateMap({ file: id, source: id }),
124
+ };
125
+ }
126
+ },
91
127
  };
92
128
  async function importHydrogenConfig() {
93
129
  if (server) {
94
130
  const loaded = await server.ssrLoadModule(exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID);
95
131
  return loaded.default;
96
132
  }
97
- const { loaded } = await (0, viteception_js_1.viteception)([exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID]);
133
+ const { loaded } = await (0, viteception_js_1.viteception)([exports.VIRTUAL_PROXY_HYDROGEN_CONFIG_ID], {
134
+ root: config.root,
135
+ });
98
136
  return loaded[0].default;
99
137
  }
100
138
  };
@@ -9,10 +9,23 @@ const magic_string_1 = __importDefault(require("magic-string"));
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const fs_1 = __importDefault(require("fs"));
11
11
  const fast_glob_1 = __importDefault(require("fast-glob"));
12
+ const vite_js_1 = require("../../utilities/vite.js");
12
13
  const SSR_BUNDLE_NAME = 'index.js';
13
14
  // Keep this in the outer scope to share it
14
15
  // across client <> server builds.
15
16
  let clientBuildPath;
17
+ /* -- Plugin notes:
18
+ * This plugin simplifies the way a platform entry file imports user files. This is
19
+ * needed to write generic integrations with different platform providers.
20
+ *
21
+ * Instead of using relative paths:
22
+ * `import handleRequest from '../../<arbitrary_path>/src/App.server';`
23
+ * `import indexTemplate from '../../<arbitrary_path>/dist/client/index.html?raw';`
24
+ *
25
+ * It allows importing from a known static path which dynamically resolves the user files:
26
+ * `import {handleRequest, indexTemplate} from '@shopify/hydrogen/platforms';`
27
+ *
28
+ */
16
29
  exports.default = () => {
17
30
  let config;
18
31
  let isESM;
@@ -23,7 +36,7 @@ exports.default = () => {
23
36
  config = _config;
24
37
  if (config.build.ssr) {
25
38
  const { output = {} } = config.build.rollupOptions || {};
26
- const { format = '' } = (Array.isArray(output) ? output[0] : output) || {};
39
+ const { format = vite_js_1.isVite3 ? 'es' : '' } = (Array.isArray(output) ? output[0] : output) || {};
27
40
  isESM = Boolean(process.env.WORKER) || ['es', 'esm'].includes(format);
28
41
  }
29
42
  },
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* -- Plugin notes:
4
+ * This plugin makes sure we don't leak server logic to the browser when importing
5
+ * the `useEnvContext` utility.
6
+ */
3
7
  exports.default = () => {
4
8
  return {
5
- name: 'vite-plugin-ssr-interop',
9
+ name: 'hydrogen:ssr-interop',
6
10
  enforce: 'pre',
7
11
  transform(code, id, options = {}) {
8
12
  if (options.ssr && id.includes('foundation/ssr-interop')) {
@@ -3,6 +3,7 @@ export interface HydrogenVitePluginOptions {
3
3
  purgeQueryCacheOnBuild?: boolean;
4
4
  configPath?: string;
5
5
  optimizeBoundaries?: boolean | 'build';
6
+ assetHashVersion?: string;
6
7
  /**
7
8
  * Experimental features
8
9
  */
@@ -2,12 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.viteception = void 0;
4
4
  const vite_1 = require("vite");
5
+ const vite_js_1 = require("../utilities/vite.js");
5
6
  async function viteception(paths, options) {
6
7
  const isWorker = process.env.WORKER;
7
8
  delete process.env.WORKER;
8
9
  const server = await (0, vite_1.createServer)({
9
10
  clearScreen: false,
10
- server: { middlewareMode: 'ssr' },
11
+ server: {
12
+ middlewareMode: vite_js_1.isVite3 ? true : 'ssr',
13
+ hmr: false,
14
+ },
15
+ // @ts-ignore
16
+ appType: 'custom',
11
17
  ...options,
12
18
  });
13
19
  if (isWorker) {
@@ -0,0 +1 @@
1
+ export declare const isVite3: any;
@@ -0,0 +1,30 @@
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.isVite3 = void 0;
27
+ // `version` is only exported in Vite 3
28
+ const vite = __importStar(require("vite"));
29
+ // @ts-ignore
30
+ exports.isVite3 = vite.version?.startsWith('3.');
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "engines": {
8
8
  "node": ">=14"
9
9
  },
10
- "version": "1.2.0",
10
+ "version": "1.3.2",
11
11
  "description": "Modern custom Shopify storefronts",
12
12
  "license": "MIT",
13
13
  "main": "dist/esnext/index.js",
@@ -89,8 +89,9 @@
89
89
  "@graphql-codegen/typescript-operations": "2.3.2",
90
90
  "@graphql-typed-document-node/core": "^3.1.0",
91
91
  "@rollup/plugin-graphql": "^1.0.0",
92
- "@testing-library/jest-dom": "^5.16.4",
92
+ "@testing-library/jest-dom": "^5.16.5",
93
93
  "@testing-library/react": "^13.3.0",
94
+ "@testing-library/user-event": "^14.4.3",
94
95
  "@types/body-parser": "^1.19.2",
95
96
  "@types/connect": "^3.4.34",
96
97
  "@types/graphql": "^14.5.0",
@@ -104,13 +105,13 @@
104
105
  "babel-loader": "^8.2.2",
105
106
  "cpy-cli": "^3.1.0",
106
107
  "eslint-plugin-import": "^2.26.0",
107
- "happy-dom": "^6.0.3",
108
+ "happy-dom": "^6.0.4",
108
109
  "mkdirp": "^1.0.4",
109
110
  "npm-run-all": "^4.1.5",
110
111
  "postcss": "^8",
111
112
  "raw-loader": "^4.0.2",
112
113
  "rimraf": "^3.0.2",
113
- "vitest": "^0.18.0"
114
+ "vitest": "^0.22.1"
114
115
  },
115
116
  "peerDependencies": {
116
117
  "body-parser": "^1.20.0",
@@ -118,7 +119,7 @@
118
119
  "react": "^18.2.0",
119
120
  "react-dom": "^18.2.0",
120
121
  "serve-static": "^1.14.1",
121
- "vite": "^2.9.0"
122
+ "vite": "^2.9.0 || ^3.0.0"
122
123
  },
123
124
  "peerDependenciesMeta": {
124
125
  "body-parser": {
@@ -150,7 +151,7 @@
150
151
  "react-helmet-async": "^1.3.0",
151
152
  "serve-static": "^1.15.0",
152
153
  "set-cookie-parser": "^2.5.0",
153
- "undici": "^5.5.1",
154
+ "undici": "^5.8.2",
154
155
  "uuid": "^8.3.2",
155
156
  "vite-plugin-inspect": "^0.3.6",
156
157
  "web-streams-polyfill": "^3.2.0",