@plasmicapp/loader-react 1.0.341 → 1.0.343

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/dist/index.js CHANGED
@@ -92,12 +92,12 @@ __export(src_exports, {
92
92
  PlasmicComponentLoader: () => PlasmicComponentLoader,
93
93
  PlasmicRootProvider: () => PlasmicRootProvider,
94
94
  convertBundlesToComponentRenderData: () => convertBundlesToComponentRenderData,
95
- extractPlasmicQueryData: () => import_prepass2.extractPlasmicQueryData,
95
+ extractPlasmicQueryData: () => extractPlasmicQueryData,
96
96
  extractPlasmicQueryDataFromElement: () => extractPlasmicQueryDataFromElement,
97
97
  hydrateFromElement: () => hydrateFromElement,
98
98
  initPlasmicLoader: () => initPlasmicLoader,
99
99
  matchesPagePath: () => matchesPagePath,
100
- plasmicPrepass: () => import_prepass2.plasmicPrepass,
100
+ plasmicPrepass: () => plasmicPrepass,
101
101
  renderToElement: () => renderToElement,
102
102
  renderToString: () => renderToString,
103
103
  repeatedElement: () => import_host3.repeatedElement,
@@ -110,19 +110,22 @@ __export(src_exports, {
110
110
  });
111
111
  module.exports = __toCommonJS(src_exports);
112
112
 
113
- // src/loader.ts
113
+ // src/loader-client.ts
114
114
  var PlasmicDataSourcesContext = __toESM(require("@plasmicapp/data-sources-context"));
115
115
  var PlasmicHost = __toESM(require("@plasmicapp/host"));
116
116
  var import_host2 = require("@plasmicapp/host");
117
- var import_loader_core2 = require("@plasmicapp/loader-core");
118
- var import_loader_fetcher = require("@plasmicapp/loader-fetcher");
119
- var import_loader_splits = require("@plasmicapp/loader-splits");
117
+ var import_loader_core3 = require("@plasmicapp/loader-core");
120
118
  var PlasmicQuery = __toESM(require("@plasmicapp/query"));
121
119
  var import_react = __toESM(require("react"));
122
120
  var import_react_dom = __toESM(require("react-dom"));
123
121
  var jsxDevRuntime = __toESM(require("react/jsx-dev-runtime"));
124
122
  var jsxRuntime = __toESM(require("react/jsx-runtime"));
125
123
 
124
+ // src/PlasmicRootProvider.tsx
125
+ var import_host = require("@plasmicapp/host");
126
+ var import_query = require("@plasmicapp/query");
127
+ var React2 = __toESM(require("react"));
128
+
126
129
  // src/utils.tsx
127
130
  var import_pascalcase = __toESM(require("pascalcase"));
128
131
  var React = __toESM(require("react"));
@@ -266,80 +269,18 @@ function MaybeWrap(props) {
266
269
  function uniq(elements) {
267
270
  return Array.from(new Set(elements));
268
271
  }
269
-
270
- // src/component-lookup.ts
271
- function getFirstCompMeta(metas, lookup) {
272
- const filtered = getCompMetas(metas, lookup);
273
- return filtered.length === 0 ? void 0 : filtered[0];
274
- }
275
- var ComponentLookup = class {
276
- constructor(bundle, registry) {
277
- this.bundle = bundle;
278
- this.registry = registry;
279
- }
280
- getComponentMeta(spec) {
281
- const compMeta = getFirstCompMeta(this.bundle.components, spec);
282
- return compMeta;
283
- }
284
- getComponent(spec, opts = {}) {
285
- const compMeta = getFirstCompMeta(this.bundle.components, spec);
286
- if (!compMeta) {
287
- throw new Error(`Component not found: ${spec}`);
288
- }
289
- const moduleName = compMeta.entry;
290
- if (!this.registry.hasModule(moduleName, opts)) {
291
- throw new Error(`Component not yet fetched: ${compMeta.name}`);
272
+ function uniqBy(elements, iterator) {
273
+ const vis = /* @__PURE__ */ new Set();
274
+ const filtered = [];
275
+ for (const elt of elements) {
276
+ const key = iterator(elt);
277
+ if (!vis.has(key)) {
278
+ vis.add(key);
279
+ filtered.push(elt);
292
280
  }
293
- const entry = this.registry.load(moduleName, {
294
- forceOriginal: opts.forceOriginal
295
- });
296
- return !opts.forceOriginal && typeof (entry == null ? void 0 : entry.getPlasmicComponent) === "function" ? entry.getPlasmicComponent() : entry.default;
297
- }
298
- hasComponent(spec) {
299
- const compMeta = getFirstCompMeta(this.bundle.components, spec);
300
- if (compMeta) {
301
- return this.registry.hasModule(compMeta.entry);
302
- }
303
- return false;
304
- }
305
- getGlobalContexts() {
306
- const customGlobalMetas = this.bundle.globalGroups.filter(
307
- (m) => m.type === "global-user-defined"
308
- );
309
- return customGlobalMetas.map((meta) => ({
310
- meta,
311
- context: this.registry.load(meta.contextFile).default
312
- }));
313
281
  }
314
- getGlobalContextsProvider(spec) {
315
- const compMeta = getFirstCompMeta(this.bundle.components, spec);
316
- const projectMeta = compMeta ? this.bundle.projects.find((x) => x.id === compMeta.projectId) : void 0;
317
- if (!projectMeta || !projectMeta.globalContextsProviderFileName || !this.registry.hasModule(projectMeta.globalContextsProviderFileName)) {
318
- return void 0;
319
- }
320
- const entry = this.registry.load(
321
- projectMeta.globalContextsProviderFileName
322
- );
323
- return typeof (entry == null ? void 0 : entry.getPlasmicComponent) === "function" ? entry.getPlasmicComponent() : entry.default;
324
- }
325
- getRootProvider() {
326
- const entry = this.registry.load("root-provider.js");
327
- return entry.default;
328
- }
329
- getCss() {
330
- return this.bundle.modules.browser.filter(
331
- (mod) => mod.type === "asset" && mod.fileName.endsWith("css")
332
- );
333
- }
334
- getRemoteFonts() {
335
- return this.bundle.projects.flatMap((p) => p.remoteFonts);
336
- }
337
- };
338
-
339
- // src/PlasmicRootProvider.tsx
340
- var import_host = require("@plasmicapp/host");
341
- var import_query = require("@plasmicapp/query");
342
- var React2 = __toESM(require("react"));
282
+ return filtered;
283
+ }
343
284
 
344
285
  // src/variation.ts
345
286
  function getPlasmicCookieValues() {
@@ -423,7 +364,7 @@ function PlasmicRootProvider(props) {
423
364
  } = props;
424
365
  const loader = props.loader.__internal;
425
366
  if (prefetchedData) {
426
- loader.registerPrefetchedBundle(prefetchedData == null ? void 0 : prefetchedData.bundle);
367
+ loader.registerPrefetchedBundle(prefetchedData.bundle);
427
368
  }
428
369
  const [splits, setSplits] = React2.useState(loader.getActiveSplits());
429
370
  const forceUpdate = useForceUpdate();
@@ -440,6 +381,66 @@ function PlasmicRootProvider(props) {
440
381
  loader.subscribePlasmicRoot(watcher);
441
382
  return () => loader.unsubscribePlasmicRoot(watcher);
442
383
  }, [watcher, loader]);
384
+ const currentContextValue = React2.useContext(PlasmicRootContext);
385
+ const { user, userAuthToken, isUserLoading, authRedirectUri } = props;
386
+ const value = React2.useMemo(() => {
387
+ var _a, _b, _c;
388
+ const withCurrentContextValueFallback = (v, key) => {
389
+ return v !== void 0 ? v : currentContextValue == null ? void 0 : currentContextValue[key];
390
+ };
391
+ return {
392
+ globalVariants: [
393
+ ...mergeGlobalVariantsSpec(
394
+ globalVariants != null ? globalVariants : [],
395
+ getGlobalVariantsFromSplits(splits, variation != null ? variation : {})
396
+ ),
397
+ ...(_a = currentContextValue == null ? void 0 : currentContextValue.globalVariants) != null ? _a : []
398
+ ],
399
+ globalContextsProps: __spreadValues(__spreadValues({}, (_b = currentContextValue == null ? void 0 : currentContextValue.globalContextsProps) != null ? _b : {}), globalContextsProps != null ? globalContextsProps : {}),
400
+ loader: withCurrentContextValueFallback(loader, "loader"),
401
+ variation: __spreadValues(__spreadValues({}, (_c = currentContextValue == null ? void 0 : currentContextValue.variation) != null ? _c : {}), variation != null ? variation : {}),
402
+ translator: withCurrentContextValueFallback(translator, "translator"),
403
+ Head: withCurrentContextValueFallback(Head, "Head"),
404
+ Link: withCurrentContextValueFallback(Link, "Link"),
405
+ user: withCurrentContextValueFallback(user, "user"),
406
+ userAuthToken: withCurrentContextValueFallback(
407
+ userAuthToken,
408
+ "userAuthToken"
409
+ ),
410
+ isUserLoading: withCurrentContextValueFallback(
411
+ isUserLoading,
412
+ "isUserLoading"
413
+ ),
414
+ authRedirectUri: withCurrentContextValueFallback(
415
+ authRedirectUri,
416
+ "authRedirectUri"
417
+ ),
418
+ suspenseFallback: withCurrentContextValueFallback(
419
+ suspenseFallback,
420
+ "suspenseFallback"
421
+ ),
422
+ disableLoadingBoundary: withCurrentContextValueFallback(
423
+ disableLoadingBoundary,
424
+ "disableLoadingBoundary"
425
+ )
426
+ };
427
+ }, [
428
+ globalVariants,
429
+ variation,
430
+ globalContextsProps,
431
+ loader,
432
+ splits,
433
+ translator,
434
+ Head,
435
+ Link,
436
+ user,
437
+ userAuthToken,
438
+ isUserLoading,
439
+ authRedirectUri,
440
+ suspenseFallback,
441
+ disableLoadingBoundary,
442
+ currentContextValue
443
+ ]);
443
444
  React2.useEffect(() => {
444
445
  ensureVariationCookies(variation);
445
446
  loader.trackRender({
@@ -449,46 +450,9 @@ function PlasmicRootProvider(props) {
449
450
  teamIds: loader.getTeamIds(),
450
451
  projectIds: loader.getProjectIds()
451
452
  },
452
- variation
453
+ variation: value.variation
453
454
  });
454
- }, [loader, variation]);
455
- const { user, userAuthToken, isUserLoading, authRedirectUri } = props;
456
- const value = React2.useMemo(
457
- () => ({
458
- globalVariants: mergeGlobalVariantsSpec(
459
- globalVariants != null ? globalVariants : [],
460
- getGlobalVariantsFromSplits(splits, variation != null ? variation : {})
461
- ),
462
- globalContextsProps,
463
- loader,
464
- variation,
465
- translator,
466
- Head,
467
- Link,
468
- user,
469
- userAuthToken,
470
- isUserLoading,
471
- authRedirectUri,
472
- suspenseFallback,
473
- disableLoadingBoundary
474
- }),
475
- [
476
- globalVariants,
477
- variation,
478
- globalContextsProps,
479
- loader,
480
- splits,
481
- translator,
482
- Head,
483
- Link,
484
- user,
485
- userAuthToken,
486
- isUserLoading,
487
- authRedirectUri,
488
- suspenseFallback,
489
- disableLoadingBoundary
490
- ]
491
- );
455
+ }, [loader, value]);
492
456
  const reactMajorVersion = +React2.version.split(".")[0];
493
457
  return /* @__PURE__ */ React2.createElement(
494
458
  import_query.PlasmicQueryDataProvider,
@@ -583,6 +547,11 @@ function createUseGlobalVariant(name, projectId) {
583
547
  };
584
548
  }
585
549
 
550
+ // src/loader-shared.ts
551
+ var import_loader_core2 = require("@plasmicapp/loader-core");
552
+ var import_loader_fetcher = require("@plasmicapp/loader-fetcher");
553
+ var import_loader_splits = require("@plasmicapp/loader-splits");
554
+
586
555
  // src/bundles.ts
587
556
  var import_loader_core = require("@plasmicapp/loader-core");
588
557
  function getUsedComps(allComponents, entryCompIds) {
@@ -697,11 +666,6 @@ function mergeBundles(target, from) {
697
666
  globalGroups: [...target.globalGroups, ...newGlobals]
698
667
  });
699
668
  }
700
- const existingExternals = new Set(target.external);
701
- const newExternals = target.external.filter((x) => !existingExternals.has(x));
702
- if (newExternals.length > 0) {
703
- target = __spreadProps(__spreadValues({}, target), { external: [...target.external, ...newExternals] });
704
- }
705
669
  const existingSplitIds = new Set(target.activeSplits.map((s) => s.id));
706
670
  const newSplits = (_a = from.activeSplits.filter((s) => !existingSplitIds.has(s.id))) != null ? _a : [];
707
671
  if (newSplits.length > 0) {
@@ -719,9 +683,101 @@ var convertBundlesToComponentRenderData = (bundles, compMetas) => {
719
683
  return prepComponentData(mergedBundles, compMetas);
720
684
  };
721
685
 
722
- // src/loader-react-server.ts
723
- var ReactServerPlasmicComponentLoader = class {
686
+ // src/component-lookup.ts
687
+ function getFirstCompMeta(metas, lookup) {
688
+ const filtered = getCompMetas(metas, lookup);
689
+ return filtered.length === 0 ? void 0 : filtered[0];
690
+ }
691
+ var ComponentLookup = class {
692
+ constructor(bundle, registry) {
693
+ this.bundle = bundle;
694
+ this.registry = registry;
695
+ }
696
+ getComponentMeta(spec) {
697
+ const compMeta = getFirstCompMeta(this.bundle.components, spec);
698
+ return compMeta;
699
+ }
700
+ getComponent(spec, opts = {}) {
701
+ const compMeta = getFirstCompMeta(this.bundle.components, spec);
702
+ if (!compMeta) {
703
+ throw new Error(`Component not found: ${spec}`);
704
+ }
705
+ const moduleName = compMeta.entry;
706
+ if (!this.registry.hasModule(moduleName, opts)) {
707
+ throw new Error(`Component not yet fetched: ${compMeta.name}`);
708
+ }
709
+ const entry = this.registry.load(moduleName, {
710
+ forceOriginal: opts.forceOriginal
711
+ });
712
+ return !opts.forceOriginal && typeof (entry == null ? void 0 : entry.getPlasmicComponent) === "function" ? entry.getPlasmicComponent() : entry.default;
713
+ }
714
+ hasComponent(spec) {
715
+ const compMeta = getFirstCompMeta(this.bundle.components, spec);
716
+ if (compMeta) {
717
+ return this.registry.hasModule(compMeta.entry);
718
+ }
719
+ return false;
720
+ }
721
+ getGlobalContexts() {
722
+ const customGlobalMetas = this.bundle.globalGroups.filter(
723
+ (m) => m.type === "global-user-defined"
724
+ );
725
+ return customGlobalMetas.map((meta) => ({
726
+ meta,
727
+ context: this.registry.load(meta.contextFile).default
728
+ }));
729
+ }
730
+ getGlobalContextsProvider(spec) {
731
+ const compMeta = getFirstCompMeta(this.bundle.components, spec);
732
+ const projectMeta = compMeta ? this.bundle.projects.find((x) => x.id === compMeta.projectId) : void 0;
733
+ if (!projectMeta || !projectMeta.globalContextsProviderFileName || !this.registry.hasModule(projectMeta.globalContextsProviderFileName)) {
734
+ return void 0;
735
+ }
736
+ const entry = this.registry.load(
737
+ projectMeta.globalContextsProviderFileName
738
+ );
739
+ return typeof (entry == null ? void 0 : entry.getPlasmicComponent) === "function" ? entry.getPlasmicComponent() : entry.default;
740
+ }
741
+ getRootProvider() {
742
+ const entry = this.registry.load("root-provider.js");
743
+ return entry.default;
744
+ }
745
+ getCss() {
746
+ return this.bundle.modules.browser.filter(
747
+ (mod) => mod.type === "asset" && mod.fileName.endsWith("css")
748
+ );
749
+ }
750
+ getRemoteFonts() {
751
+ return this.bundle.projects.flatMap((p) => p.remoteFonts);
752
+ }
753
+ };
754
+
755
+ // src/loader-shared.ts
756
+ var SUBSTITUTED_COMPONENTS = {};
757
+ var REGISTERED_CODE_COMPONENT_HELPERS = {};
758
+ var SUBSTITUTED_GLOBAL_VARIANT_HOOKS = {};
759
+ var REGISTERED_CUSTOM_FUNCTIONS = {};
760
+ function customFunctionImportAlias(meta) {
761
+ const customFunctionPrefix = `__fn_`;
762
+ return meta.namespace ? `${customFunctionPrefix}${meta.namespace}__${meta.name}` : `${customFunctionPrefix}${meta.name}`;
763
+ }
764
+ function parseFetchComponentDataArgs(...args) {
765
+ let specs;
766
+ let opts;
767
+ if (Array.isArray(args[0])) {
768
+ specs = args[0];
769
+ opts = args[1];
770
+ } else {
771
+ specs = args;
772
+ opts = void 0;
773
+ }
774
+ return { specs, opts };
775
+ }
776
+ var BaseInternalPlasmicComponentLoader = class {
724
777
  constructor(args) {
778
+ this.registry = new import_loader_core2.Registry();
779
+ this.globalVariants = [];
780
+ this.subs = [];
725
781
  this.bundle = {
726
782
  modules: {
727
783
  browser: [],
@@ -729,7 +785,6 @@ var ReactServerPlasmicComponentLoader = class {
729
785
  },
730
786
  components: [],
731
787
  globalGroups: [],
732
- external: [],
733
788
  projects: [],
734
789
  activeSplits: [],
735
790
  bundleKey: null,
@@ -740,6 +795,7 @@ var ReactServerPlasmicComponentLoader = class {
740
795
  this.tracker = args.tracker;
741
796
  this.onBundleMerged = args.onBundleMerged;
742
797
  this.onBundleFetched = args.onBundleFetched;
798
+ this.registerModules(args.builtinModules);
743
799
  }
744
800
  maybeGetCompMetas(...specs) {
745
801
  const found = /* @__PURE__ */ new Set();
@@ -842,11 +898,50 @@ var ReactServerPlasmicComponentLoader = class {
842
898
  return bundle;
843
899
  });
844
900
  }
845
- mergeBundle(bundle) {
846
- var _a, _b;
847
- this.bundle = bundle;
848
- this.bundle.bundleKey = (_a = this.bundle.bundleKey) != null ? _a : null;
849
- (_b = this.onBundleMerged) == null ? void 0 : _b.call(this);
901
+ mergeBundle(newBundle) {
902
+ var _a, _b, _c, _d, _e;
903
+ newBundle.bundleKey = (_a = newBundle.bundleKey) != null ? _a : null;
904
+ if (newBundle.bundleKey && this.bundle.bundleKey && newBundle.bundleKey !== this.bundle.bundleKey) {
905
+ console.warn(
906
+ `Plasmic Error: Different code export hashes. This can happen if your app is using different loaders with different project IDs or project versions.
907
+ Conflicting values:
908
+ ${newBundle.bundleKey}
909
+ ${this.bundle.bundleKey}`
910
+ );
911
+ }
912
+ const bundles = [this.bundle, newBundle];
913
+ this.bundle = {
914
+ activeSplits: uniqBy(
915
+ bundles.flatMap((bundle) => bundle.activeSplits),
916
+ (split) => split.id
917
+ ),
918
+ components: uniqBy(
919
+ bundles.flatMap((bundle) => bundle.components),
920
+ (c) => c.id
921
+ ),
922
+ globalGroups: uniqBy(
923
+ bundles.flatMap((bundle) => bundle.globalGroups),
924
+ (g) => g.id
925
+ ),
926
+ modules: {
927
+ browser: uniqBy(
928
+ bundles.flatMap((bundle) => bundle.modules.browser),
929
+ (m) => m.fileName
930
+ ),
931
+ server: uniqBy(
932
+ bundles.flatMap((bundle) => bundle.modules.server),
933
+ (m) => m.fileName
934
+ )
935
+ },
936
+ projects: uniqBy(
937
+ bundles.flatMap((bundle) => bundle.projects),
938
+ (p) => p.id
939
+ ),
940
+ // Avoid `undefined` as it cannot be serialized as JSON
941
+ bundleKey: (_c = (_b = newBundle.bundleKey) != null ? _b : this.bundle.bundleKey) != null ? _c : null,
942
+ deferChunksByDefault: (_d = newBundle.deferChunksByDefault) != null ? _d : false
943
+ };
944
+ (_e = this.onBundleMerged) == null ? void 0 : _e.call(this);
850
945
  }
851
946
  getBundle() {
852
947
  return this.bundle;
@@ -859,89 +954,12 @@ var ReactServerPlasmicComponentLoader = class {
859
954
  },
860
955
  components: [],
861
956
  globalGroups: [],
862
- external: [],
863
957
  projects: [],
864
958
  activeSplits: [],
865
959
  bundleKey: null,
866
960
  deferChunksByDefault: false
867
961
  };
868
- }
869
- };
870
- function parseFetchComponentDataArgs(...args) {
871
- let specs;
872
- let opts;
873
- if (Array.isArray(args[0])) {
874
- specs = args[0];
875
- opts = args[1];
876
- } else {
877
- specs = args;
878
- opts = void 0;
879
- }
880
- return { specs, opts };
881
- }
882
-
883
- // src/loader.ts
884
- var SUBSTITUTED_COMPONENTS = {};
885
- var REGISTERED_CODE_COMPONENT_HELPERS = {};
886
- var SUBSTITUTED_GLOBAL_VARIANT_HOOKS = {};
887
- var REGISTERED_CUSTOM_FUNCTIONS = {};
888
- function customFunctionImportAlias(meta) {
889
- const customFunctionPrefix = `__fn_`;
890
- return meta.namespace ? `${customFunctionPrefix}${meta.namespace}__${meta.name}` : `${customFunctionPrefix}${meta.name}`;
891
- }
892
- var InternalPlasmicComponentLoader = class {
893
- constructor(opts) {
894
- this.opts = opts;
895
- this.registry = new import_loader_core2.Registry();
896
- this.subs = [];
897
- this.roots = [];
898
- this.globalVariants = [];
899
- this.tracker = new import_loader_core2.PlasmicTracker({
900
- projectIds: opts.projects.map((p) => p.id),
901
- platform: opts.platform,
902
- preview: opts.preview,
903
- nativeFetch: opts.nativeFetch
904
- });
905
- this.reactServerLoader = new ReactServerPlasmicComponentLoader({
906
- opts,
907
- fetcher: new import_loader_core2.PlasmicModulesFetcher(opts),
908
- tracker: this.tracker,
909
- onBundleMerged: () => {
910
- this.refreshRegistry();
911
- },
912
- onBundleFetched: () => {
913
- this.roots.forEach((watcher) => {
914
- var _a;
915
- return (_a = watcher.onDataFetched) == null ? void 0 : _a.call(watcher);
916
- });
917
- }
918
- });
919
- this.registerModules({
920
- react: import_react.default,
921
- "react-dom": import_react_dom.default,
922
- "react/jsx-runtime": jsxRuntime,
923
- "react/jsx-dev-runtime": jsxDevRuntime,
924
- // Also inject @plasmicapp/query and @plasmicapp/host to use the
925
- // same contexts here and in loader-downloaded code.
926
- "@plasmicapp/query": PlasmicQuery,
927
- "@plasmicapp/data-sources-context": PlasmicDataSourcesContext,
928
- "@plasmicapp/host": PlasmicHost,
929
- "@plasmicapp/loader-runtime-registry": {
930
- components: SUBSTITUTED_COMPONENTS,
931
- globalVariantHooks: SUBSTITUTED_GLOBAL_VARIANT_HOOKS,
932
- codeComponentHelpers: REGISTERED_CODE_COMPONENT_HELPERS,
933
- functions: REGISTERED_CUSTOM_FUNCTIONS
934
- }
935
- });
936
- }
937
- getBundle() {
938
- return this.reactServerLoader.getBundle();
939
- }
940
- setGlobalVariants(globalVariants) {
941
- this.globalVariants = globalVariants;
942
- }
943
- getGlobalVariants() {
944
- return this.globalVariants;
962
+ this.registry.clear();
945
963
  }
946
964
  registerModules(modules) {
947
965
  if (Object.keys(modules).some(
@@ -962,112 +980,56 @@ var InternalPlasmicComponentLoader = class {
962
980
  this.internalSubstituteComponent(component, name, void 0);
963
981
  }
964
982
  internalSubstituteComponent(component, name, codeComponentHelpers) {
965
- if (!this.registry.isEmpty()) {
983
+ if (!this.isRegistryEmpty()) {
966
984
  console.warn(
967
985
  "Calling PlasmicComponentLoader.registerSubstitution() after Plasmic component has rendered; starting over."
968
986
  );
969
- this.registry.clear();
987
+ this.clearRegistry();
970
988
  }
971
989
  this.subs.push({ lookup: name, component, codeComponentHelpers });
972
990
  }
973
- registerComponent(component, meta) {
974
- var _a, _b;
975
- const stateHelpers = Object.fromEntries(
976
- Object.entries((_a = meta.states) != null ? _a : {}).filter(
977
- ([_, stateSpec]) => Object.keys(stateSpec).some((key) => import_host2.stateHelpersKeys.includes(key))
978
- ).map(([stateName, stateSpec]) => [
979
- stateName,
980
- Object.fromEntries(
981
- import_host2.stateHelpersKeys.filter((key) => key in stateSpec).map((key) => [key, stateSpec[key]])
982
- )
983
- ])
984
- );
985
- const helpers = { states: stateHelpers };
986
- this.internalSubstituteComponent(
987
- component,
988
- { name: meta.name, isCode: true },
989
- Object.keys(stateHelpers).length > 0 ? helpers : void 0
990
- );
991
- (0, import_host2.registerComponent)(component, __spreadValues(__spreadProps(__spreadValues({}, meta), {
992
- // Import path is not used as we will use component substitution
993
- importPath: (_b = meta.importPath) != null ? _b : ""
994
- }), Object.keys(stateHelpers).length > 0 ? {
995
- componentHelpers: {
996
- helpers,
997
- importPath: "",
998
- importName: ""
999
- }
1000
- } : {}));
991
+ refreshRegistry() {
992
+ for (const sub of this.subs) {
993
+ const metas = getCompMetas(this.getBundle().components, sub.lookup);
994
+ metas.forEach((meta) => {
995
+ SUBSTITUTED_COMPONENTS[meta.id] = sub.component;
996
+ if (sub.codeComponentHelpers) {
997
+ REGISTERED_CODE_COMPONENT_HELPERS[meta.id] = sub.codeComponentHelpers;
998
+ }
999
+ });
1000
+ }
1001
+ this.registry.updateModules(this.getBundle());
1001
1002
  }
1002
- registerFunction(fn, meta) {
1003
- var _a;
1004
- (0, import_host2.registerFunction)(fn, __spreadProps(__spreadValues({}, meta), {
1005
- importPath: (_a = meta.importPath) != null ? _a : ""
1006
- }));
1007
- REGISTERED_CUSTOM_FUNCTIONS[customFunctionImportAlias(meta)] = fn;
1003
+ isRegistryEmpty() {
1004
+ return this.registry.isEmpty();
1008
1005
  }
1009
- registerGlobalContext(context, meta) {
1010
- var _a;
1011
- this.substituteComponent(context, { name: meta.name, isCode: true });
1012
- (0, import_host2.registerGlobalContext)(context, __spreadProps(__spreadValues({}, meta), {
1013
- importPath: (_a = meta.importPath) != null ? _a : ""
1014
- }));
1006
+ clearRegistry() {
1007
+ this.registry.clear();
1015
1008
  }
1016
- registerTrait(trait, meta) {
1017
- (0, import_host2.registerTrait)(trait, meta);
1009
+ setGlobalVariants(globalVariants) {
1010
+ this.globalVariants = globalVariants;
1018
1011
  }
1019
- registerToken(token) {
1020
- (0, import_host2.registerToken)(token);
1012
+ getGlobalVariants() {
1013
+ return this.globalVariants;
1021
1014
  }
1022
1015
  registerPrefetchedBundle(bundle) {
1023
1016
  if (!isBrowser) {
1024
1017
  const cachedBundle = (0, import_loader_fetcher.internal_getCachedBundleInNodeServer)(this.opts);
1025
1018
  if (cachedBundle) {
1026
- this.reactServerLoader.mergeBundle(cachedBundle);
1019
+ this.mergeBundle(cachedBundle);
1027
1020
  }
1028
1021
  }
1029
- this.reactServerLoader.mergeBundle(bundle);
1030
- }
1031
- subscribePlasmicRoot(watcher) {
1032
- this.roots.push(watcher);
1033
- }
1034
- unsubscribePlasmicRoot(watcher) {
1035
- const index = this.roots.indexOf(watcher);
1036
- if (index >= 0) {
1037
- this.roots.splice(index, 1);
1038
- }
1039
- }
1040
- clearCache() {
1041
- this.reactServerLoader.clearCache();
1042
- this.registry.clear();
1022
+ this.mergeBundle(bundle);
1043
1023
  }
1044
1024
  getLookup() {
1045
1025
  return new ComponentLookup(this.getBundle(), this.registry);
1046
1026
  }
1047
- maybeFetchComponentData(...args) {
1048
- return this.reactServerLoader.maybeFetchComponentData(...args);
1049
- }
1050
- fetchComponentData(...args) {
1051
- return this.reactServerLoader.fetchComponentData(...args);
1052
- }
1053
- fetchPages(opts) {
1054
- return this.reactServerLoader.fetchPages(opts);
1055
- }
1056
- fetchComponents() {
1057
- return this.reactServerLoader.fetchComponents();
1058
- }
1059
- getActiveSplits() {
1060
- return this.reactServerLoader.getActiveSplits();
1061
- }
1062
- getChunksUrl(bundle, modules) {
1063
- return this.reactServerLoader.getChunksUrl(bundle, modules);
1064
- }
1065
1027
  trackConversion(value = 0) {
1066
1028
  this.tracker.trackConversion(value);
1067
1029
  }
1068
1030
  getActiveVariation(opts) {
1069
1031
  return __async(this, null, function* () {
1070
- yield this.reactServerLoader.fetchComponents();
1032
+ yield this.fetchComponents();
1071
1033
  return (0, import_loader_splits.getActiveVariation)(__spreadProps(__spreadValues({}, opts), {
1072
1034
  splits: this.getBundle().activeSplits
1073
1035
  }));
@@ -1090,23 +1052,6 @@ var InternalPlasmicComponentLoader = class {
1090
1052
  trackRender(opts) {
1091
1053
  this.tracker.trackRender(opts);
1092
1054
  }
1093
- refreshRegistry() {
1094
- for (const sub of this.subs) {
1095
- const metas = getCompMetas(this.getBundle().components, sub.lookup);
1096
- metas.forEach((meta) => {
1097
- SUBSTITUTED_COMPONENTS[meta.id] = sub.component;
1098
- if (sub.codeComponentHelpers) {
1099
- REGISTERED_CODE_COMPONENT_HELPERS[meta.id] = sub.codeComponentHelpers;
1100
- }
1101
- });
1102
- }
1103
- for (const globalGroup of this.getBundle().globalGroups) {
1104
- if (globalGroup.type !== "global-screen") {
1105
- SUBSTITUTED_GLOBAL_VARIANT_HOOKS[globalGroup.id] = createUseGlobalVariant(globalGroup.name, globalGroup.projectId);
1106
- }
1107
- }
1108
- this.registry.updateModules(this.getBundle());
1109
- }
1110
1055
  };
1111
1056
  var PlasmicComponentLoader = class {
1112
1057
  constructor(internal) {
@@ -1226,9 +1171,118 @@ var PlasmicComponentLoader = class {
1226
1171
  }
1227
1172
  };
1228
1173
 
1174
+ // src/loader-client.ts
1175
+ var InternalPlasmicComponentLoader = class extends BaseInternalPlasmicComponentLoader {
1176
+ constructor(opts) {
1177
+ const tracker = new import_loader_core3.PlasmicTracker({
1178
+ projectIds: opts.projects.map((p) => p.id),
1179
+ platform: opts.platform,
1180
+ preview: opts.preview,
1181
+ nativeFetch: opts.nativeFetch
1182
+ });
1183
+ super({
1184
+ opts,
1185
+ tracker,
1186
+ fetcher: new import_loader_core3.PlasmicModulesFetcher(opts),
1187
+ onBundleMerged: () => {
1188
+ this.refreshRegistry();
1189
+ },
1190
+ onBundleFetched: () => {
1191
+ this.roots.forEach((watcher) => {
1192
+ var _a;
1193
+ return (_a = watcher.onDataFetched) == null ? void 0 : _a.call(watcher);
1194
+ });
1195
+ },
1196
+ builtinModules: {
1197
+ react: import_react.default,
1198
+ "react-dom": import_react_dom.default,
1199
+ "react/jsx-runtime": jsxRuntime,
1200
+ "react/jsx-dev-runtime": jsxDevRuntime,
1201
+ // Also inject @plasmicapp/query and @plasmicapp/host to use the
1202
+ // same contexts here and in loader-downloaded code.
1203
+ "@plasmicapp/query": PlasmicQuery,
1204
+ "@plasmicapp/data-sources-context": PlasmicDataSourcesContext,
1205
+ "@plasmicapp/host": PlasmicHost,
1206
+ "@plasmicapp/loader-runtime-registry": {
1207
+ components: SUBSTITUTED_COMPONENTS,
1208
+ globalVariantHooks: SUBSTITUTED_GLOBAL_VARIANT_HOOKS,
1209
+ codeComponentHelpers: REGISTERED_CODE_COMPONENT_HELPERS,
1210
+ functions: REGISTERED_CUSTOM_FUNCTIONS
1211
+ }
1212
+ }
1213
+ });
1214
+ this.roots = [];
1215
+ }
1216
+ registerComponent(component, meta) {
1217
+ var _a, _b;
1218
+ const stateHelpers = Object.fromEntries(
1219
+ Object.entries((_a = meta.states) != null ? _a : {}).filter(
1220
+ ([_, stateSpec]) => Object.keys(stateSpec).some((key) => import_host2.stateHelpersKeys.includes(key))
1221
+ ).map(([stateName, stateSpec]) => [
1222
+ stateName,
1223
+ Object.fromEntries(
1224
+ import_host2.stateHelpersKeys.filter((key) => key in stateSpec).map((key) => [key, stateSpec[key]])
1225
+ )
1226
+ ])
1227
+ );
1228
+ const helpers = { states: stateHelpers };
1229
+ this.internalSubstituteComponent(
1230
+ component,
1231
+ { name: meta.name, isCode: true },
1232
+ Object.keys(stateHelpers).length > 0 ? helpers : void 0
1233
+ );
1234
+ (0, import_host2.registerComponent)(component, __spreadValues(__spreadProps(__spreadValues({}, meta), {
1235
+ // Import path is not used as we will use component substitution
1236
+ importPath: (_b = meta.importPath) != null ? _b : ""
1237
+ }), Object.keys(stateHelpers).length > 0 ? {
1238
+ componentHelpers: {
1239
+ helpers,
1240
+ importPath: "",
1241
+ importName: ""
1242
+ }
1243
+ } : {}));
1244
+ }
1245
+ registerFunction(fn, meta) {
1246
+ var _a;
1247
+ (0, import_host2.registerFunction)(fn, __spreadProps(__spreadValues({}, meta), {
1248
+ importPath: (_a = meta.importPath) != null ? _a : ""
1249
+ }));
1250
+ REGISTERED_CUSTOM_FUNCTIONS[customFunctionImportAlias(meta)] = fn;
1251
+ }
1252
+ registerGlobalContext(context, meta) {
1253
+ var _a;
1254
+ this.substituteComponent(context, { name: meta.name, isCode: true });
1255
+ (0, import_host2.registerGlobalContext)(context, __spreadProps(__spreadValues({}, meta), {
1256
+ importPath: (_a = meta.importPath) != null ? _a : ""
1257
+ }));
1258
+ }
1259
+ registerTrait(trait, meta) {
1260
+ (0, import_host2.registerTrait)(trait, meta);
1261
+ }
1262
+ registerToken(token) {
1263
+ (0, import_host2.registerToken)(token);
1264
+ }
1265
+ subscribePlasmicRoot(watcher) {
1266
+ this.roots.push(watcher);
1267
+ }
1268
+ unsubscribePlasmicRoot(watcher) {
1269
+ const index = this.roots.indexOf(watcher);
1270
+ if (index >= 0) {
1271
+ this.roots.splice(index, 1);
1272
+ }
1273
+ }
1274
+ refreshRegistry() {
1275
+ for (const globalGroup of this.getBundle().globalGroups) {
1276
+ if (globalGroup.type !== "global-screen") {
1277
+ SUBSTITUTED_GLOBAL_VARIANT_HOOKS[globalGroup.id] = createUseGlobalVariant(globalGroup.name, globalGroup.projectId);
1278
+ }
1279
+ }
1280
+ super.refreshRegistry();
1281
+ }
1282
+ };
1283
+
1229
1284
  // src/index.ts
1230
1285
  var import_host3 = require("@plasmicapp/host");
1231
- var import_prepass2 = require("@plasmicapp/prepass");
1232
1286
  var import_query2 = require("@plasmicapp/query");
1233
1287
 
1234
1288
  // src/PlasmicComponent.tsx
@@ -1316,7 +1370,7 @@ function PlasmicComponent(props) {
1316
1370
  if (!Component) {
1317
1371
  return null;
1318
1372
  }
1319
- let element2 = /* @__PURE__ */ React5.createElement(Component, __spreadValues({}, componentProps));
1373
+ let elt = /* @__PURE__ */ React5.createElement(Component, __spreadValues({}, componentProps));
1320
1374
  if (isRootLoader) {
1321
1375
  const lookup = loader.getLookup();
1322
1376
  const ReactWebRootProvider = lookup.getRootProvider();
@@ -1324,7 +1378,7 @@ function PlasmicComponent(props) {
1324
1378
  name: component,
1325
1379
  projectId
1326
1380
  });
1327
- element2 = /* @__PURE__ */ React5.createElement(
1381
+ elt = /* @__PURE__ */ React5.createElement(
1328
1382
  ReactWebRootProvider,
1329
1383
  __spreadProps(__spreadValues({}, rest), {
1330
1384
  userAuthToken,
@@ -1341,11 +1395,11 @@ function PlasmicComponent(props) {
1341
1395
  cond: !!GlobalContextsProvider,
1342
1396
  wrapper: (children) => /* @__PURE__ */ React5.createElement(GlobalContextsProvider, __spreadValues({}, globalContextsProps), children)
1343
1397
  },
1344
- /* @__PURE__ */ React5.createElement(PlasmicComponentContext.Provider, { value: true }, element2)
1398
+ /* @__PURE__ */ React5.createElement(PlasmicComponentContext.Provider, { value: true }, elt)
1345
1399
  )
1346
1400
  );
1347
1401
  }
1348
- return element2;
1402
+ return elt;
1349
1403
  }, [
1350
1404
  Component,
1351
1405
  componentProps,
@@ -1362,11 +1416,20 @@ function PlasmicComponent(props) {
1362
1416
  return element;
1363
1417
  }
1364
1418
 
1419
+ // src/prepass-client.ts
1420
+ var import_prepass = require("@plasmicapp/prepass");
1421
+ function extractPlasmicQueryData(element) {
1422
+ return (0, import_prepass.extractPlasmicQueryData)(element);
1423
+ }
1424
+ function plasmicPrepass(element) {
1425
+ return (0, import_prepass.plasmicPrepass)(element);
1426
+ }
1427
+
1365
1428
  // src/render.tsx
1429
+ var import_prepass2 = require("@plasmicapp/prepass");
1366
1430
  var import_react2 = __toESM(require("react"));
1367
1431
  var import_react_dom2 = __toESM(require("react-dom"));
1368
1432
  var import_server = require("react-dom/server");
1369
- var import_prepass = require("@plasmicapp/prepass");
1370
1433
  function renderToElement(_0, _1, _2) {
1371
1434
  return __async(this, arguments, function* (loader, target, lookup, opts = {}) {
1372
1435
  return new Promise((resolve) => {
@@ -1382,7 +1445,7 @@ function renderToString(loader, lookup, opts = {}) {
1382
1445
  function extractPlasmicQueryDataFromElement(_0, _1) {
1383
1446
  return __async(this, arguments, function* (loader, lookup, opts = {}) {
1384
1447
  const element = makeElement(loader, lookup, opts);
1385
- return (0, import_prepass.extractPlasmicQueryData)(element);
1448
+ return (0, import_prepass2.extractPlasmicQueryData)(element);
1386
1449
  });
1387
1450
  }
1388
1451
  function hydrateFromElement(_0, _1, _2) {