@plasmicapp/loader-react 1.0.41 → 1.0.45

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.
@@ -14,12 +14,6 @@ export interface GlobalVariantSpec {
14
14
  * or application.
15
15
  */
16
16
  export declare function PlasmicRootProvider(props: {
17
- /**
18
- * If you have pre-fetched component data via PlasmicComponentLoader,
19
- * you can pass them in here; PlasmicComponent will avoid fetching
20
- * component data that have already been pre-fetched.
21
- */
22
- prefetchedData?: ComponentRenderData;
23
17
  /**
24
18
  * The global PlasmicComponentLoader instance you created via
25
19
  * initPlasmicLoader().
@@ -38,6 +32,22 @@ export declare function PlasmicRootProvider(props: {
38
32
  * If true, will skip installing fonts
39
33
  */
40
34
  skipFonts?: boolean;
35
+ /**
36
+ * If you have pre-fetched component data via PlasmicComponentLoader,
37
+ * you can pass them in here; PlasmicComponent will avoid fetching
38
+ * component data that have already been pre-fetched.
39
+ */
40
+ prefetchedData?: ComponentRenderData;
41
+ /**
42
+ * If you have pre-fetched data that are needed by usePlasmicQueryData(),
43
+ * then pass in the pre-fetched cache here, mapping query key to fetched data.
44
+ */
45
+ prefetchedQueryData?: Record<string, any>;
46
+ /**
47
+ * Specifies whether usePlasmicQueryData() should be operating in suspense mode
48
+ * (throwing promises).
49
+ */
50
+ suspenseForQueryData?: boolean;
41
51
  }): JSX.Element;
42
52
  export declare function usePlasmicRootContext(): PlasmicRootContextValue | undefined;
43
53
  export {};
package/dist/index.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export { PlasmicCanvasContext, PlasmicCanvasHost, PrimitiveType, PropType, repeatedElement, } from '@plasmicapp/host';
2
2
  export { ComponentMeta, PageMeta, PageMetadata } from '@plasmicapp/loader-core';
3
+ export { usePlasmicQueryData } from '@plasmicapp/query';
3
4
  export { convertBundlesToComponentRenderData } from './bundles';
4
5
  export { ComponentRenderData, InitOptions, initPlasmicLoader, PlasmicComponentLoader, } from './loader';
5
6
  export { PlasmicComponent } from './PlasmicComponent';
6
7
  export { GlobalVariantSpec, PlasmicRootProvider } from './PlasmicRootProvider';
8
+ export { extractPlasmicQueryData, plasmicPrepass } from './prepass';
7
9
  export { hydrateFromElement, renderToElement, renderToString } from './render';
8
10
  export { usePlasmicComponent } from './usePlasmicComponent';
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
6
 
7
7
  var host = require('@plasmicapp/host');
8
+ var query = require('@plasmicapp/query');
8
9
  var loaderCore = require('@plasmicapp/loader-core');
9
10
  var React = require('react');
10
11
  var React__default = _interopDefault(React);
@@ -12,6 +13,8 @@ var ReactDOM = _interopDefault(require('react-dom'));
12
13
  var jsxDevRuntime = require('react/jsx-dev-runtime');
13
14
  var jsxRuntime = require('react/jsx-runtime');
14
15
  var pascalcase = _interopDefault(require('pascalcase'));
16
+ var reactIs = require('react-is');
17
+ var prepass = _interopDefault(require('react-ssr-prepass'));
15
18
  var ReactDOMServer = _interopDefault(require('react-dom/server'));
16
19
 
17
20
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
@@ -68,6 +71,22 @@ function _extends() {
68
71
  return _extends.apply(this, arguments);
69
72
  }
70
73
 
74
+ function _inheritsLoose(subClass, superClass) {
75
+ subClass.prototype = Object.create(superClass.prototype);
76
+ subClass.prototype.constructor = subClass;
77
+
78
+ _setPrototypeOf(subClass, superClass);
79
+ }
80
+
81
+ function _setPrototypeOf(o, p) {
82
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
83
+ o.__proto__ = p;
84
+ return o;
85
+ };
86
+
87
+ return _setPrototypeOf(o, p);
88
+ }
89
+
71
90
  function _unsupportedIterableToArray(o, minLen) {
72
91
  if (!o) return;
73
92
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -1196,7 +1215,9 @@ function PlasmicRootProvider(props) {
1196
1215
  prefetchedData = props.prefetchedData,
1197
1216
  children = props.children,
1198
1217
  skipCss = props.skipCss,
1199
- skipFonts = props.skipFonts;
1218
+ skipFonts = props.skipFonts,
1219
+ prefetchedQueryData = props.prefetchedQueryData,
1220
+ suspenseForQueryData = props.suspenseForQueryData;
1200
1221
  var loader = props.loader.__internal;
1201
1222
 
1202
1223
  if (prefetchedData) {
@@ -1209,13 +1230,16 @@ function PlasmicRootProvider(props) {
1209
1230
  loader: loader
1210
1231
  };
1211
1232
  }, [globalVariants, loader]);
1212
- return React.createElement(PlasmicRootContext.Provider, {
1233
+ return React.createElement(query.PlasmicQueryDataProvider, {
1234
+ prefetchedCache: prefetchedQueryData,
1235
+ suspense: suspenseForQueryData
1236
+ }, React.createElement(PlasmicRootContext.Provider, {
1213
1237
  value: value
1214
1238
  }, !skipCss && React.createElement(PlasmicCss, {
1215
1239
  loader: loader,
1216
1240
  prefetchedData: prefetchedData,
1217
1241
  skipFonts: skipFonts
1218
- }), children);
1242
+ }), children));
1219
1243
  }
1220
1244
  /**
1221
1245
  * Inject all css modules as <style/> tags. We can't use the usual styleInjector postcss
@@ -2107,6 +2131,216 @@ function PlasmicComponent(props) {
2107
2131
  return element;
2108
2132
  }
2109
2133
 
2134
+ /**
2135
+ * Performs a prepass over Plasmic content, kicking off the necessary
2136
+ * data fetches, and populating the fetched data into a cache. This
2137
+ * cache can be passed as prefetchedQueryData into PlasmicRootProvider.
2138
+ *
2139
+ * To limit rendering errors that can occur when you do this, we recommend
2140
+ * that you pass in _only_ the PlasmicComponents that you are planning to use
2141
+ * as the argument. For example:
2142
+ *
2143
+ * const cache = await extractPlasmicQueryData(
2144
+ * <PlasmicRootProvider loader={PLASMIC} prefetchedData={plasmicData}>
2145
+ * <PlasmicComponent component="Home" componentProps={{
2146
+ * // Specify the component prop overrides you are planning to use
2147
+ * // to render the page, as they may change what data is fetched.
2148
+ * ...
2149
+ * }} />
2150
+ * <PlasmicComponent component="NavBar" componentProps={{
2151
+ * ...
2152
+ * }} />
2153
+ * ...
2154
+ * </PlasmicRootProvider>
2155
+ * );
2156
+ *
2157
+ * If your PlasmicComponent will be wrapping components that require special
2158
+ * context set up, you should also wrap the element above with those context
2159
+ * providers.
2160
+ *
2161
+ * You should avoid passing in elements that are not related to Plasmic, as any
2162
+ * rendering errors from those elements during the prepass may result in data
2163
+ * not being populated in the cache.
2164
+ *
2165
+ * @param element a React element containing instances of PlasmicComponent.
2166
+ * Will attempt to satisfy all data needs from usePlasmicDataQuery()
2167
+ * in this element tree.
2168
+ * @returns an object mapping query key to fetched data
2169
+ */
2170
+
2171
+ function extractPlasmicQueryData(_x) {
2172
+ return _extractPlasmicQueryData.apply(this, arguments);
2173
+ }
2174
+ /**
2175
+ * Runs react-ssr-prepass on `element`, while isolating rendering errors
2176
+ * as much as possible for each PlasmicComponent instance.
2177
+ */
2178
+
2179
+ function _extractPlasmicQueryData() {
2180
+ _extractPlasmicQueryData = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(element) {
2181
+ var cache;
2182
+ return runtime_1.wrap(function _callee$(_context) {
2183
+ while (1) {
2184
+ switch (_context.prev = _context.next) {
2185
+ case 0:
2186
+ cache = {};
2187
+ _context.prev = 1;
2188
+ _context.next = 4;
2189
+ return plasmicPrepass(React__default.createElement(query.PrepassContext.Provider, {
2190
+ value: {
2191
+ cache: cache
2192
+ }
2193
+ }, element));
2194
+
2195
+ case 4:
2196
+ _context.next = 9;
2197
+ break;
2198
+
2199
+ case 6:
2200
+ _context.prev = 6;
2201
+ _context.t0 = _context["catch"](1);
2202
+ console.warn("PLASMIC: Error encountered while pre-rendering", _context.t0);
2203
+
2204
+ case 9:
2205
+ return _context.abrupt("return", Object.fromEntries(Array.from(Object.entries(cache)).map(function (_ref4) {
2206
+ var key = _ref4[0],
2207
+ data = _ref4[1].data;
2208
+ return [key, data];
2209
+ }).filter(function (_ref5) {
2210
+ var data = _ref5[1];
2211
+ return !!data;
2212
+ })));
2213
+
2214
+ case 10:
2215
+ case "end":
2216
+ return _context.stop();
2217
+ }
2218
+ }
2219
+ }, _callee, null, [[1, 6]]);
2220
+ }));
2221
+ return _extractPlasmicQueryData.apply(this, arguments);
2222
+ }
2223
+
2224
+ function plasmicPrepass(_x2) {
2225
+ return _plasmicPrepass.apply(this, arguments);
2226
+ }
2227
+ /**
2228
+ * Returns a clone of the element tree, where componentProps of PlasmicComponent
2229
+ * has been processed such that any React elements found are wrapped in
2230
+ * an error boundary. Makes it possible to isolate rendering errors while still
2231
+ * finishing as much data fetched as possible.
2232
+ */
2233
+
2234
+ function _plasmicPrepass() {
2235
+ _plasmicPrepass = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(element) {
2236
+ return runtime_1.wrap(function _callee2$(_context2) {
2237
+ while (1) {
2238
+ switch (_context2.prev = _context2.next) {
2239
+ case 0:
2240
+ _context2.next = 2;
2241
+ return prepass(buildPlasmicPrepassElement(element));
2242
+
2243
+ case 2:
2244
+ case "end":
2245
+ return _context2.stop();
2246
+ }
2247
+ }
2248
+ }, _callee2);
2249
+ }));
2250
+ return _plasmicPrepass.apply(this, arguments);
2251
+ }
2252
+
2253
+ function buildPlasmicPrepassElement(element) {
2254
+ return React__default.createElement(GenericErrorBoundary, null, processReactElement(element));
2255
+ }
2256
+
2257
+ function processReactElement(element) {
2258
+ if (element.type === PlasmicComponent) {
2259
+ return React__default.cloneElement(element, processPlasmicComponentProps(element.props));
2260
+ } else {
2261
+ return React__default.cloneElement(element, processComponentProps(element.props));
2262
+ }
2263
+ }
2264
+
2265
+ function processComponentProps(props) {
2266
+ if (!props || typeof props !== 'object') {
2267
+ return props;
2268
+ }
2269
+
2270
+ var processValue = function processValue(value) {
2271
+ if (React__default.isValidElement(value)) {
2272
+ return processReactElement(value);
2273
+ } else if (Array.isArray(value)) {
2274
+ return value.map(function (v) {
2275
+ return processValue(v);
2276
+ });
2277
+ } else if (isLiteralObject(value)) {
2278
+ return Object.fromEntries(Object.entries(value).map(function (_ref) {
2279
+ var k = _ref[0],
2280
+ v = _ref[1];
2281
+ return [k, processValue(v)];
2282
+ }));
2283
+ } else {
2284
+ return value;
2285
+ }
2286
+ };
2287
+
2288
+ return Object.fromEntries(Object.entries(props).map(function (_ref2) {
2289
+ var k = _ref2[0],
2290
+ v = _ref2[1];
2291
+ return [k, processValue(v)];
2292
+ }));
2293
+ }
2294
+
2295
+ var GenericErrorBoundary = /*#__PURE__*/function (_React$Component) {
2296
+ _inheritsLoose(GenericErrorBoundary, _React$Component);
2297
+
2298
+ function GenericErrorBoundary(props) {
2299
+ return _React$Component.call(this, props) || this;
2300
+ }
2301
+
2302
+ var _proto = GenericErrorBoundary.prototype;
2303
+
2304
+ _proto.componentDidCatch = function componentDidCatch(error) {
2305
+ console.log("Plasmic: Encountered error while prepass rendering:", error);
2306
+ };
2307
+
2308
+ _proto.render = function render() {
2309
+ return this.props.children;
2310
+ };
2311
+
2312
+ return GenericErrorBoundary;
2313
+ }(React__default.Component);
2314
+ /**
2315
+ * To process the componentProps passed to PlasmicComponent, wrap any
2316
+ * React element we find in <GenericErrorBoundary />.
2317
+ */
2318
+
2319
+
2320
+ function processPlasmicComponentProps(x) {
2321
+ if (!x) {
2322
+ return x;
2323
+ } else if (reactIs.isFragment(x)) {
2324
+ return React__default.createElement(React__default.Fragment, null, React__default.Children.map(x.props.children, processPlasmicComponentProps));
2325
+ } else if (React__default.isValidElement(x)) {
2326
+ return React__default.createElement(GenericErrorBoundary, null, x);
2327
+ } else if (Array.isArray(x)) {
2328
+ return x.map(processPlasmicComponentProps);
2329
+ } else if (isLiteralObject(x)) {
2330
+ return Object.fromEntries(Object.entries(x).map(function (_ref3) {
2331
+ var key = _ref3[0],
2332
+ val = _ref3[1];
2333
+ return [key, processPlasmicComponentProps(val)];
2334
+ }));
2335
+ } else {
2336
+ return x;
2337
+ }
2338
+ }
2339
+
2340
+ function isLiteralObject(obj) {
2341
+ return !!obj && typeof obj === 'object' && obj.constructor === Object;
2342
+ }
2343
+
2110
2344
  function renderToElement(_x, _x2, _x3, _x4) {
2111
2345
  return _renderToElement.apply(this, arguments);
2112
2346
  }
@@ -2211,12 +2445,20 @@ Object.defineProperty(exports, 'repeatedElement', {
2211
2445
  return host.repeatedElement;
2212
2446
  }
2213
2447
  });
2448
+ Object.defineProperty(exports, 'usePlasmicQueryData', {
2449
+ enumerable: true,
2450
+ get: function () {
2451
+ return query.usePlasmicQueryData;
2452
+ }
2453
+ });
2214
2454
  exports.PlasmicComponent = PlasmicComponent;
2215
2455
  exports.PlasmicComponentLoader = PlasmicComponentLoader;
2216
2456
  exports.PlasmicRootProvider = PlasmicRootProvider;
2217
2457
  exports.convertBundlesToComponentRenderData = convertBundlesToComponentRenderData;
2458
+ exports.extractPlasmicQueryData = extractPlasmicQueryData;
2218
2459
  exports.hydrateFromElement = hydrateFromElement;
2219
2460
  exports.initPlasmicLoader = initPlasmicLoader;
2461
+ exports.plasmicPrepass = plasmicPrepass;
2220
2462
  exports.renderToElement = renderToElement;
2221
2463
  exports.renderToString = renderToString;
2222
2464
  exports.usePlasmicComponent = usePlasmicComponent;