@vercel/microfrontends 2.0.0-canary.1 → 2.0.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +11 -9
  2. package/README.md +21 -4
  3. package/dist/bin/cli.cjs +142 -13
  4. package/dist/config.cjs +123 -3
  5. package/dist/config.cjs.map +1 -1
  6. package/dist/config.d.ts +33 -2
  7. package/dist/config.js +122 -2
  8. package/dist/config.js.map +1 -1
  9. package/dist/experimental/sveltekit.cjs +128 -4
  10. package/dist/experimental/sveltekit.cjs.map +1 -1
  11. package/dist/experimental/sveltekit.js +127 -3
  12. package/dist/experimental/sveltekit.js.map +1 -1
  13. package/dist/experimental/vite.cjs +128 -4
  14. package/dist/experimental/vite.cjs.map +1 -1
  15. package/dist/experimental/vite.js +127 -3
  16. package/dist/experimental/vite.js.map +1 -1
  17. package/dist/microfrontends/server.cjs +128 -4
  18. package/dist/microfrontends/server.cjs.map +1 -1
  19. package/dist/microfrontends/server.d.ts +4 -3
  20. package/dist/microfrontends/server.js +127 -3
  21. package/dist/microfrontends/server.js.map +1 -1
  22. package/dist/next/config.cjs +131 -131
  23. package/dist/next/config.cjs.map +1 -1
  24. package/dist/next/config.d.ts +5 -0
  25. package/dist/next/config.js +130 -130
  26. package/dist/next/config.js.map +1 -1
  27. package/dist/next/middleware.cjs +125 -105
  28. package/dist/next/middleware.cjs.map +1 -1
  29. package/dist/next/middleware.js +125 -105
  30. package/dist/next/middleware.js.map +1 -1
  31. package/dist/next/testing.cjs +126 -6
  32. package/dist/next/testing.cjs.map +1 -1
  33. package/dist/next/testing.d.ts +2 -2
  34. package/dist/next/testing.js +124 -4
  35. package/dist/next/testing.js.map +1 -1
  36. package/dist/overrides.d.ts +3 -3
  37. package/dist/schema.d.ts +2 -2
  38. package/dist/{types-4299bff1.d.ts → types-88602303.d.ts} +1 -1
  39. package/dist/{types-0deb756b.d.ts → types-e7523e61.d.ts} +1 -1
  40. package/dist/utils/mfe-port.cjs +128 -4
  41. package/dist/utils/mfe-port.cjs.map +1 -1
  42. package/dist/utils/mfe-port.js +127 -3
  43. package/dist/utils/mfe-port.js.map +1 -1
  44. package/dist/validation.d.ts +1 -1
  45. package/package.json +7 -2
@@ -389,8 +389,106 @@ function findConfig({ dir }) {
389
389
  // src/config/microfrontends-config/isomorphic/index.ts
390
390
  var import_jsonc_parser2 = require("jsonc-parser");
391
391
 
392
- // src/config/microfrontends-config/isomorphic/validation.ts
392
+ // src/config/microfrontends-config/client/index.ts
393
393
  var import_path_to_regexp = require("path-to-regexp");
394
+ var regexpCache = /* @__PURE__ */ new Map();
395
+ var getRegexp = (path7) => {
396
+ const existing = regexpCache.get(path7);
397
+ if (existing) {
398
+ return existing;
399
+ }
400
+ const regexp = (0, import_path_to_regexp.pathToRegexp)(path7);
401
+ regexpCache.set(path7, regexp);
402
+ return regexp;
403
+ };
404
+ var MicrofrontendConfigClient = class {
405
+ constructor(config, opts) {
406
+ this.pathCache = {};
407
+ this.hasFlaggedPaths = config.hasFlaggedPaths ?? false;
408
+ for (const app of Object.values(config.applications)) {
409
+ if (app.routing) {
410
+ if (app.routing.some((match) => match.flag)) {
411
+ this.hasFlaggedPaths = true;
412
+ }
413
+ const newRouting = [];
414
+ const pathsWithoutFlags = [];
415
+ for (const group of app.routing) {
416
+ if (group.flag) {
417
+ if (opts?.removeFlaggedPaths) {
418
+ continue;
419
+ }
420
+ if (group.group) {
421
+ delete group.group;
422
+ }
423
+ newRouting.push(group);
424
+ } else {
425
+ pathsWithoutFlags.push(...group.paths);
426
+ }
427
+ }
428
+ if (pathsWithoutFlags.length > 0) {
429
+ newRouting.push({ paths: pathsWithoutFlags });
430
+ }
431
+ app.routing = newRouting;
432
+ }
433
+ }
434
+ this.serialized = config;
435
+ if (this.hasFlaggedPaths) {
436
+ this.serialized.hasFlaggedPaths = this.hasFlaggedPaths;
437
+ }
438
+ this.applications = config.applications;
439
+ }
440
+ /**
441
+ * Create a new `MicrofrontendConfigClient` from a JSON string.
442
+ * Config must be passed in to remain framework agnostic
443
+ */
444
+ static fromEnv(config) {
445
+ if (!config) {
446
+ throw new Error(
447
+ "Could not construct MicrofrontendConfigClient: configuration is empty or undefined. Did you set up your application with `withMicrofrontends`?"
448
+ );
449
+ }
450
+ return new MicrofrontendConfigClient(JSON.parse(config));
451
+ }
452
+ isEqual(other) {
453
+ return this === other || JSON.stringify(this.applications) === JSON.stringify(other.applications);
454
+ }
455
+ getApplicationNameForPath(path7) {
456
+ if (!path7.startsWith("/")) {
457
+ throw new Error(`Path must start with a /`);
458
+ }
459
+ if (this.pathCache[path7]) {
460
+ return this.pathCache[path7];
461
+ }
462
+ const pathname = new URL(path7, "https://example.com").pathname;
463
+ for (const [name, application] of Object.entries(this.applications)) {
464
+ if (application.routing) {
465
+ for (const group of application.routing) {
466
+ for (const childPath of group.paths) {
467
+ const regexp = getRegexp(childPath);
468
+ if (regexp.test(pathname)) {
469
+ this.pathCache[path7] = name;
470
+ return name;
471
+ }
472
+ }
473
+ }
474
+ }
475
+ }
476
+ const defaultApplication = Object.entries(this.applications).find(
477
+ ([, application]) => application.default
478
+ );
479
+ if (!defaultApplication) {
480
+ return null;
481
+ }
482
+ this.pathCache[path7] = defaultApplication[0];
483
+ return defaultApplication[0];
484
+ }
485
+ serialize() {
486
+ return this.serialized;
487
+ }
488
+ };
489
+
490
+ // src/config/microfrontends-config/isomorphic/validation.ts
491
+ var import_path_to_regexp2 = require("path-to-regexp");
394
492
  var LIST_FORMATTER = new Intl.ListFormat("en", {
395
493
  style: "long",
396
494
  type: "conjunction"
@@ -418,7 +516,7 @@ var validateConfigPaths = (applicationConfigsById) => {
418
516
  } else {
419
517
  pathsByApplicationId.set(path7, {
420
518
  applications: [id],
421
- matcher: (0, import_path_to_regexp.pathToRegexp)(path7),
519
+ matcher: (0, import_path_to_regexp2.pathToRegexp)(path7),
422
520
  applicationId: id
423
521
  });
424
522
  }
@@ -465,7 +563,7 @@ var validateConfigPaths = (applicationConfigsById) => {
465
563
  var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
466
564
  function validatePathExpression(path7) {
467
565
  try {
468
- const tokens = (0, import_path_to_regexp.parse)(path7);
566
+ const tokens = (0, import_path_to_regexp2.parse)(path7);
469
567
  if (/(?<!\\)\{/.test(path7)) {
470
568
  return `Optional paths are not supported: ${path7}`;
471
569
  }
@@ -923,6 +1021,28 @@ var MicrofrontendConfigIsomorphic = class {
923
1021
  getLocalProxyPort() {
924
1022
  return this.config.options?.localProxyPort ?? DEFAULT_LOCAL_PROXY_PORT;
925
1023
  }
1024
+ toClientConfig(options) {
1025
+ const applications = Object.fromEntries(
1026
+ Object.entries(this.childApplications).map(([name, application]) => [
1027
+ hashApplicationName(name),
1028
+ {
1029
+ default: false,
1030
+ routing: application.routing
1031
+ }
1032
+ ])
1033
+ );
1034
+ applications[hashApplicationName(this.defaultApplication.name)] = {
1035
+ default: true
1036
+ };
1037
+ return new MicrofrontendConfigClient(
1038
+ {
1039
+ applications
1040
+ },
1041
+ {
1042
+ removeFlaggedPaths: options?.removeFlaggedPaths
1043
+ }
1044
+ );
1045
+ }
926
1046
  /**
927
1047
  * Serializes the class back to the Schema type.
928
1048
  *
@@ -1342,6 +1462,7 @@ var MicrofrontendsServer = class {
1342
1462
  * This can return either a Child or Main configuration.
1343
1463
  */
1344
1464
  static infer({
1465
+ appName,
1345
1466
  directory,
1346
1467
  filePath,
1347
1468
  cookies
@@ -1354,7 +1475,10 @@ var MicrofrontendsServer = class {
1354
1475
  }
1355
1476
  try {
1356
1477
  const packageRoot = findPackageRoot(directory);
1357
- const applicationContext = getApplicationContext({ packageRoot });
1478
+ const applicationContext = getApplicationContext({
1479
+ appName,
1480
+ packageRoot
1481
+ });
1358
1482
  const maybeConfig = findConfig({ dir: packageRoot });
1359
1483
  if (maybeConfig) {
1360
1484
  return MicrofrontendsServer.fromFile({