@shopify/hydrogen 0.19.0 → 0.20.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 +58 -0
- package/config.d.ts +1 -0
- 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/CartProvider/CartProvider.client.d.ts +3 -1
- package/dist/esnext/components/CartProvider/CartProvider.client.js +22 -20
- 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 +54 -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 +2 -2
- package/dist/esnext/components/Video/Video.d.ts +3 -3
- package/dist/esnext/components/Video/Video.js +7 -4
- package/dist/esnext/entry-server.d.ts +13 -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/framework/Hydration/Html.js +3 -1
- package/dist/esnext/framework/plugin.js +3 -30
- 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 +68 -3
- package/dist/esnext/framework/plugins/vite-plugin-hydration-auto-import.js +1 -4
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-config.js +5 -4
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-middleware.d.ts +1 -1
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-middleware.js +2 -3
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-rsc.d.ts +1 -0
- package/dist/esnext/framework/plugins/vite-plugin-hydrogen-rsc.js +35 -0
- package/dist/esnext/framework/plugins/vite-plugin-platform-entry.js +1 -1
- package/dist/esnext/framework/plugins/vite-plugin-ssr-interop.js +6 -3
- package/dist/esnext/storefront-api-types.d.ts +5 -3
- package/dist/esnext/storefront-api-types.js +5 -3
- package/dist/esnext/types.d.ts +3 -3
- 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 +1 -1
- package/dist/esnext/utilities/index.js +1 -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 +86 -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 +13 -1
- package/dist/node/framework/Hydration/Html.js +3 -1
- package/dist/node/framework/plugin.js +3 -53
- 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 +71 -3
- package/dist/node/framework/plugins/vite-plugin-hydration-auto-import.js +1 -4
- package/dist/node/framework/plugins/vite-plugin-hydrogen-config.js +5 -4
- package/dist/node/framework/plugins/vite-plugin-hydrogen-middleware.d.ts +1 -1
- package/dist/node/framework/plugins/vite-plugin-hydrogen-middleware.js +2 -3
- package/dist/node/framework/plugins/vite-plugin-hydrogen-rsc.d.ts +1 -0
- package/dist/node/framework/plugins/vite-plugin-hydrogen-rsc.js +41 -0
- package/dist/node/framework/plugins/vite-plugin-platform-entry.js +1 -1
- package/dist/node/framework/plugins/vite-plugin-ssr-interop.js +6 -3
- package/dist/node/storefront-api-types.d.ts +5 -3
- package/dist/node/storefront-api-types.js +5 -3
- package/dist/node/types.d.ts +3 -3
- 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 +1 -1
- package/dist/node/utilities/index.js +1 -2
- 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 +1 -1
- package/vendor/react-server-dom-vite/cjs/react-server-dom-vite-plugin.js +173 -29
- 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 +173 -29
|
@@ -17,46 +17,150 @@ var vite = require('vite');
|
|
|
17
17
|
var fs = require('fs');
|
|
18
18
|
var path = require('path');
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
21
|
+
if (!o) return;
|
|
22
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
23
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
24
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
25
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
26
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _arrayLikeToArray(arr, len) {
|
|
30
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
31
|
+
|
|
32
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
33
|
+
|
|
34
|
+
return arr2;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
38
|
+
var it;
|
|
39
|
+
|
|
40
|
+
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
|
41
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
42
|
+
if (it) o = it;
|
|
43
|
+
var i = 0;
|
|
44
|
+
|
|
45
|
+
var F = function () {};
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
s: F,
|
|
49
|
+
n: function () {
|
|
50
|
+
if (i >= o.length) return {
|
|
51
|
+
done: true
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
done: false,
|
|
55
|
+
value: o[i++]
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
e: function (e) {
|
|
59
|
+
throw e;
|
|
60
|
+
},
|
|
61
|
+
f: F
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var normalCompletion = true,
|
|
69
|
+
didErr = false,
|
|
70
|
+
err;
|
|
71
|
+
return {
|
|
72
|
+
s: function () {
|
|
73
|
+
it = o[Symbol.iterator]();
|
|
74
|
+
},
|
|
75
|
+
n: function () {
|
|
76
|
+
var step = it.next();
|
|
77
|
+
normalCompletion = step.done;
|
|
78
|
+
return step;
|
|
79
|
+
},
|
|
80
|
+
e: function (e) {
|
|
81
|
+
didErr = true;
|
|
82
|
+
err = e;
|
|
83
|
+
},
|
|
84
|
+
f: function () {
|
|
85
|
+
try {
|
|
86
|
+
if (!normalCompletion && it.return != null) it.return();
|
|
87
|
+
} finally {
|
|
88
|
+
if (didErr) throw err;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
21
94
|
var rscViteFileRE = /\/react-server-dom-vite.js/;
|
|
22
95
|
function ReactFlightVitePlugin() {
|
|
23
96
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
24
|
-
_ref$clientComponentP = _ref.clientComponentPaths,
|
|
25
|
-
clientComponentPaths = _ref$clientComponentP === void 0 ? [] : _ref$clientComponentP,
|
|
26
97
|
_ref$isServerComponen = _ref.isServerComponentImporterAllowed,
|
|
27
98
|
isServerComponentImporterAllowed = _ref$isServerComponen === void 0 ? function (importer) {
|
|
28
99
|
return false;
|
|
29
|
-
} : _ref$isServerComponen
|
|
100
|
+
} : _ref$isServerComponen,
|
|
101
|
+
_ref$isClientComponen = _ref.isClientComponent,
|
|
102
|
+
isClientComponent = _ref$isClientComponen === void 0 ? function (id) {
|
|
103
|
+
return /\.client\.[jt]sx?($|\?)/.test(id);
|
|
104
|
+
} : _ref$isClientComponen,
|
|
105
|
+
findClientComponentsForClientBuild = _ref.findClientComponentsForClientBuild;
|
|
30
106
|
|
|
31
107
|
var config;
|
|
108
|
+
var server;
|
|
109
|
+
var invalidateTimeout;
|
|
110
|
+
var absoluteImporterPath;
|
|
111
|
+
|
|
112
|
+
function invalidateImporter() {
|
|
113
|
+
clearTimeout(invalidateTimeout);
|
|
114
|
+
invalidateTimeout = setTimeout(function () {
|
|
115
|
+
return server.watcher.emit('change', absoluteImporterPath);
|
|
116
|
+
}, 100);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function wrapIfClientComponent(id) {
|
|
120
|
+
var handle = function (isClient) {
|
|
121
|
+
if (!isClient) return null;
|
|
122
|
+
|
|
123
|
+
if (server) {
|
|
124
|
+
var moduleNode = server.moduleGraph.getModuleById(id);
|
|
125
|
+
|
|
126
|
+
if (!moduleNode.__isClientComponent) {
|
|
127
|
+
moduleNode.__isClientComponent = true;
|
|
128
|
+
if (absoluteImporterPath) invalidateImporter();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return proxyClientComponent(id.split('?')[0]);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
var tmp = isClientComponent(id);
|
|
136
|
+
return typeof tmp === 'boolean' ? handle(tmp) : tmp.then(handle);
|
|
137
|
+
}
|
|
138
|
+
|
|
32
139
|
return {
|
|
33
140
|
name: 'vite-plugin-react-server-components',
|
|
34
141
|
enforce: 'pre',
|
|
142
|
+
configureServer: function (_server) {
|
|
143
|
+
server = _server;
|
|
144
|
+
},
|
|
35
145
|
configResolved: function (_config) {
|
|
36
146
|
config = _config; // By pushing this plugin at the end of the existing array,
|
|
37
147
|
// we enforce running it *after* Vite resolves import.meta.glob.
|
|
38
148
|
|
|
39
149
|
config.plugins.push(hashImportsPlugin);
|
|
40
150
|
},
|
|
41
|
-
resolveId:
|
|
151
|
+
resolveId: function (source, importer) {
|
|
42
152
|
if (!importer) return null;
|
|
43
153
|
/**
|
|
44
154
|
* Throw errors when non-Server Components try to load Server Components.
|
|
45
155
|
*/
|
|
46
156
|
|
|
47
|
-
if (/\.server(\.[jt]sx?)?$/.test(source) && !(/(\.server\.[jt]sx?|
|
|
157
|
+
if (/\.server(\.[jt]sx?)?$/.test(source) && !(/(\.server\.[jt]sx?|index\.html)$/.test(importer) || isServerComponentImporterAllowed(importer, source))) {
|
|
48
158
|
throw new Error("Cannot import " + source + " from \"" + importer + "\". " + 'By react-server convention, .server.js files can only be imported from other .server.js files. ' + 'That way nobody accidentally sends these to the client by indirectly importing it.');
|
|
49
159
|
}
|
|
50
160
|
},
|
|
51
|
-
load:
|
|
161
|
+
load: function (id) {
|
|
52
162
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (/\.client\.[jt]sx?$/.test(id)) {
|
|
56
|
-
return proxyClientComponent(id);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return null;
|
|
163
|
+
return options.ssr && shouldCheckClientComponent(id) ? wrapIfClientComponent(id) : null;
|
|
60
164
|
},
|
|
61
165
|
transform: function (code, id) {
|
|
62
166
|
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
@@ -79,20 +183,30 @@ function ReactFlightVitePlugin() {
|
|
|
79
183
|
return code.replace(INJECTING_RE, 'globalThis.__COMPONENT_INDEX');
|
|
80
184
|
}
|
|
81
185
|
|
|
82
|
-
var
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
186
|
+
var injectGlobs = function (clientComponents) {
|
|
187
|
+
var importerPath = path.dirname(id);
|
|
188
|
+
var importers = clientComponents.map(function (absolutePath) {
|
|
189
|
+
return vite.normalizePath(path.relative(importerPath, absolutePath));
|
|
190
|
+
});
|
|
191
|
+
var injectedGlobs = "Object.assign(Object.create(null), " + importers.map(function (glob) {
|
|
192
|
+
return (// Mark the globs to modify the result after Vite resolves them.
|
|
193
|
+
"/* HASH_BEGIN */ " + ("import.meta.glob('" + vite.normalizePath(glob) + "') /* HASH_END */")
|
|
194
|
+
);
|
|
195
|
+
}).join(', ') + ");";
|
|
196
|
+
return code.replace(INJECTING_RE, injectedGlobs);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
if (config.command === 'serve') {
|
|
200
|
+
absoluteImporterPath = id.split('?')[0];
|
|
201
|
+
return injectGlobs(findClientComponentsForDev(server));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (!findClientComponentsForClientBuild) {
|
|
205
|
+
throw new Error('[react-server-dom-vite] Parameter findClientComponentsForClientBuild is required for client build');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
var tmp = findClientComponentsForClientBuild(config);
|
|
209
|
+
return Array.isArray(tmp) ? injectGlobs(tmp) : tmp.then(injectGlobs);
|
|
96
210
|
}
|
|
97
211
|
}
|
|
98
212
|
};
|
|
@@ -151,6 +265,34 @@ async function proxyClientComponent(filepath, src) {
|
|
|
151
265
|
});
|
|
152
266
|
return proxyCode;
|
|
153
267
|
}
|
|
268
|
+
|
|
269
|
+
function shouldCheckClientComponent(id) {
|
|
270
|
+
return /\.[jt]sx?($|\?)/.test(id) && !/[&?]no-proxy($|&)/.test(id);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function findClientComponentsForDev(server) {
|
|
274
|
+
var clientComponents = []; // eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
|
275
|
+
|
|
276
|
+
var _iterator = _createForOfIteratorHelper(server.moduleGraph.fileToModulesMap.values()),
|
|
277
|
+
_step;
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
281
|
+
var set = _step.value;
|
|
282
|
+
var clientModule = Array.from(set).find(function (moduleNode) {
|
|
283
|
+
return moduleNode.__isClientComponent;
|
|
284
|
+
});
|
|
285
|
+
if (clientModule) clientComponents.push(clientModule.file);
|
|
286
|
+
}
|
|
287
|
+
} catch (err) {
|
|
288
|
+
_iterator.e(err);
|
|
289
|
+
} finally {
|
|
290
|
+
_iterator.f();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return clientComponents;
|
|
294
|
+
}
|
|
295
|
+
|
|
154
296
|
var hashImportsPlugin = {
|
|
155
297
|
name: 'vite-plugin-react-server-components-hash-imports',
|
|
156
298
|
enforce: 'post',
|
|
@@ -165,6 +307,8 @@ var hashImportsPlugin = {
|
|
|
165
307
|
});
|
|
166
308
|
}
|
|
167
309
|
}
|
|
168
|
-
};
|
|
310
|
+
}; // This can be used in custom findClientComponentsForClientBuild implementations
|
|
311
|
+
|
|
312
|
+
ReactFlightVitePlugin.findClientComponentsFromServer = findClientComponentsForDev;
|
|
169
313
|
|
|
170
314
|
module.exports = ReactFlightVitePlugin;
|
|
@@ -65,6 +65,8 @@ function wrapInClientProxy(_ref) {
|
|
|
65
65
|
var moduleRef = createModuleReference(id, value, name, isDefault);
|
|
66
66
|
|
|
67
67
|
var get = function (target, prop, receiver) {
|
|
68
|
+
if (prop === '$$unwrappedValue') return value;
|
|
69
|
+
if (prop === '$$moduleReference') return moduleRef;
|
|
68
70
|
return Reflect.get(isRsc() ? moduleRef : target, prop, receiver);
|
|
69
71
|
};
|
|
70
72
|
|
|
@@ -13,46 +13,150 @@ import { normalizePath, transformWithEsbuild } from 'vite';
|
|
|
13
13
|
import { promises } from 'fs';
|
|
14
14
|
import path from 'path';
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
17
|
+
if (!o) return;
|
|
18
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
19
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
20
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
21
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
22
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function _arrayLikeToArray(arr, len) {
|
|
26
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
27
|
+
|
|
28
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
29
|
+
|
|
30
|
+
return arr2;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
34
|
+
var it;
|
|
35
|
+
|
|
36
|
+
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
|
37
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
38
|
+
if (it) o = it;
|
|
39
|
+
var i = 0;
|
|
40
|
+
|
|
41
|
+
var F = function () {};
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
s: F,
|
|
45
|
+
n: function () {
|
|
46
|
+
if (i >= o.length) return {
|
|
47
|
+
done: true
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
done: false,
|
|
51
|
+
value: o[i++]
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
e: function (e) {
|
|
55
|
+
throw e;
|
|
56
|
+
},
|
|
57
|
+
f: F
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
var normalCompletion = true,
|
|
65
|
+
didErr = false,
|
|
66
|
+
err;
|
|
67
|
+
return {
|
|
68
|
+
s: function () {
|
|
69
|
+
it = o[Symbol.iterator]();
|
|
70
|
+
},
|
|
71
|
+
n: function () {
|
|
72
|
+
var step = it.next();
|
|
73
|
+
normalCompletion = step.done;
|
|
74
|
+
return step;
|
|
75
|
+
},
|
|
76
|
+
e: function (e) {
|
|
77
|
+
didErr = true;
|
|
78
|
+
err = e;
|
|
79
|
+
},
|
|
80
|
+
f: function () {
|
|
81
|
+
try {
|
|
82
|
+
if (!normalCompletion && it.return != null) it.return();
|
|
83
|
+
} finally {
|
|
84
|
+
if (didErr) throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
17
90
|
var rscViteFileRE = /\/react-server-dom-vite.js/;
|
|
18
91
|
function ReactFlightVitePlugin() {
|
|
19
92
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
20
|
-
_ref$clientComponentP = _ref.clientComponentPaths,
|
|
21
|
-
clientComponentPaths = _ref$clientComponentP === void 0 ? [] : _ref$clientComponentP,
|
|
22
93
|
_ref$isServerComponen = _ref.isServerComponentImporterAllowed,
|
|
23
94
|
isServerComponentImporterAllowed = _ref$isServerComponen === void 0 ? function (importer) {
|
|
24
95
|
return false;
|
|
25
|
-
} : _ref$isServerComponen
|
|
96
|
+
} : _ref$isServerComponen,
|
|
97
|
+
_ref$isClientComponen = _ref.isClientComponent,
|
|
98
|
+
isClientComponent = _ref$isClientComponen === void 0 ? function (id) {
|
|
99
|
+
return /\.client\.[jt]sx?($|\?)/.test(id);
|
|
100
|
+
} : _ref$isClientComponen,
|
|
101
|
+
findClientComponentsForClientBuild = _ref.findClientComponentsForClientBuild;
|
|
26
102
|
|
|
27
103
|
var config;
|
|
104
|
+
var server;
|
|
105
|
+
var invalidateTimeout;
|
|
106
|
+
var absoluteImporterPath;
|
|
107
|
+
|
|
108
|
+
function invalidateImporter() {
|
|
109
|
+
clearTimeout(invalidateTimeout);
|
|
110
|
+
invalidateTimeout = setTimeout(function () {
|
|
111
|
+
return server.watcher.emit('change', absoluteImporterPath);
|
|
112
|
+
}, 100);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function wrapIfClientComponent(id) {
|
|
116
|
+
var handle = function (isClient) {
|
|
117
|
+
if (!isClient) return null;
|
|
118
|
+
|
|
119
|
+
if (server) {
|
|
120
|
+
var moduleNode = server.moduleGraph.getModuleById(id);
|
|
121
|
+
|
|
122
|
+
if (!moduleNode.__isClientComponent) {
|
|
123
|
+
moduleNode.__isClientComponent = true;
|
|
124
|
+
if (absoluteImporterPath) invalidateImporter();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return proxyClientComponent(id.split('?')[0]);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
var tmp = isClientComponent(id);
|
|
132
|
+
return typeof tmp === 'boolean' ? handle(tmp) : tmp.then(handle);
|
|
133
|
+
}
|
|
134
|
+
|
|
28
135
|
return {
|
|
29
136
|
name: 'vite-plugin-react-server-components',
|
|
30
137
|
enforce: 'pre',
|
|
138
|
+
configureServer: function (_server) {
|
|
139
|
+
server = _server;
|
|
140
|
+
},
|
|
31
141
|
configResolved: function (_config) {
|
|
32
142
|
config = _config; // By pushing this plugin at the end of the existing array,
|
|
33
143
|
// we enforce running it *after* Vite resolves import.meta.glob.
|
|
34
144
|
|
|
35
145
|
config.plugins.push(hashImportsPlugin);
|
|
36
146
|
},
|
|
37
|
-
resolveId:
|
|
147
|
+
resolveId: function (source, importer) {
|
|
38
148
|
if (!importer) return null;
|
|
39
149
|
/**
|
|
40
150
|
* Throw errors when non-Server Components try to load Server Components.
|
|
41
151
|
*/
|
|
42
152
|
|
|
43
|
-
if (/\.server(\.[jt]sx?)?$/.test(source) && !(/(\.server\.[jt]sx?|
|
|
153
|
+
if (/\.server(\.[jt]sx?)?$/.test(source) && !(/(\.server\.[jt]sx?|index\.html)$/.test(importer) || isServerComponentImporterAllowed(importer, source))) {
|
|
44
154
|
throw new Error("Cannot import " + source + " from \"" + importer + "\". " + 'By react-server convention, .server.js files can only be imported from other .server.js files. ' + 'That way nobody accidentally sends these to the client by indirectly importing it.');
|
|
45
155
|
}
|
|
46
156
|
},
|
|
47
|
-
load:
|
|
157
|
+
load: function (id) {
|
|
48
158
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (/\.client\.[jt]sx?$/.test(id)) {
|
|
52
|
-
return proxyClientComponent(id);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return null;
|
|
159
|
+
return options.ssr && shouldCheckClientComponent(id) ? wrapIfClientComponent(id) : null;
|
|
56
160
|
},
|
|
57
161
|
transform: function (code, id) {
|
|
58
162
|
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
@@ -75,20 +179,30 @@ function ReactFlightVitePlugin() {
|
|
|
75
179
|
return code.replace(INJECTING_RE, 'globalThis.__COMPONENT_INDEX');
|
|
76
180
|
}
|
|
77
181
|
|
|
78
|
-
var
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
182
|
+
var injectGlobs = function (clientComponents) {
|
|
183
|
+
var importerPath = path.dirname(id);
|
|
184
|
+
var importers = clientComponents.map(function (absolutePath) {
|
|
185
|
+
return normalizePath(path.relative(importerPath, absolutePath));
|
|
186
|
+
});
|
|
187
|
+
var injectedGlobs = "Object.assign(Object.create(null), " + importers.map(function (glob) {
|
|
188
|
+
return (// Mark the globs to modify the result after Vite resolves them.
|
|
189
|
+
"/* HASH_BEGIN */ " + ("import.meta.glob('" + normalizePath(glob) + "') /* HASH_END */")
|
|
190
|
+
);
|
|
191
|
+
}).join(', ') + ");";
|
|
192
|
+
return code.replace(INJECTING_RE, injectedGlobs);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
if (config.command === 'serve') {
|
|
196
|
+
absoluteImporterPath = id.split('?')[0];
|
|
197
|
+
return injectGlobs(findClientComponentsForDev(server));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!findClientComponentsForClientBuild) {
|
|
201
|
+
throw new Error('[react-server-dom-vite] Parameter findClientComponentsForClientBuild is required for client build');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
var tmp = findClientComponentsForClientBuild(config);
|
|
205
|
+
return Array.isArray(tmp) ? injectGlobs(tmp) : tmp.then(injectGlobs);
|
|
92
206
|
}
|
|
93
207
|
}
|
|
94
208
|
};
|
|
@@ -147,6 +261,34 @@ async function proxyClientComponent(filepath, src) {
|
|
|
147
261
|
});
|
|
148
262
|
return proxyCode;
|
|
149
263
|
}
|
|
264
|
+
|
|
265
|
+
function shouldCheckClientComponent(id) {
|
|
266
|
+
return /\.[jt]sx?($|\?)/.test(id) && !/[&?]no-proxy($|&)/.test(id);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function findClientComponentsForDev(server) {
|
|
270
|
+
var clientComponents = []; // eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
|
271
|
+
|
|
272
|
+
var _iterator = _createForOfIteratorHelper(server.moduleGraph.fileToModulesMap.values()),
|
|
273
|
+
_step;
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
277
|
+
var set = _step.value;
|
|
278
|
+
var clientModule = Array.from(set).find(function (moduleNode) {
|
|
279
|
+
return moduleNode.__isClientComponent;
|
|
280
|
+
});
|
|
281
|
+
if (clientModule) clientComponents.push(clientModule.file);
|
|
282
|
+
}
|
|
283
|
+
} catch (err) {
|
|
284
|
+
_iterator.e(err);
|
|
285
|
+
} finally {
|
|
286
|
+
_iterator.f();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return clientComponents;
|
|
290
|
+
}
|
|
291
|
+
|
|
150
292
|
var hashImportsPlugin = {
|
|
151
293
|
name: 'vite-plugin-react-server-components-hash-imports',
|
|
152
294
|
enforce: 'post',
|
|
@@ -161,6 +303,8 @@ var hashImportsPlugin = {
|
|
|
161
303
|
});
|
|
162
304
|
}
|
|
163
305
|
}
|
|
164
|
-
};
|
|
306
|
+
}; // This can be used in custom findClientComponentsForClientBuild implementations
|
|
307
|
+
|
|
308
|
+
ReactFlightVitePlugin.findClientComponentsFromServer = findClientComponentsForDev;
|
|
165
309
|
|
|
166
310
|
export default ReactFlightVitePlugin;
|