@plasmicapp/loader-react 1.0.344 → 1.0.345

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.
@@ -103,6 +103,109 @@ var import_loader_splits = require("@plasmicapp/loader-splits");
103
103
 
104
104
  // src/bundles.ts
105
105
  var import_loader_core = require("@plasmicapp/loader-core");
106
+
107
+ // src/utils.tsx
108
+ var import_pascalcase = __toESM(require("pascalcase"));
109
+ var React = __toESM(require("react"));
110
+ var isBrowser = typeof window !== "undefined";
111
+ function isNameSpec(lookup) {
112
+ return "name" in lookup;
113
+ }
114
+ function toFullLookup(lookup) {
115
+ const namePart = typeof lookup === "string" ? lookup : lookup.name;
116
+ const projectId = typeof lookup === "string" ? void 0 : lookup.projectId;
117
+ const codeComponent = typeof lookup === "string" ? void 0 : lookup.isCode;
118
+ if (codeComponent !== true && namePart.startsWith("/")) {
119
+ return { path: normalizePath(namePart), projectId };
120
+ } else {
121
+ return {
122
+ name: codeComponent ? namePart : normalizeName(namePart),
123
+ rawName: namePart.trim(),
124
+ projectId,
125
+ isCode: codeComponent
126
+ };
127
+ }
128
+ }
129
+ function normalizePath(path) {
130
+ return path.trim();
131
+ }
132
+ function normalizeName(name) {
133
+ return (0, import_pascalcase.default)(name).trim();
134
+ }
135
+ function matchesPagePath(pattern, path) {
136
+ const normalizedPattern = "/" + pattern.replace(/^\/|\/$/g, "");
137
+ const normalizedPath = "/" + path.replace(/^\/|\/$/g, "");
138
+ const regexString = normalizedPattern.replace(/\/\[\[\.\.\.([^\]^]+)]]/g, "(?:/([^]*))?").replace(/\/\[\.\.\.([^\]^]+)]/g, "/([^]*)").replace(/\[([^\]^]+)]/g, "([^/]+)").replace(/\//g, "\\/");
139
+ const regex = new RegExp(`^/?${regexString}$`);
140
+ const match = normalizedPath.match(regex);
141
+ if (!match)
142
+ return false;
143
+ const slugNames = [...pattern.matchAll(/\[\.?\.?\.?([^[\]]+)]/g)].map(
144
+ (m) => m[1]
145
+ );
146
+ const params = {};
147
+ for (let i = 0; i < slugNames.length; i++) {
148
+ const slugName = slugNames[i];
149
+ const value = match[i + 1];
150
+ if (pattern.includes(`[[...${slugName}]]`)) {
151
+ params[slugName] = value ? value.split("/").filter(Boolean) : [];
152
+ } else if (pattern.includes(`[...${slugName}]`)) {
153
+ params[slugName] = value.split("/").filter(Boolean);
154
+ } else if (value !== void 0) {
155
+ params[slugName] = value;
156
+ }
157
+ }
158
+ return { params };
159
+ }
160
+ function isDynamicPagePath(path) {
161
+ return !!path.match(/\[[^/]*\]/);
162
+ }
163
+ function matchesCompMeta(lookup, meta) {
164
+ if (lookup.projectId && meta.projectId !== lookup.projectId) {
165
+ return false;
166
+ }
167
+ return isNameSpec(lookup) ? (lookup.name === meta.name || lookup.rawName === meta.name || lookup.rawName === meta.displayName) && (lookup.isCode == null || lookup.isCode === meta.isCode) : !!(meta.path && matchesPagePath(meta.path, lookup.path));
168
+ }
169
+ function getCompMetas(metas, lookup) {
170
+ const full = toFullLookup(lookup);
171
+ return metas.filter((meta) => matchesCompMeta(full, meta)).map(
172
+ (meta) => {
173
+ if (isNameSpec(full) || !meta.path) {
174
+ return meta;
175
+ }
176
+ const match = matchesPagePath(meta.path, full.path);
177
+ if (!match) {
178
+ return meta;
179
+ }
180
+ return __spreadProps(__spreadValues({}, meta), { params: match.params });
181
+ }
182
+ ).sort(
183
+ (meta1, meta2) => (
184
+ // We sort the matched component metas by the number of path params, so
185
+ // if there are two pages `/products/foo` and `/products/[slug]`,
186
+ // the first one will have higher precedence.
187
+ Array.from(Object.keys(meta1.params || {})).length - Array.from(Object.keys(meta2.params || {})).length
188
+ )
189
+ );
190
+ }
191
+ function getLookupSpecName(lookup) {
192
+ if (typeof lookup === "string") {
193
+ return lookup;
194
+ } else if (lookup.projectId) {
195
+ return `${lookup.name} (project ${lookup.projectId})`;
196
+ } else {
197
+ return lookup.name;
198
+ }
199
+ }
200
+ function uniq(elements) {
201
+ return Array.from(new Set(elements));
202
+ }
203
+ function intersect(a, b) {
204
+ const setB = new Set(b);
205
+ return a.filter((elt) => setB.has(elt));
206
+ }
207
+
208
+ // src/bundles.ts
106
209
  function getUsedComps(allComponents, entryCompIds) {
107
210
  const q = [...entryCompIds];
108
211
  const seenIds = new Set(entryCompIds);
@@ -171,14 +274,7 @@ function prepComponentData(bundle, compMetas, opts) {
171
274
  };
172
275
  }
173
276
  function mergeBundles(target, from) {
174
- var _a2;
175
- const existingCompIds = new Set(target.components.map((c) => c.id));
176
- const newCompMetas = from.components.filter(
177
- (m) => !existingCompIds.has(m.id)
178
- );
179
- if (newCompMetas.length > 0) {
180
- target = __spreadProps(__spreadValues({}, target), { components: [...target.components, ...newCompMetas] });
181
- }
277
+ var _a2, _b, _c, _d, _e;
182
278
  const existingProjects = new Set(target.projects.map((p) => p.id));
183
279
  const newProjects = from.projects.filter((p) => !existingProjects.has(p.id));
184
280
  if (newProjects.length > 0) {
@@ -186,6 +282,35 @@ function mergeBundles(target, from) {
186
282
  projects: [...target.projects, ...newProjects]
187
283
  });
188
284
  }
285
+ const existingCompIds = new Set(target.components.map((c) => c.id));
286
+ function shouldIncludeComponentInBundle(c) {
287
+ if (existingCompIds.has(c.id)) {
288
+ return false;
289
+ }
290
+ if (!existingProjects.has(c.projectId)) {
291
+ return true;
292
+ }
293
+ if (!target.filteredIds[c.projectId]) {
294
+ return true;
295
+ }
296
+ return target.filteredIds[c.projectId].includes(c.id);
297
+ }
298
+ const newCompMetas = from.components.filter(
299
+ (m) => shouldIncludeComponentInBundle(m)
300
+ );
301
+ if (newCompMetas.length > 0) {
302
+ target = __spreadProps(__spreadValues({}, target), { components: [...target.components, ...newCompMetas] });
303
+ Object.entries(from.filteredIds).forEach(([projectId, ids]) => {
304
+ if (!target.filteredIds[projectId]) {
305
+ target.filteredIds[projectId] = [...ids];
306
+ } else {
307
+ target.filteredIds[projectId] = intersect(
308
+ target.filteredIds[projectId],
309
+ ids
310
+ );
311
+ }
312
+ });
313
+ }
189
314
  const existingModules = {
190
315
  browser: new Set(target.modules.browser.map((m) => m.fileName)),
191
316
  server: new Set(target.modules.server.map((m) => m.fileName))
@@ -216,12 +341,18 @@ function mergeBundles(target, from) {
216
341
  });
217
342
  }
218
343
  const existingSplitIds = new Set(target.activeSplits.map((s) => s.id));
219
- const newSplits = (_a2 = from.activeSplits.filter((s) => !existingSplitIds.has(s.id))) != null ? _a2 : [];
344
+ const newSplits = (_a2 = from.activeSplits.filter(
345
+ // Don't include splits belonging to projects already present
346
+ // in the target bundle
347
+ (s) => !existingSplitIds.has(s.id) && !existingProjects.has(s.projectId)
348
+ )) != null ? _a2 : [];
220
349
  if (newSplits.length > 0) {
221
350
  target = __spreadProps(__spreadValues({}, target), {
222
351
  activeSplits: [...target.activeSplits, ...newSplits]
223
352
  });
224
353
  }
354
+ target.bundleKey = (_c = (_b = target.bundleKey) != null ? _b : from.bundleKey) != null ? _c : null;
355
+ target.deferChunksByDefault = (_e = (_d = target.deferChunksByDefault) != null ? _d : from.deferChunksByDefault) != null ? _e : false;
225
356
  return target;
226
357
  }
227
358
  var convertBundlesToComponentRenderData = (bundles, compMetas) => {
@@ -232,115 +363,6 @@ var convertBundlesToComponentRenderData = (bundles, compMetas) => {
232
363
  return prepComponentData(mergedBundles, compMetas);
233
364
  };
234
365
 
235
- // src/utils.tsx
236
- var import_pascalcase = __toESM(require("pascalcase"));
237
- var React = __toESM(require("react"));
238
- var isBrowser = typeof window !== "undefined";
239
- function isNameSpec(lookup) {
240
- return "name" in lookup;
241
- }
242
- function toFullLookup(lookup) {
243
- const namePart = typeof lookup === "string" ? lookup : lookup.name;
244
- const projectId = typeof lookup === "string" ? void 0 : lookup.projectId;
245
- const codeComponent = typeof lookup === "string" ? void 0 : lookup.isCode;
246
- if (codeComponent !== true && namePart.startsWith("/")) {
247
- return { path: normalizePath(namePart), projectId };
248
- } else {
249
- return {
250
- name: codeComponent ? namePart : normalizeName(namePart),
251
- rawName: namePart.trim(),
252
- projectId,
253
- isCode: codeComponent
254
- };
255
- }
256
- }
257
- function normalizePath(path) {
258
- return path.trim();
259
- }
260
- function normalizeName(name) {
261
- return (0, import_pascalcase.default)(name).trim();
262
- }
263
- function matchesPagePath(pattern, path) {
264
- const normalizedPattern = "/" + pattern.replace(/^\/|\/$/g, "");
265
- const normalizedPath = "/" + path.replace(/^\/|\/$/g, "");
266
- const regexString = normalizedPattern.replace(/\/\[\[\.\.\.([^\]^]+)]]/g, "(?:/([^]*))?").replace(/\/\[\.\.\.([^\]^]+)]/g, "/([^]*)").replace(/\[([^\]^]+)]/g, "([^/]+)").replace(/\//g, "\\/");
267
- const regex = new RegExp(`^/?${regexString}$`);
268
- const match = normalizedPath.match(regex);
269
- if (!match)
270
- return false;
271
- const slugNames = [...pattern.matchAll(/\[\.?\.?\.?([^[\]]+)]/g)].map(
272
- (m) => m[1]
273
- );
274
- const params = {};
275
- for (let i = 0; i < slugNames.length; i++) {
276
- const slugName = slugNames[i];
277
- const value = match[i + 1];
278
- if (pattern.includes(`[[...${slugName}]]`)) {
279
- params[slugName] = value ? value.split("/").filter(Boolean) : [];
280
- } else if (pattern.includes(`[...${slugName}]`)) {
281
- params[slugName] = value.split("/").filter(Boolean);
282
- } else if (value !== void 0) {
283
- params[slugName] = value;
284
- }
285
- }
286
- return { params };
287
- }
288
- function isDynamicPagePath(path) {
289
- return !!path.match(/\[[^/]*\]/);
290
- }
291
- function matchesCompMeta(lookup, meta) {
292
- if (lookup.projectId && meta.projectId !== lookup.projectId) {
293
- return false;
294
- }
295
- return isNameSpec(lookup) ? (lookup.name === meta.name || lookup.rawName === meta.name || lookup.rawName === meta.displayName) && (lookup.isCode == null || lookup.isCode === meta.isCode) : !!(meta.path && matchesPagePath(meta.path, lookup.path));
296
- }
297
- function getCompMetas(metas, lookup) {
298
- const full = toFullLookup(lookup);
299
- return metas.filter((meta) => matchesCompMeta(full, meta)).map(
300
- (meta) => {
301
- if (isNameSpec(full) || !meta.path) {
302
- return meta;
303
- }
304
- const match = matchesPagePath(meta.path, full.path);
305
- if (!match) {
306
- return meta;
307
- }
308
- return __spreadProps(__spreadValues({}, meta), { params: match.params });
309
- }
310
- ).sort(
311
- (meta1, meta2) => (
312
- // We sort the matched component metas by the number of path params, so
313
- // if there are two pages `/products/foo` and `/products/[slug]`,
314
- // the first one will have higher precedence.
315
- Array.from(Object.keys(meta1.params || {})).length - Array.from(Object.keys(meta2.params || {})).length
316
- )
317
- );
318
- }
319
- function getLookupSpecName(lookup) {
320
- if (typeof lookup === "string") {
321
- return lookup;
322
- } else if (lookup.projectId) {
323
- return `${lookup.name} (project ${lookup.projectId})`;
324
- } else {
325
- return lookup.name;
326
- }
327
- }
328
- function uniq(elements) {
329
- return Array.from(new Set(elements));
330
- }
331
- function uniqBy(elements, iterator) {
332
- const vis = /* @__PURE__ */ new Set();
333
- const filtered = [];
334
- for (const elt of elements) {
335
- const key = iterator(elt);
336
- if (!vis.has(key)) {
337
- vis.add(key);
338
- filtered.push(elt);
339
- }
340
- }
341
- return filtered;
342
- }
343
-
344
366
  // src/component-lookup.ts
345
367
  function getFirstCompMeta(metas, lookup) {
346
368
  const filtered = getCompMetas(metas, lookup);
@@ -498,7 +520,8 @@ var BaseInternalPlasmicComponentLoader = class {
498
520
  projects: [],
499
521
  activeSplits: [],
500
522
  bundleKey: null,
501
- deferChunksByDefault: false
523
+ deferChunksByDefault: false,
524
+ filteredIds: {}
502
525
  };
503
526
  this.opts = args.opts;
504
527
  this.fetcher = args.fetcher;
@@ -609,7 +632,7 @@ var BaseInternalPlasmicComponentLoader = class {
609
632
  });
610
633
  }
611
634
  mergeBundle(newBundle) {
612
- var _a2, _b, _c, _d, _e;
635
+ var _a2, _b;
613
636
  newBundle.bundleKey = (_a2 = newBundle.bundleKey) != null ? _a2 : null;
614
637
  if (newBundle.bundleKey && this.bundle.bundleKey && newBundle.bundleKey !== this.bundle.bundleKey) {
615
638
  console.warn(
@@ -619,39 +642,8 @@ ${newBundle.bundleKey}
619
642
  ${this.bundle.bundleKey}`
620
643
  );
621
644
  }
622
- const bundles = [this.bundle, newBundle];
623
- this.bundle = {
624
- activeSplits: uniqBy(
625
- bundles.flatMap((bundle) => bundle.activeSplits),
626
- (split) => split.id
627
- ),
628
- components: uniqBy(
629
- bundles.flatMap((bundle) => bundle.components),
630
- (c) => c.id
631
- ),
632
- globalGroups: uniqBy(
633
- bundles.flatMap((bundle) => bundle.globalGroups),
634
- (g) => g.id
635
- ),
636
- modules: {
637
- browser: uniqBy(
638
- bundles.flatMap((bundle) => bundle.modules.browser),
639
- (m) => m.fileName
640
- ),
641
- server: uniqBy(
642
- bundles.flatMap((bundle) => bundle.modules.server),
643
- (m) => m.fileName
644
- )
645
- },
646
- projects: uniqBy(
647
- bundles.flatMap((bundle) => bundle.projects),
648
- (p) => p.id
649
- ),
650
- // Avoid `undefined` as it cannot be serialized as JSON
651
- bundleKey: (_c = (_b = newBundle.bundleKey) != null ? _b : this.bundle.bundleKey) != null ? _c : null,
652
- deferChunksByDefault: (_d = newBundle.deferChunksByDefault) != null ? _d : false
653
- };
654
- (_e = this.onBundleMerged) == null ? void 0 : _e.call(this);
645
+ this.bundle = mergeBundles(newBundle, this.bundle);
646
+ (_b = this.onBundleMerged) == null ? void 0 : _b.call(this);
655
647
  }
656
648
  getBundle() {
657
649
  return this.bundle;
@@ -667,7 +659,8 @@ ${this.bundle.bundleKey}`
667
659
  projects: [],
668
660
  activeSplits: [],
669
661
  bundleKey: null,
670
- deferChunksByDefault: false
662
+ deferChunksByDefault: false,
663
+ filteredIds: {}
671
664
  };
672
665
  this.registry.clear();
673
666
  }