@vercel/microfrontends 0.14.0 → 0.16.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 (121) hide show
  1. package/dist/bin/cli.cjs +392 -1620
  2. package/dist/config.cjs +478 -1001
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.d.ts +4 -25
  5. package/dist/config.js +474 -980
  6. package/dist/config.js.map +1 -1
  7. package/dist/{v2/microfrontends → microfrontends}/server.cjs +91 -150
  8. package/dist/microfrontends/server.cjs.map +1 -0
  9. package/dist/{v2/microfrontends → microfrontends}/server.d.ts +4 -4
  10. package/dist/{v2/microfrontends → microfrontends}/server.js +91 -150
  11. package/dist/microfrontends/server.js.map +1 -0
  12. package/dist/{v2/microfrontends.cjs → microfrontends.cjs} +49 -28
  13. package/dist/microfrontends.cjs.map +1 -0
  14. package/dist/{v2/microfrontends.d.ts → microfrontends.d.ts} +4 -4
  15. package/dist/{v2/microfrontends.js → microfrontends.js} +48 -27
  16. package/dist/microfrontends.js.map +1 -0
  17. package/dist/next/client.cjs +1 -1
  18. package/dist/next/client.cjs.map +1 -1
  19. package/dist/next/client.js +1 -1
  20. package/dist/next/client.js.map +1 -1
  21. package/dist/next/config.cjs +1344 -1024
  22. package/dist/next/config.cjs.map +1 -1
  23. package/dist/next/config.d.ts +1 -1
  24. package/dist/next/config.js +1343 -1023
  25. package/dist/next/config.js.map +1 -1
  26. package/dist/next/endpoints.cjs +77 -18
  27. package/dist/next/endpoints.cjs.map +1 -1
  28. package/dist/next/endpoints.d.ts +13 -2
  29. package/dist/next/endpoints.js +77 -18
  30. package/dist/next/endpoints.js.map +1 -1
  31. package/dist/next/middleware.cjs +765 -395
  32. package/dist/next/middleware.cjs.map +1 -1
  33. package/dist/next/middleware.d.ts +10 -5
  34. package/dist/next/middleware.js +765 -395
  35. package/dist/next/middleware.js.map +1 -1
  36. package/dist/next/testing.cjs +615 -1031
  37. package/dist/next/testing.cjs.map +1 -1
  38. package/dist/next/testing.d.ts +14 -12
  39. package/dist/next/testing.js +609 -1015
  40. package/dist/next/testing.js.map +1 -1
  41. package/dist/overrides.cjs +40 -108
  42. package/dist/overrides.cjs.map +1 -1
  43. package/dist/overrides.d.ts +24 -2
  44. package/dist/overrides.js +36 -106
  45. package/dist/overrides.js.map +1 -1
  46. package/dist/{v2/routing.cjs → routing.cjs} +3 -3
  47. package/dist/routing.cjs.map +1 -0
  48. package/dist/{v2/schema.cjs → schema.cjs} +1 -1
  49. package/dist/schema.cjs.map +1 -0
  50. package/dist/schema.d.ts +1 -0
  51. package/dist/utils/mfe-port.cjs +237 -1338
  52. package/dist/utils/mfe-port.cjs.map +1 -1
  53. package/dist/utils/mfe-port.js +230 -1331
  54. package/dist/utils/mfe-port.js.map +1 -1
  55. package/dist/validation.cjs +31 -361
  56. package/dist/validation.cjs.map +1 -1
  57. package/dist/validation.d.ts +3 -146
  58. package/dist/validation.js +30 -359
  59. package/dist/validation.js.map +1 -1
  60. package/package.json +29 -92
  61. package/schema/schema.json +174 -244
  62. package/dist/config/client.cjs +0 -54
  63. package/dist/config/client.cjs.map +0 -1
  64. package/dist/config/client.d.ts +0 -23
  65. package/dist/config/client.js +0 -28
  66. package/dist/config/client.js.map +0 -1
  67. package/dist/config/edge.cjs +0 -508
  68. package/dist/config/edge.cjs.map +0 -1
  69. package/dist/config/edge.d.ts +0 -20
  70. package/dist/config/edge.js +0 -481
  71. package/dist/config/edge.js.map +0 -1
  72. package/dist/microfrontend-config-983a5139.d.ts +0 -154
  73. package/dist/schema-2922d49e.d.ts +0 -182
  74. package/dist/v2/config.cjs +0 -709
  75. package/dist/v2/config.cjs.map +0 -1
  76. package/dist/v2/config.d.ts +0 -4
  77. package/dist/v2/config.js +0 -684
  78. package/dist/v2/config.js.map +0 -1
  79. package/dist/v2/microfrontends/server.cjs.map +0 -1
  80. package/dist/v2/microfrontends/server.js.map +0 -1
  81. package/dist/v2/microfrontends.cjs.map +0 -1
  82. package/dist/v2/microfrontends.js.map +0 -1
  83. package/dist/v2/next/client.cjs +0 -3
  84. package/dist/v2/next/client.cjs.map +0 -1
  85. package/dist/v2/next/client.d.ts +0 -45
  86. package/dist/v2/next/client.js +0 -3
  87. package/dist/v2/next/client.js.map +0 -1
  88. package/dist/v2/next/config.cjs +0 -2178
  89. package/dist/v2/next/config.cjs.map +0 -1
  90. package/dist/v2/next/config.d.ts +0 -22
  91. package/dist/v2/next/config.js +0 -2143
  92. package/dist/v2/next/config.js.map +0 -1
  93. package/dist/v2/next/endpoints.cjs +0 -141
  94. package/dist/v2/next/endpoints.cjs.map +0 -1
  95. package/dist/v2/next/endpoints.d.ts +0 -26
  96. package/dist/v2/next/endpoints.js +0 -116
  97. package/dist/v2/next/endpoints.js.map +0 -1
  98. package/dist/v2/next/middleware.cjs +0 -1099
  99. package/dist/v2/next/middleware.cjs.map +0 -1
  100. package/dist/v2/next/middleware.d.ts +0 -34
  101. package/dist/v2/next/middleware.js +0 -1071
  102. package/dist/v2/next/middleware.js.map +0 -1
  103. package/dist/v2/next/testing.cjs +0 -992
  104. package/dist/v2/next/testing.cjs.map +0 -1
  105. package/dist/v2/next/testing.d.ts +0 -55
  106. package/dist/v2/next/testing.js +0 -961
  107. package/dist/v2/next/testing.js.map +0 -1
  108. package/dist/v2/overrides.cjs +0 -75
  109. package/dist/v2/overrides.cjs.map +0 -1
  110. package/dist/v2/overrides.d.ts +0 -24
  111. package/dist/v2/overrides.js +0 -45
  112. package/dist/v2/overrides.js.map +0 -1
  113. package/dist/v2/routing.cjs.map +0 -1
  114. package/dist/v2/schema.cjs.map +0 -1
  115. package/dist/v2/schema.d.ts +0 -1
  116. package/schema/schema-v2.json +0 -266
  117. /package/dist/{v2/routing.d.ts → routing.d.ts} +0 -0
  118. /package/dist/{v2/routing.js → routing.js} +0 -0
  119. /package/dist/{v2/routing.js.map → routing.js.map} +0 -0
  120. /package/dist/{v2/schema.js → schema.js} +0 -0
  121. /package/dist/{v2/schema.js.map → schema.js.map} +0 -0
@@ -25,21 +25,75 @@ __export(endpoints_exports, {
25
25
  module.exports = __toCommonJS(endpoints_exports);
26
26
  var import_server = require("next/server");
27
27
 
28
- // src/config/client/client-config.ts
29
- function getClientConfigFromEnv() {
30
- const clientConfigJson = process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG;
31
- if (!clientConfigJson) {
32
- throw new Error(
33
- "Could not find client micro-frontends config in the environment"
28
+ // src/config/microfrontends-config/client/index.ts
29
+ var import_path_to_regexp = require("path-to-regexp");
30
+ var MicrofrontendConfigClient = class {
31
+ constructor(config, opts) {
32
+ this.pathCache = {};
33
+ this.serialized = config;
34
+ if (opts == null ? void 0 : opts.removeFlaggedPaths) {
35
+ for (const app of Object.values(config.applications)) {
36
+ if (app.routing) {
37
+ app.routing = app.routing.filter((match) => !match.flag);
38
+ }
39
+ }
40
+ }
41
+ this.applications = config.applications;
42
+ }
43
+ /**
44
+ * Create a new `MicrofrontendConfigClient` from a JSON string.
45
+ * Config must be passed in to remain framework agnostic
46
+ */
47
+ static fromEnv(config, opts) {
48
+ if (!config) {
49
+ throw new Error("No microfrontends configuration found");
50
+ }
51
+ return new MicrofrontendConfigClient(
52
+ JSON.parse(config),
53
+ opts
34
54
  );
35
55
  }
36
- return JSON.parse(clientConfigJson);
37
- }
56
+ isEqual(other) {
57
+ return JSON.stringify(this.applications) === JSON.stringify(other.applications);
58
+ }
59
+ getApplicationNameForPath(path) {
60
+ if (!path.startsWith("/")) {
61
+ throw new Error(`Path must start with a /`);
62
+ }
63
+ if (this.pathCache[path]) {
64
+ return this.pathCache[path];
65
+ }
66
+ const pathname = new URL(path, "https://example.com").pathname;
67
+ for (const [name, application] of Object.entries(this.applications)) {
68
+ if (application.routing) {
69
+ for (const group of application.routing) {
70
+ for (const childPath of group.paths) {
71
+ const regexp = (0, import_path_to_regexp.pathToRegexp)(childPath);
72
+ if (regexp.test(pathname)) {
73
+ this.pathCache[path] = name;
74
+ return name;
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ const defaultApplication = Object.entries(this.applications).find(
81
+ ([, application]) => application.default
82
+ );
83
+ if (!defaultApplication) {
84
+ return null;
85
+ }
86
+ this.pathCache[path] = defaultApplication[0];
87
+ return defaultApplication[0];
88
+ }
89
+ serialize() {
90
+ return this.serialized;
91
+ }
92
+ };
38
93
 
39
94
  // src/config/well-known/endpoints.ts
40
- async function getWellKnownClientData(flagValues = {}) {
41
- var _a;
42
- const config = getClientConfigFromEnv();
95
+ async function getWellKnownClientData(clientConfig, flagValues = {}) {
96
+ const config = MicrofrontendConfigClient.fromEnv(clientConfig);
43
97
  for (const [applicationName, application] of Object.entries(
44
98
  config.applications
45
99
  )) {
@@ -47,9 +101,9 @@ async function getWellKnownClientData(flagValues = {}) {
47
101
  continue;
48
102
  }
49
103
  const newRoutingMatches = [];
50
- for (const pathGroup of application.routing.matches) {
51
- if ((_a = pathGroup.options) == null ? void 0 : _a.flag) {
52
- const flagName = pathGroup.options.flag;
104
+ for (const pathGroup of application.routing) {
105
+ if (pathGroup.flag) {
106
+ const flagName = pathGroup.flag;
53
107
  const flagFn = flagValues[flagName];
54
108
  if (!flagFn) {
55
109
  throw new Error(
@@ -64,16 +118,21 @@ async function getWellKnownClientData(flagValues = {}) {
64
118
  newRoutingMatches.push(pathGroup);
65
119
  }
66
120
  }
67
- application.routing.matches = newRoutingMatches;
121
+ application.routing = newRoutingMatches;
68
122
  }
69
123
  return {
70
- config
124
+ config: config.serialize()
71
125
  };
72
126
  }
73
127
 
74
128
  // src/next/endpoints/index.ts
75
- async function wellKnownNextjsClientConfigAppRoute() {
76
- return import_server.NextResponse.json(await getWellKnownClientData());
129
+ async function wellKnownNextjsClientConfigAppRoute(flagValues) {
130
+ return import_server.NextResponse.json(
131
+ await getWellKnownClientData(
132
+ process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,
133
+ flagValues
134
+ )
135
+ );
77
136
  }
78
137
  // Annotate the CommonJS export names for ESM import in node:
79
138
  0 && (module.exports = {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/next/endpoints/index.ts","../../src/config/client/client-config.ts","../../src/config/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config/client/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/microfrontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(await getWellKnownClientData());\n}\n","import type { Config } from '../schema';\nimport type { ClientConfig, WellKnownClientData } from './types';\n\n/**\n * Given a micro-frontends configuration, this returns a version that is\n * acceptable to be used client-side without leaking any information.\n */\nexport function getConfigForClient(config: Config): ClientConfig {\n return {\n applications: Object.fromEntries(\n Object.entries(config.applications).map(([name, application]) => [\n name,\n {\n default: application.default,\n routing: application.routing,\n },\n ]),\n ),\n };\n}\n\nexport function getClientConfigFromEnv(): ClientConfig {\n // This has to use the actual name rather than the constant variable in order\n // for the env var to be replaced during the build correctly.\n const clientConfigJson = process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG;\n if (!clientConfigJson) {\n throw new Error(\n 'Could not find client micro-frontends config in the environment',\n );\n }\n return JSON.parse(clientConfigJson) as ClientConfig;\n}\n\nexport type { ClientConfig, WellKnownClientData };\n","import { getClientConfigFromEnv } from '../client/client-config';\nimport type { WellKnownClientData } from '../client/types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * micro-frontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/microfrontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = getClientConfigFromEnv();\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing.matches) {\n if (pathGroup.options?.flag) {\n const flagName = pathGroup.options.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing.matches = newRoutingMatches;\n }\n\n return {\n config,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;;;ACqBtB,SAAS,yBAAuC;AAGrD,QAAM,mBAAmB,QAAQ,IAAI;AACrC,MAAI,CAAC,kBAAkB;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,KAAK,MAAM,gBAAgB;AACpC;;;ACpBA,eAAsB,uBACpB,aAAqD,CAAC,GACxB;AAbhC;AAcE,QAAM,SAAS,uBAAuB;AACtC,aAAW,CAAC,iBAAiB,WAAW,KAAK,OAAO;AAAA,IAClD,OAAO;AAAA,EACT,GAAG;AACD,QAAI,CAAC,YAAY,SAAS;AACxB;AAAA,IACF;AACA,UAAM,oBAAoB,CAAC;AAC3B,eAAW,aAAa,YAAY,QAAQ,SAAS;AACnD,WAAI,eAAU,YAAV,mBAAmB,MAAM;AAC3B,cAAM,WAAW,UAAU,QAAQ;AACnC,cAAM,SAAS,WAAW,QAAQ;AAClC,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR,SAAS,8DAA8D,UAAU,yBAAyB;AAAA,UAC5G;AAAA,QACF;AAEA,cAAM,cAAc,MAAM,OAAO;AACjC,YAAI,aAAa;AACf,4BAAkB,KAAK,SAAS;AAAA,QAClC;AAAA,MACF,OAAO;AACL,0BAAkB,KAAK,SAAS;AAAA,MAClC;AAAA,IACF;AACA,gBAAY,QAAQ,UAAU;AAAA,EAChC;AAEA,SAAO;AAAA,IACL;AAAA,EACF;AACF;;;AFhCA,eAAsB,sCAA6D;AACjF,SAAO,2BAAa,KAA0B,MAAM,uBAAuB,CAAC;AAC9E;","names":[]}
1
+ {"version":3,"sources":["../../src/next/endpoints/index.ts","../../src/config/microfrontends-config/client/index.ts","../../src/config/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/microfrontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(\n flagValues: Record<string, () => Promise<boolean>>,\n): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(\n process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,\n flagValues,\n ),\n );\n}\n\nexport type { WellKnownClientData };\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/microfrontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA6B;;;ACA7B,4BAA6B;AAOtB,IAAM,4BAAN,MAAgC;AAAA,EAKrC,YAAY,QAAsB,MAAyC;AAH3E,qBAAoC,CAAC;AAInC,SAAK,aAAa;AAClB,QAAI,6BAAM,oBAAoB;AAC5B,iBAAW,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG;AACpD,YAAI,IAAI,SAAS;AACf,cAAI,UAAU,IAAI,QAAQ,OAAO,CAAC,UAAU,CAAC,MAAM,IAAI;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AACA,SAAK,eAAe,OAAO;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,QACL,QACA,MAC2B;AAC3B,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI,MAAM,uCAAuC;AAAA,IACzD;AACA,WAAO,IAAI;AAAA,MACT,KAAK,MAAM,MAAM;AAAA,MACjB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,QAAQ,OAA2C;AACjD,WACE,KAAK,UAAU,KAAK,YAAY,MAAM,KAAK,UAAU,MAAM,YAAY;AAAA,EAE3E;AAAA,EAEA,0BAA0B,MAA6B;AACrD,QAAI,CAAC,KAAK,WAAW,GAAG,GAAG;AACzB,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAEA,QAAI,KAAK,UAAU,IAAI,GAAG;AACxB,aAAO,KAAK,UAAU,IAAI;AAAA,IAC5B;AAEA,UAAM,WAAW,IAAI,IAAI,MAAM,qBAAqB,EAAE;AACtD,eAAW,CAAC,MAAM,WAAW,KAAK,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,SAAO;AAAA,IACL,QAAQ,OAAO,UAAU;AAAA,EAC3B;AACF;;;AFjCA,eAAsB,oCACpB,YACuB;AACvB,SAAO,2BAAa;AAAA,IAClB,MAAM;AAAA,MACJ,QAAQ,IAAI;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -1,4 +1,15 @@
1
1
  import { NextResponse } from 'next/server';
2
+ import { C as ClientConfig } from '../types-fc30696d.js';
3
+ import '../types-a29d224a.js';
4
+
5
+ /**
6
+ * Data that is returned from the `.well-known/vercel/microfrontends/client-config`
7
+ * endpoint that is used by the client to ensure that navigations and prefetches
8
+ * are routed correctly.
9
+ */
10
+ interface WellKnownClientData {
11
+ config: ClientConfig;
12
+ }
2
13
 
3
14
  /**
4
15
  * A Next.js App Router API handler to export the micro-frontends client config
@@ -10,6 +21,6 @@ import { NextResponse } from 'next/server';
10
21
  * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';
11
22
  * ```
12
23
  */
13
- declare function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse>;
24
+ declare function wellKnownNextjsClientConfigAppRoute(flagValues: Record<string, () => Promise<boolean>>): Promise<NextResponse>;
14
25
 
15
- export { wellKnownNextjsClientConfigAppRoute };
26
+ export { WellKnownClientData, wellKnownNextjsClientConfigAppRoute };
@@ -1,21 +1,75 @@
1
1
  // src/next/endpoints/index.ts
2
2
  import { NextResponse } from "next/server";
3
3
 
4
- // src/config/client/client-config.ts
5
- function getClientConfigFromEnv() {
6
- const clientConfigJson = process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG;
7
- if (!clientConfigJson) {
8
- throw new Error(
9
- "Could not find client micro-frontends config in the environment"
4
+ // src/config/microfrontends-config/client/index.ts
5
+ import { pathToRegexp } from "path-to-regexp";
6
+ var MicrofrontendConfigClient = class {
7
+ constructor(config, opts) {
8
+ this.pathCache = {};
9
+ this.serialized = config;
10
+ if (opts == null ? void 0 : opts.removeFlaggedPaths) {
11
+ for (const app of Object.values(config.applications)) {
12
+ if (app.routing) {
13
+ app.routing = app.routing.filter((match) => !match.flag);
14
+ }
15
+ }
16
+ }
17
+ this.applications = config.applications;
18
+ }
19
+ /**
20
+ * Create a new `MicrofrontendConfigClient` from a JSON string.
21
+ * Config must be passed in to remain framework agnostic
22
+ */
23
+ static fromEnv(config, opts) {
24
+ if (!config) {
25
+ throw new Error("No microfrontends configuration found");
26
+ }
27
+ return new MicrofrontendConfigClient(
28
+ JSON.parse(config),
29
+ opts
10
30
  );
11
31
  }
12
- return JSON.parse(clientConfigJson);
13
- }
32
+ isEqual(other) {
33
+ return JSON.stringify(this.applications) === JSON.stringify(other.applications);
34
+ }
35
+ getApplicationNameForPath(path) {
36
+ if (!path.startsWith("/")) {
37
+ throw new Error(`Path must start with a /`);
38
+ }
39
+ if (this.pathCache[path]) {
40
+ return this.pathCache[path];
41
+ }
42
+ const pathname = new URL(path, "https://example.com").pathname;
43
+ for (const [name, application] of Object.entries(this.applications)) {
44
+ if (application.routing) {
45
+ for (const group of application.routing) {
46
+ for (const childPath of group.paths) {
47
+ const regexp = pathToRegexp(childPath);
48
+ if (regexp.test(pathname)) {
49
+ this.pathCache[path] = name;
50
+ return name;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ const defaultApplication = Object.entries(this.applications).find(
57
+ ([, application]) => application.default
58
+ );
59
+ if (!defaultApplication) {
60
+ return null;
61
+ }
62
+ this.pathCache[path] = defaultApplication[0];
63
+ return defaultApplication[0];
64
+ }
65
+ serialize() {
66
+ return this.serialized;
67
+ }
68
+ };
14
69
 
15
70
  // src/config/well-known/endpoints.ts
16
- async function getWellKnownClientData(flagValues = {}) {
17
- var _a;
18
- const config = getClientConfigFromEnv();
71
+ async function getWellKnownClientData(clientConfig, flagValues = {}) {
72
+ const config = MicrofrontendConfigClient.fromEnv(clientConfig);
19
73
  for (const [applicationName, application] of Object.entries(
20
74
  config.applications
21
75
  )) {
@@ -23,9 +77,9 @@ async function getWellKnownClientData(flagValues = {}) {
23
77
  continue;
24
78
  }
25
79
  const newRoutingMatches = [];
26
- for (const pathGroup of application.routing.matches) {
27
- if ((_a = pathGroup.options) == null ? void 0 : _a.flag) {
28
- const flagName = pathGroup.options.flag;
80
+ for (const pathGroup of application.routing) {
81
+ if (pathGroup.flag) {
82
+ const flagName = pathGroup.flag;
29
83
  const flagFn = flagValues[flagName];
30
84
  if (!flagFn) {
31
85
  throw new Error(
@@ -40,16 +94,21 @@ async function getWellKnownClientData(flagValues = {}) {
40
94
  newRoutingMatches.push(pathGroup);
41
95
  }
42
96
  }
43
- application.routing.matches = newRoutingMatches;
97
+ application.routing = newRoutingMatches;
44
98
  }
45
99
  return {
46
- config
100
+ config: config.serialize()
47
101
  };
48
102
  }
49
103
 
50
104
  // src/next/endpoints/index.ts
51
- async function wellKnownNextjsClientConfigAppRoute() {
52
- return NextResponse.json(await getWellKnownClientData());
105
+ async function wellKnownNextjsClientConfigAppRoute(flagValues) {
106
+ return NextResponse.json(
107
+ await getWellKnownClientData(
108
+ process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,
109
+ flagValues
110
+ )
111
+ );
53
112
  }
54
113
  export {
55
114
  wellKnownNextjsClientConfigAppRoute
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/next/endpoints/index.ts","../../src/config/client/client-config.ts","../../src/config/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config/client/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/microfrontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(await getWellKnownClientData());\n}\n","import type { Config } from '../schema';\nimport type { ClientConfig, WellKnownClientData } from './types';\n\n/**\n * Given a micro-frontends configuration, this returns a version that is\n * acceptable to be used client-side without leaking any information.\n */\nexport function getConfigForClient(config: Config): ClientConfig {\n return {\n applications: Object.fromEntries(\n Object.entries(config.applications).map(([name, application]) => [\n name,\n {\n default: application.default,\n routing: application.routing,\n },\n ]),\n ),\n };\n}\n\nexport function getClientConfigFromEnv(): ClientConfig {\n // This has to use the actual name rather than the constant variable in order\n // for the env var to be replaced during the build correctly.\n const clientConfigJson = process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG;\n if (!clientConfigJson) {\n throw new Error(\n 'Could not find client micro-frontends config in the environment',\n );\n }\n return JSON.parse(clientConfigJson) as ClientConfig;\n}\n\nexport type { ClientConfig, WellKnownClientData };\n","import { getClientConfigFromEnv } from '../client/client-config';\nimport type { WellKnownClientData } from '../client/types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * micro-frontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/microfrontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = getClientConfigFromEnv();\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing.matches) {\n if (pathGroup.options?.flag) {\n const flagName = pathGroup.options.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing.matches = newRoutingMatches;\n }\n\n return {\n config,\n };\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;;;ACqBtB,SAAS,yBAAuC;AAGrD,QAAM,mBAAmB,QAAQ,IAAI;AACrC,MAAI,CAAC,kBAAkB;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO,KAAK,MAAM,gBAAgB;AACpC;;;ACpBA,eAAsB,uBACpB,aAAqD,CAAC,GACxB;AAbhC;AAcE,QAAM,SAAS,uBAAuB;AACtC,aAAW,CAAC,iBAAiB,WAAW,KAAK,OAAO;AAAA,IAClD,OAAO;AAAA,EACT,GAAG;AACD,QAAI,CAAC,YAAY,SAAS;AACxB;AAAA,IACF;AACA,UAAM,oBAAoB,CAAC;AAC3B,eAAW,aAAa,YAAY,QAAQ,SAAS;AACnD,WAAI,eAAU,YAAV,mBAAmB,MAAM;AAC3B,cAAM,WAAW,UAAU,QAAQ;AACnC,cAAM,SAAS,WAAW,QAAQ;AAClC,YAAI,CAAC,QAAQ;AACX,gBAAM,IAAI;AAAA,YACR,SAAS,8DAA8D,UAAU,yBAAyB;AAAA,UAC5G;AAAA,QACF;AAEA,cAAM,cAAc,MAAM,OAAO;AACjC,YAAI,aAAa;AACf,4BAAkB,KAAK,SAAS;AAAA,QAClC;AAAA,MACF,OAAO;AACL,0BAAkB,KAAK,SAAS;AAAA,MAClC;AAAA,IACF;AACA,gBAAY,QAAQ,UAAU;AAAA,EAChC;AAEA,SAAO;AAAA,IACL;AAAA,EACF;AACF;;;AFhCA,eAAsB,sCAA6D;AACjF,SAAO,aAAa,KAA0B,MAAM,uBAAuB,CAAC;AAC9E;","names":[]}
1
+ {"version":3,"sources":["../../src/next/endpoints/index.ts","../../src/config/microfrontends-config/client/index.ts","../../src/config/well-known/endpoints.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport { getWellKnownClientData } from '../../config/well-known/endpoints';\nimport type { WellKnownClientData } from '../../config/well-known/types';\n\n/**\n * A Next.js App Router API handler to export the micro-frontends client config\n * data.\n *\n * @example In the `app/.well-known/vercel/microfrontends/client-config/route.ts` file,\n * add this code:\n * ```\n * export { wellKnownNextjsClientConfigAppRoute as GET } from '@vercel/microfrontends/next/endpoints';\n * ```\n */\nexport async function wellKnownNextjsClientConfigAppRoute(\n flagValues: Record<string, () => Promise<boolean>>,\n): Promise<NextResponse> {\n return NextResponse.json<WellKnownClientData>(\n await getWellKnownClientData(\n process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,\n flagValues,\n ),\n );\n}\n\nexport type { WellKnownClientData };\n","import { pathToRegexp } from 'path-to-regexp';\nimport type { ClientConfig } from './types';\n\ninterface MicrofrontendConfigClientOptions {\n removeFlaggedPaths?: boolean;\n}\n\nexport class MicrofrontendConfigClient {\n applications: ClientConfig['applications'];\n pathCache: Record<string, string> = {};\n private readonly serialized: ClientConfig;\n\n constructor(config: ClientConfig, opts?: MicrofrontendConfigClientOptions) {\n this.serialized = config;\n if (opts?.removeFlaggedPaths) {\n for (const app of Object.values(config.applications)) {\n if (app.routing) {\n app.routing = app.routing.filter((match) => !match.flag);\n }\n }\n }\n this.applications = config.applications;\n }\n\n /**\n * Create a new `MicrofrontendConfigClient` from a JSON string.\n * Config must be passed in to remain framework agnostic\n */\n static fromEnv(\n config: string | undefined,\n opts?: MicrofrontendConfigClientOptions,\n ): MicrofrontendConfigClient {\n if (!config) {\n throw new Error('No microfrontends configuration found');\n }\n return new MicrofrontendConfigClient(\n JSON.parse(config) as ClientConfig,\n opts,\n );\n }\n\n isEqual(other: MicrofrontendConfigClient): boolean {\n return (\n JSON.stringify(this.applications) === JSON.stringify(other.applications)\n );\n }\n\n getApplicationNameForPath(path: string): string | null {\n if (!path.startsWith('/')) {\n throw new Error(`Path must start with a /`);\n }\n\n if (this.pathCache[path]) {\n return this.pathCache[path];\n }\n\n const pathname = new URL(path, 'https://example.com').pathname;\n for (const [name, application] of Object.entries(this.applications)) {\n if (application.routing) {\n for (const group of application.routing) {\n for (const childPath of group.paths) {\n const regexp = pathToRegexp(childPath);\n if (regexp.test(pathname)) {\n this.pathCache[path] = name;\n return name;\n }\n }\n }\n }\n }\n const defaultApplication = Object.entries(this.applications).find(\n ([, application]) => application.default,\n );\n if (!defaultApplication) {\n return null;\n }\n\n this.pathCache[path] = defaultApplication[0];\n return defaultApplication[0];\n }\n\n serialize(): ClientConfig {\n return this.serialized;\n }\n}\n","import { MicrofrontendConfigClient } from '../microfrontends-config/client';\nimport type { WellKnownClientData } from './types';\n\n/**\n * Returns data used by the client to ensure that navigations across\n * microfrontend boundaries are routed and prefetched correctly. The client\n * configuration is safe to expose to users.\n *\n * This data should be exposed in a `.well-known/vercel/microfrontends/client-config`\n * endpoint.\n */\nexport async function getWellKnownClientData(\n clientConfig: string | undefined,\n flagValues: Record<string, () => Promise<boolean>> = {},\n): Promise<WellKnownClientData> {\n const config = MicrofrontendConfigClient.fromEnv(clientConfig);\n for (const [applicationName, application] of Object.entries(\n config.applications,\n )) {\n if (!application.routing) {\n continue;\n }\n const newRoutingMatches = [];\n for (const pathGroup of application.routing) {\n if (pathGroup.flag) {\n const flagName = pathGroup.flag;\n const flagFn = flagValues[flagName];\n if (!flagFn) {\n throw new Error(\n `Flag \"${flagName}\" was specified to control routing for path group \"${pathGroup.group}\" in application ${applicationName} but not found in provided flag values.`,\n );\n }\n // eslint-disable-next-line no-await-in-loop\n const flagEnabled = await flagFn();\n if (flagEnabled) {\n newRoutingMatches.push(pathGroup);\n }\n } else {\n newRoutingMatches.push(pathGroup);\n }\n }\n application.routing = newRoutingMatches;\n }\n\n return {\n config: config.serialize(),\n };\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;;;ACA7B,SAAS,oBAAoB;AAOtB,IAAM,4BAAN,MAAgC;AAAA,EAKrC,YAAY,QAAsB,MAAyC;AAH3E,qBAAoC,CAAC;AAInC,SAAK,aAAa;AAClB,QAAI,6BAAM,oBAAoB;AAC5B,iBAAW,OAAO,OAAO,OAAO,OAAO,YAAY,GAAG;AACpD,YAAI,IAAI,SAAS;AACf,cAAI,UAAU,IAAI,QAAQ,OAAO,CAAC,UAAU,CAAC,MAA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