@vercel/microfrontends 0.10.0 → 0.10.1

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 (62) hide show
  1. package/README.md +5 -5
  2. package/dist/bin/cli.cjs +172 -86
  3. package/dist/config/client.d.ts +1 -1
  4. package/dist/config/edge.d.ts +2 -2
  5. package/dist/config.cjs +1 -6
  6. package/dist/config.cjs.map +1 -1
  7. package/dist/config.d.ts +3 -3
  8. package/dist/config.js +1 -6
  9. package/dist/config.js.map +1 -1
  10. package/dist/{index-05742bef.d.ts → index-acb44057.d.ts} +2 -2
  11. package/dist/{microfrontend-config-2425db74.d.ts → microfrontend-config-983a5139.d.ts} +1 -1
  12. package/dist/next/client.cjs +1 -1
  13. package/dist/next/client.cjs.map +1 -1
  14. package/dist/next/client.js +1 -1
  15. package/dist/next/client.js.map +1 -1
  16. package/dist/next/config.cjs +3 -10
  17. package/dist/next/config.cjs.map +1 -1
  18. package/dist/next/config.js +3 -10
  19. package/dist/next/config.js.map +1 -1
  20. package/dist/next/middleware.cjs +5 -7
  21. package/dist/next/middleware.cjs.map +1 -1
  22. package/dist/next/middleware.js +5 -7
  23. package/dist/next/middleware.js.map +1 -1
  24. package/dist/next/testing.cjs +1 -6
  25. package/dist/next/testing.cjs.map +1 -1
  26. package/dist/next/testing.d.ts +2 -2
  27. package/dist/next/testing.js +1 -6
  28. package/dist/next/testing.js.map +1 -1
  29. package/dist/overrides.d.ts +2 -2
  30. package/dist/{schema-83a75e61.d.ts → schema-2922d49e.d.ts} +1 -7
  31. package/dist/{types-4fd1c7c6.d.ts → types-7b1cd9f7.d.ts} +1 -7
  32. package/dist/{types-13f3e535.d.ts → types-c3d15d04.d.ts} +1 -1
  33. package/dist/v2/config.d.ts +3 -3
  34. package/dist/v2/microfrontends/server.cjs +122 -32
  35. package/dist/v2/microfrontends/server.cjs.map +1 -1
  36. package/dist/v2/microfrontends/server.d.ts +7 -4
  37. package/dist/v2/microfrontends/server.js +122 -32
  38. package/dist/v2/microfrontends/server.js.map +1 -1
  39. package/dist/v2/microfrontends.d.ts +3 -3
  40. package/dist/v2/next/client.cjs +1 -1
  41. package/dist/v2/next/client.cjs.map +1 -1
  42. package/dist/v2/next/client.js +1 -1
  43. package/dist/v2/next/client.js.map +1 -1
  44. package/dist/v2/next/config.cjs +128 -40
  45. package/dist/v2/next/config.cjs.map +1 -1
  46. package/dist/v2/next/config.js +128 -40
  47. package/dist/v2/next/config.js.map +1 -1
  48. package/dist/v2/next/middleware.cjs +1 -1
  49. package/dist/v2/next/middleware.cjs.map +1 -1
  50. package/dist/v2/next/middleware.js +1 -1
  51. package/dist/v2/next/middleware.js.map +1 -1
  52. package/dist/v2/overrides.d.ts +3 -3
  53. package/dist/v2/schema.cjs.map +1 -1
  54. package/dist/v2/schema.d.ts +1 -1
  55. package/dist/validation.cjs +0 -8
  56. package/dist/validation.cjs.map +1 -1
  57. package/dist/validation.d.ts +2 -8
  58. package/dist/validation.js +0 -8
  59. package/dist/validation.js.map +1 -1
  60. package/package.json +8 -7
  61. package/schema/schema-v2.json +0 -4
  62. package/schema/schema.json +0 -4
@@ -1,6 +1,6 @@
1
1
  // src/config-v2/microfrontends/server/index.ts
2
- import fs from "node:fs";
3
- import { dirname } from "node:path";
2
+ import fs2 from "node:fs";
3
+ import { dirname as dirname2, join } from "node:path";
4
4
 
5
5
  // src/config-v2/overrides/constants.ts
6
6
  var OVERRIDES_COOKIE_PREFIX = "vercel-microfrontends-override";
@@ -184,21 +184,21 @@ var MicrofrontendConfigClient = class {
184
184
  isEqual(other) {
185
185
  return JSON.stringify(this.applications) === JSON.stringify(other.applications);
186
186
  }
187
- getApplicationNameForPath(path2) {
188
- if (!path2.startsWith("/")) {
187
+ getApplicationNameForPath(path3) {
188
+ if (!path3.startsWith("/")) {
189
189
  throw new Error(`Path must start with a /`);
190
190
  }
191
- if (this.pathCache[path2]) {
192
- return this.pathCache[path2];
191
+ if (this.pathCache[path3]) {
192
+ return this.pathCache[path3];
193
193
  }
194
- const pathname = new URL(path2, "https://example.com").pathname;
194
+ const pathname = new URL(path3, "https://example.com").pathname;
195
195
  for (const [name, application] of Object.entries(this.applications)) {
196
196
  if (application.routing) {
197
197
  for (const group of application.routing) {
198
198
  for (const childPath of group.paths) {
199
199
  const regexp = pathToRegexp(childPath);
200
200
  if (regexp.test(pathname)) {
201
- this.pathCache[path2] = name;
201
+ this.pathCache[path3] = name;
202
202
  return name;
203
203
  }
204
204
  }
@@ -211,7 +211,7 @@ var MicrofrontendConfigClient = class {
211
211
  if (!defaultApplication) {
212
212
  return null;
213
213
  }
214
- this.pathCache[path2] = defaultApplication[0];
214
+ this.pathCache[path3] = defaultApplication[0];
215
215
  return defaultApplication[0];
216
216
  }
217
217
  serialize() {
@@ -243,22 +243,22 @@ var validateConfigPaths = (applicationConfigsById) => {
243
243
  continue;
244
244
  }
245
245
  for (const pathMatch of app.routing) {
246
- for (const path2 of pathMatch.paths) {
247
- const tokens = parsePathRegexp(path2);
246
+ for (const path3 of pathMatch.paths) {
247
+ const tokens = parsePathRegexp(path3);
248
248
  for (const token of tokens.slice(0, -1)) {
249
249
  if (typeof token !== "string") {
250
250
  errors.push(
251
- `Path ${path2} may only have a :wildcard in the last path component`
251
+ `Path ${path3} may only have a :wildcard in the last path component`
252
252
  );
253
253
  }
254
254
  }
255
- const existing = pathsByApplicationId.get(path2);
255
+ const existing = pathsByApplicationId.get(path3);
256
256
  if (existing) {
257
257
  existing.applications.push(id);
258
258
  } else {
259
- pathsByApplicationId.set(path2, {
259
+ pathsByApplicationId.set(path3, {
260
260
  applications: [id],
261
- matcher: pathToRegexp2(path2),
261
+ matcher: pathToRegexp2(path3),
262
262
  applicationId: id
263
263
  });
264
264
  }
@@ -266,10 +266,10 @@ var validateConfigPaths = (applicationConfigsById) => {
266
266
  }
267
267
  }
268
268
  const entries = Array.from(pathsByApplicationId.entries());
269
- entries.forEach(([path2, { applications: ids, matcher, applicationId }]) => {
269
+ entries.forEach(([path3, { applications: ids, matcher, applicationId }]) => {
270
270
  if (ids.length > 1) {
271
271
  errors.push(
272
- `Duplicate path "${path2}" for applications "${ids.join(", ")}"`
272
+ `Duplicate path "${path3}" for applications "${ids.join(", ")}"`
273
273
  );
274
274
  }
275
275
  entries.forEach(
@@ -277,14 +277,14 @@ var validateConfigPaths = (applicationConfigsById) => {
277
277
  matchPath,
278
278
  { applications: matchIds, applicationId: matchApplicationId }
279
279
  ]) => {
280
- if (path2 === matchPath) {
280
+ if (path3 === matchPath) {
281
281
  return;
282
282
  }
283
283
  if (applicationId === matchApplicationId) {
284
284
  return;
285
285
  }
286
286
  if (matcher.test(matchPath)) {
287
- const source = `"${path2}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
287
+ const source = `"${path3}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
288
288
  const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
289
289
  errors.push(
290
290
  `Overlapping path detected between ${source} and ${destination}`
@@ -775,8 +775,82 @@ var Microfrontends = class {
775
775
  }
776
776
  };
777
777
 
778
- // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
778
+ // src/config-v2/microfrontends/utils/find-repository-root.ts
779
+ import fs from "node:fs";
779
780
  import path from "node:path";
781
+ var GIT_DIRECTORY = ".git";
782
+ function findRepositoryRoot(startDir) {
783
+ let currentDir = startDir || process.cwd();
784
+ while (currentDir !== path.parse(currentDir).root) {
785
+ const gitPath = path.join(currentDir, GIT_DIRECTORY);
786
+ if (fs.existsSync(gitPath) && fs.statSync(gitPath).isDirectory()) {
787
+ return currentDir;
788
+ }
789
+ currentDir = path.dirname(currentDir);
790
+ }
791
+ throw new Error(
792
+ "Repository root not found. Specify the root of the repository with the `repository.root` option."
793
+ );
794
+ }
795
+
796
+ // src/config-v2/microfrontends/utils/find-package-path.ts
797
+ import { dirname } from "node:path";
798
+ import { readFileSync } from "node:fs";
799
+ import fg from "fast-glob";
800
+ var configCache = {};
801
+ function findPackagePathWithGlob({
802
+ repositoryRoot,
803
+ name
804
+ }) {
805
+ try {
806
+ const packageJsonPaths = fg.globSync("**/package.json", {
807
+ cwd: repositoryRoot,
808
+ absolute: true,
809
+ onlyFiles: true,
810
+ followSymbolicLinks: false,
811
+ ignore: ["**/node_modules/**", "**/.git/**"]
812
+ });
813
+ const matchingPaths = [];
814
+ for (const packageJsonPath2 of packageJsonPaths) {
815
+ const packageJsonContent = readFileSync(packageJsonPath2, "utf-8");
816
+ const packageJson = JSON.parse(packageJsonContent);
817
+ if (packageJson.name === name) {
818
+ matchingPaths.push(packageJsonPath2);
819
+ }
820
+ }
821
+ if (matchingPaths.length > 1) {
822
+ throw new Error(
823
+ `Found multiple packages with the name "${name}" in the repository: ${matchingPaths.join(", ")}`
824
+ );
825
+ }
826
+ if (matchingPaths.length === 0) {
827
+ throw new Error(
828
+ `Could not find package with the name "${name}" in the repository`
829
+ );
830
+ }
831
+ const [packageJsonPath] = matchingPaths;
832
+ return dirname(packageJsonPath);
833
+ } catch (error) {
834
+ return null;
835
+ }
836
+ }
837
+ function findPackagePath(opts) {
838
+ const cacheKey = `${opts.repositoryRoot}-${opts.name}`;
839
+ if (configCache[cacheKey]) {
840
+ return configCache[cacheKey];
841
+ }
842
+ const result = findPackagePathWithGlob(opts);
843
+ if (!result) {
844
+ throw new Error(
845
+ `Could not find package with the name "${opts.name}" in the repository`
846
+ );
847
+ }
848
+ configCache[cacheKey] = result;
849
+ return result;
850
+ }
851
+
852
+ // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
853
+ import path2 from "node:path";
780
854
 
781
855
  // src/config-v2/microfrontends/server/constants.ts
782
856
  var MFE_CONFIG_DEFAULT_FILE_PATH = "microfrontends";
@@ -790,13 +864,13 @@ function isVercel() {
790
864
  // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
791
865
  function getOutputFilePath() {
792
866
  if (isVercel()) {
793
- return path.join(
867
+ return path2.join(
794
868
  ".vercel",
795
869
  MFE_CONFIG_DEFAULT_FILE_PATH,
796
870
  MFE_CONFIG_DEFAULT_FILE_NAME
797
871
  );
798
872
  }
799
- return path.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
873
+ return path2.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
800
874
  }
801
875
 
802
876
  // src/config-v2/microfrontends/server/validation.ts
@@ -1106,10 +1180,6 @@ var schema_v2_default = {
1106
1180
  type: "string",
1107
1181
  description: "flag name that can be used to enable/disable all paths in the group"
1108
1182
  },
1109
- routeToDefaultApplication: {
1110
- type: "boolean",
1111
- description: "True to route the request to the default application for this micro-frontends set-up. This must be `true` when using `flag` or when you want to use custom logic to make the routing decision for this group of paths."
1112
- },
1113
1183
  paths: {
1114
1184
  type: "array",
1115
1185
  items: {
@@ -1186,8 +1256,8 @@ var MicrofrontendsServer = class extends Microfrontends {
1186
1256
  pretty: true
1187
1257
  }) {
1188
1258
  const outputPath = getOutputFilePath();
1189
- fs.mkdirSync(dirname(outputPath), { recursive: true });
1190
- fs.writeFileSync(
1259
+ fs2.mkdirSync(dirname2(outputPath), { recursive: true });
1260
+ fs2.writeFileSync(
1191
1261
  outputPath,
1192
1262
  JSON.stringify(
1193
1263
  this.config.toSchemaJson(),
@@ -1255,12 +1325,32 @@ var MicrofrontendsServer = class extends Microfrontends {
1255
1325
  static fromFile({
1256
1326
  filePath,
1257
1327
  cookies,
1258
- meta
1328
+ meta,
1329
+ options
1259
1330
  }) {
1260
1331
  try {
1261
- const config = fs.readFileSync(filePath, "utf-8");
1332
+ const configJson = fs2.readFileSync(filePath, "utf-8");
1333
+ const config = MicrofrontendsServer.validate(configJson);
1334
+ if (!isMainConfig(config) && (options == null ? void 0 : options.resolveMainConfig)) {
1335
+ const repositoryRoot = findRepositoryRoot();
1336
+ const packagePath = findPackagePath({
1337
+ repositoryRoot,
1338
+ name: config.partOf
1339
+ });
1340
+ if (!packagePath) {
1341
+ throw new MicrofrontendError(
1342
+ `Could not find default application "${config.partOf}" in the repository`,
1343
+ { type: "config", subtype: "not_found" }
1344
+ );
1345
+ }
1346
+ const mainConfigPath = join(packagePath, "microfrontends.json");
1347
+ return MicrofrontendsServer.fromMainConfigFile({
1348
+ filePath: mainConfigPath,
1349
+ overrides: cookies ? parseOverrides(cookies) : void 0
1350
+ });
1351
+ }
1262
1352
  return new MicrofrontendsServer({
1263
- config: MicrofrontendsServer.validate(config),
1353
+ config,
1264
1354
  overrides: cookies ? parseOverrides(cookies) : void 0,
1265
1355
  meta
1266
1356
  });
@@ -1278,7 +1368,7 @@ var MicrofrontendsServer = class extends Microfrontends {
1278
1368
  overrides
1279
1369
  }) {
1280
1370
  try {
1281
- const config = fs.readFileSync(filePath, "utf-8");
1371
+ const config = fs2.readFileSync(filePath, "utf-8");
1282
1372
  const validatedConfig = MicrofrontendsServer.validate(config);
1283
1373
  if (!isMainConfig(validatedConfig)) {
1284
1374
  throw new MicrofrontendError(