@plasmicapp/loader-react 1.0.43 → 1.0.47

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.
@@ -1,11 +1,15 @@
1
1
  import { registerComponent } from '@plasmicapp/host';
2
2
  export { PlasmicCanvasContext, PlasmicCanvasHost, repeatedElement } from '@plasmicapp/host';
3
+ import { PlasmicQueryDataProvider, PrepassContext } from '@plasmicapp/query';
4
+ export { usePlasmicQueryData } from '@plasmicapp/query';
3
5
  import { getBundleSubset, Registry, PlasmicModulesFetcher } from '@plasmicapp/loader-core';
4
6
  import React__default, { useState, useCallback, useEffect, useRef, useMemo, createElement, memo, useContext, createContext } from 'react';
5
7
  import ReactDOM from 'react-dom';
6
8
  import * as jsxDevRuntime from 'react/jsx-dev-runtime';
7
9
  import * as jsxRuntime from 'react/jsx-runtime';
8
10
  import pascalcase from 'pascalcase';
11
+ import { isFragment } from 'react-is';
12
+ import prepass from 'react-ssr-prepass';
9
13
  import ReactDOMServer from 'react-dom/server';
10
14
 
11
15
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
@@ -62,6 +66,22 @@ function _extends() {
62
66
  return _extends.apply(this, arguments);
63
67
  }
64
68
 
69
+ function _inheritsLoose(subClass, superClass) {
70
+ subClass.prototype = Object.create(superClass.prototype);
71
+ subClass.prototype.constructor = subClass;
72
+
73
+ _setPrototypeOf(subClass, superClass);
74
+ }
75
+
76
+ function _setPrototypeOf(o, p) {
77
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
78
+ o.__proto__ = p;
79
+ return o;
80
+ };
81
+
82
+ return _setPrototypeOf(o, p);
83
+ }
84
+
65
85
  function _unsupportedIterableToArray(o, minLen) {
66
86
  if (!o) return;
67
87
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -1190,7 +1210,9 @@ function PlasmicRootProvider(props) {
1190
1210
  prefetchedData = props.prefetchedData,
1191
1211
  children = props.children,
1192
1212
  skipCss = props.skipCss,
1193
- skipFonts = props.skipFonts;
1213
+ skipFonts = props.skipFonts,
1214
+ prefetchedQueryData = props.prefetchedQueryData,
1215
+ suspenseForQueryData = props.suspenseForQueryData;
1194
1216
  var loader = props.loader.__internal;
1195
1217
 
1196
1218
  if (prefetchedData) {
@@ -1203,13 +1225,16 @@ function PlasmicRootProvider(props) {
1203
1225
  loader: loader
1204
1226
  };
1205
1227
  }, [globalVariants, loader]);
1206
- return createElement(PlasmicRootContext.Provider, {
1228
+ return createElement(PlasmicQueryDataProvider, {
1229
+ prefetchedCache: prefetchedQueryData,
1230
+ suspense: suspenseForQueryData
1231
+ }, createElement(PlasmicRootContext.Provider, {
1207
1232
  value: value
1208
1233
  }, !skipCss && createElement(PlasmicCss, {
1209
1234
  loader: loader,
1210
1235
  prefetchedData: prefetchedData,
1211
1236
  skipFonts: skipFonts
1212
- }), children);
1237
+ }), children));
1213
1238
  }
1214
1239
  /**
1215
1240
  * Inject all css modules as <style/> tags. We can't use the usual styleInjector postcss
@@ -2101,6 +2126,220 @@ function PlasmicComponent(props) {
2101
2126
  return element;
2102
2127
  }
2103
2128
 
2129
+ /**
2130
+ * Performs a prepass over Plasmic content, kicking off the necessary
2131
+ * data fetches, and populating the fetched data into a cache. This
2132
+ * cache can be passed as prefetchedQueryData into PlasmicRootProvider.
2133
+ *
2134
+ * To limit rendering errors that can occur when you do this, we recommend
2135
+ * that you pass in _only_ the PlasmicComponents that you are planning to use
2136
+ * as the argument. For example:
2137
+ *
2138
+ * const cache = await extractPlasmicQueryData(
2139
+ * <PlasmicRootProvider loader={PLASMIC} prefetchedData={plasmicData}>
2140
+ * <PlasmicComponent component="Home" componentProps={{
2141
+ * // Specify the component prop overrides you are planning to use
2142
+ * // to render the page, as they may change what data is fetched.
2143
+ * ...
2144
+ * }} />
2145
+ * <PlasmicComponent component="NavBar" componentProps={{
2146
+ * ...
2147
+ * }} />
2148
+ * ...
2149
+ * </PlasmicRootProvider>
2150
+ * );
2151
+ *
2152
+ * If your PlasmicComponent will be wrapping components that require special
2153
+ * context set up, you should also wrap the element above with those context
2154
+ * providers.
2155
+ *
2156
+ * You should avoid passing in elements that are not related to Plasmic, as any
2157
+ * rendering errors from those elements during the prepass may result in data
2158
+ * not being populated in the cache.
2159
+ *
2160
+ * @param element a React element containing instances of PlasmicComponent.
2161
+ * Will attempt to satisfy all data needs from usePlasmicDataQuery()
2162
+ * in this element tree.
2163
+ * @returns an object mapping query key to fetched data
2164
+ */
2165
+
2166
+ function extractPlasmicQueryData(_x) {
2167
+ return _extractPlasmicQueryData.apply(this, arguments);
2168
+ }
2169
+ /**
2170
+ * Runs react-ssr-prepass on `element`, while isolating rendering errors
2171
+ * as much as possible for each PlasmicComponent instance.
2172
+ */
2173
+
2174
+ function _extractPlasmicQueryData() {
2175
+ _extractPlasmicQueryData = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(element) {
2176
+ var cache;
2177
+ return runtime_1.wrap(function _callee$(_context) {
2178
+ while (1) {
2179
+ switch (_context.prev = _context.next) {
2180
+ case 0:
2181
+ cache = {};
2182
+ _context.prev = 1;
2183
+ _context.next = 4;
2184
+ return plasmicPrepass(React__default.createElement(PrepassContext.Provider, {
2185
+ value: {
2186
+ cache: cache
2187
+ }
2188
+ }, element));
2189
+
2190
+ case 4:
2191
+ _context.next = 9;
2192
+ break;
2193
+
2194
+ case 6:
2195
+ _context.prev = 6;
2196
+ _context.t0 = _context["catch"](1);
2197
+ console.warn("PLASMIC: Error encountered while pre-rendering", _context.t0);
2198
+
2199
+ case 9:
2200
+ return _context.abrupt("return", Object.fromEntries(Array.from(Object.entries(cache)).map(function (_ref4) {
2201
+ var key = _ref4[0],
2202
+ data = _ref4[1].data;
2203
+ return [key, data];
2204
+ }).filter(function (_ref5) {
2205
+ var data = _ref5[1];
2206
+ return !!data;
2207
+ })));
2208
+
2209
+ case 10:
2210
+ case "end":
2211
+ return _context.stop();
2212
+ }
2213
+ }
2214
+ }, _callee, null, [[1, 6]]);
2215
+ }));
2216
+ return _extractPlasmicQueryData.apply(this, arguments);
2217
+ }
2218
+
2219
+ function plasmicPrepass(_x2) {
2220
+ return _plasmicPrepass.apply(this, arguments);
2221
+ }
2222
+ /**
2223
+ * Returns a clone of the element tree, where componentProps of PlasmicComponent
2224
+ * has been processed such that any React elements found are wrapped in
2225
+ * an error boundary. Makes it possible to isolate rendering errors while still
2226
+ * finishing as much data fetched as possible.
2227
+ */
2228
+
2229
+ function _plasmicPrepass() {
2230
+ _plasmicPrepass = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(element) {
2231
+ return runtime_1.wrap(function _callee2$(_context2) {
2232
+ while (1) {
2233
+ switch (_context2.prev = _context2.next) {
2234
+ case 0:
2235
+ _context2.next = 2;
2236
+ return prepass(buildPlasmicPrepassElement(element));
2237
+
2238
+ case 2:
2239
+ case "end":
2240
+ return _context2.stop();
2241
+ }
2242
+ }
2243
+ }, _callee2);
2244
+ }));
2245
+ return _plasmicPrepass.apply(this, arguments);
2246
+ }
2247
+
2248
+ function buildPlasmicPrepassElement(element) {
2249
+ return React__default.createElement(GenericErrorBoundary, null, processReactElement(element));
2250
+ }
2251
+
2252
+ function processReactElement(element) {
2253
+ if (element.type === PlasmicComponent) {
2254
+ return React__default.cloneElement(element, processPlasmicComponentProps(element.props));
2255
+ } else {
2256
+ return React__default.cloneElement(element, processComponentProps(element.props));
2257
+ }
2258
+ }
2259
+
2260
+ function processComponentProps(props) {
2261
+ if (!props || typeof props !== 'object') {
2262
+ return props;
2263
+ }
2264
+
2265
+ var processValue = function processValue(value, depth) {
2266
+ if (depth > 2) {
2267
+ return value;
2268
+ }
2269
+
2270
+ if (React__default.isValidElement(value)) {
2271
+ return processReactElement(value);
2272
+ } else if (Array.isArray(value)) {
2273
+ return value.map(function (v) {
2274
+ return processValue(v, depth + 1);
2275
+ });
2276
+ } else if (isLiteralObject(value)) {
2277
+ return Object.fromEntries(Object.entries(value).map(function (_ref) {
2278
+ var k = _ref[0],
2279
+ v = _ref[1];
2280
+ return [k, processValue(v, depth + 1)];
2281
+ }));
2282
+ } else {
2283
+ return value;
2284
+ }
2285
+ };
2286
+
2287
+ return Object.fromEntries(Object.entries(props).map(function (_ref2) {
2288
+ var k = _ref2[0],
2289
+ v = _ref2[1];
2290
+ return [k, processValue(v, 1)];
2291
+ }));
2292
+ }
2293
+
2294
+ var GenericErrorBoundary = /*#__PURE__*/function (_React$Component) {
2295
+ _inheritsLoose(GenericErrorBoundary, _React$Component);
2296
+
2297
+ function GenericErrorBoundary(props) {
2298
+ return _React$Component.call(this, props) || this;
2299
+ }
2300
+
2301
+ var _proto = GenericErrorBoundary.prototype;
2302
+
2303
+ _proto.componentDidCatch = function componentDidCatch(error) {
2304
+ console.log("Plasmic: Encountered error while prepass rendering:", error);
2305
+ };
2306
+
2307
+ _proto.render = function render() {
2308
+ return this.props.children;
2309
+ };
2310
+
2311
+ return GenericErrorBoundary;
2312
+ }(React__default.Component);
2313
+ /**
2314
+ * To process the componentProps passed to PlasmicComponent, wrap any
2315
+ * React element we find in <GenericErrorBoundary />.
2316
+ */
2317
+
2318
+
2319
+ function processPlasmicComponentProps(x) {
2320
+ if (!x) {
2321
+ return x;
2322
+ } else if (isFragment(x)) {
2323
+ return React__default.createElement(React__default.Fragment, null, React__default.Children.map(x.props.children, processPlasmicComponentProps));
2324
+ } else if (React__default.isValidElement(x)) {
2325
+ return React__default.createElement(GenericErrorBoundary, null, x);
2326
+ } else if (Array.isArray(x)) {
2327
+ return x.map(processPlasmicComponentProps);
2328
+ } else if (isLiteralObject(x)) {
2329
+ return Object.fromEntries(Object.entries(x).map(function (_ref3) {
2330
+ var key = _ref3[0],
2331
+ val = _ref3[1];
2332
+ return [key, processPlasmicComponentProps(val)];
2333
+ }));
2334
+ } else {
2335
+ return x;
2336
+ }
2337
+ }
2338
+
2339
+ function isLiteralObject(obj) {
2340
+ return !!obj && typeof obj === 'object' && obj.constructor === Object;
2341
+ }
2342
+
2104
2343
  function renderToElement(_x, _x2, _x3, _x4) {
2105
2344
  return _renderToElement.apply(this, arguments);
2106
2345
  }
@@ -2187,5 +2426,5 @@ function makeElement(loader, lookup, opts) {
2187
2426
  }));
2188
2427
  }
2189
2428
 
2190
- export { PlasmicComponent, PlasmicComponentLoader, PlasmicRootProvider, convertBundlesToComponentRenderData, hydrateFromElement, initPlasmicLoader, renderToElement, renderToString, usePlasmicComponent };
2429
+ export { PlasmicComponent, PlasmicComponentLoader, PlasmicRootProvider, convertBundlesToComponentRenderData, extractPlasmicQueryData, hydrateFromElement, initPlasmicLoader, plasmicPrepass, renderToElement, renderToString, usePlasmicComponent };
2191
2430
  //# sourceMappingURL=loader-react.esm.js.map